"use client" import { useState, useEffect } from "react" import { ChevronRight } from "lucide-react" import { useBetslipStore } from "@/lib/store/betslip-store" import { fetchEvents, fetchOddsForEvent, get1X2FromOddsResponse, TOP_LEAGUES, type ApiEvent, } from "@/lib/betting-api" import { cn } from "@/lib/utils" type TopMatch = { id: string league: string time: string homeTeam: string awayTeam: string odds: { home: number; draw: number; away: number } } const FALLBACK_MATCHES: TopMatch[] = [ { id: "tm1", league: "England - Premier League", time: "05:00 PM", homeTeam: "Nottingham Forest", awayTeam: "Liverpool", odds: { home: 4.09, draw: 3.93, away: 1.82 } }, { id: "tm2", league: "England - Premier League", time: "11:00 PM", homeTeam: "Man City", awayTeam: "Newcastle", odds: { home: 1.50, draw: 5.17, away: 5.93 } }, { id: "tm3", league: "England - Premier League", time: "06:00 PM", homeTeam: "Chelsea", awayTeam: "Burnley", odds: { home: 1.21, draw: 6.91, away: 11.50 } }, { id: "tm4", league: "Spain - LaLiga", time: "07:30 PM", homeTeam: "Arsenal", awayTeam: "Wolves", odds: { home: 1.56, draw: 4.16, away: 5.80 } }, { id: "tm5", league: "Italy - Serie A", time: "09:45 PM", homeTeam: "Inter Milan", awayTeam: "Napoli", odds: { home: 1.85, draw: 3.60, away: 4.20 } }, ] function parseTime(iso: string): string { try { const d = new Date(iso) return d.toLocaleTimeString("en-GB", { hour: "2-digit", minute: "2-digit", hour12: true }) } catch { return "--:--" } } export function TopMatches() { const { bets, addBet } = useBetslipStore() const [matches, setMatches] = useState(FALLBACK_MATCHES) useEffect(() => { let cancelled = false const TOP_MATCHES_SIZE = 5 const leagueIds = TOP_LEAGUES.slice(0, 4).map((l) => l.id) const nameById = Object.fromEntries(TOP_LEAGUES.map((l) => [l.id, l.name])) Promise.all(leagueIds.map((league_id) => fetchEvents({ league_id, page_size: 2, page: 1 }))) .then(async (leagueResponses) => { if (cancelled) return const list: TopMatch[] = [] const eventMeta: { e: ApiEvent; leagueName: string }[] = [] leagueResponses.forEach((res, i) => { const leagueName = nameById[leagueIds[i]] ?? "" const events = res.data ?? [] for (const e of events) { eventMeta.push({ e, leagueName }) if (eventMeta.length >= TOP_MATCHES_SIZE) break } }) const oddsResponses = await Promise.all( eventMeta.slice(0, TOP_MATCHES_SIZE).map(({ e }) => fetchOddsForEvent(e.id).catch(() => ({ data: [] }))) ) eventMeta.slice(0, TOP_MATCHES_SIZE).forEach(({ e, leagueName }, i) => { const mainOdds = get1X2FromOddsResponse(oddsResponses[i]?.data ?? []) list.push({ id: String(e.id), league: leagueName, time: parseTime(e.start_time), homeTeam: e.home_team, awayTeam: e.away_team, odds: mainOdds ? { home: mainOdds["1"], draw: mainOdds.X, away: mainOdds["2"] } : { home: 0, draw: 0, away: 0 }, }) }) if (list.length > 0) setMatches(list) }) .catch(() => {}) return () => { cancelled = true } }, []) return (
{matches.map((match) => { const eventName = `${match.homeTeam} - ${match.awayTeam}` const leagueForBet = `Football - ${match.league}` const outcomes = [ { key: "1", label: "1", odds: match.odds.home }, { key: "x", label: "X", odds: match.odds.draw }, { key: "2", label: "2", odds: match.odds.away }, ] as const return (
{/* Top Label Ribbon */}
TOP
{match.league} {match.time}
{match.homeTeam}
VS
{match.awayTeam}
{outcomes.map(({ key, label, odds }) => { const betId = `${match.id}-${key}` const isSelected = bets.some((b) => b.id === betId) return ( ) })}
) })}
) }