import { useEffect, useState } from 'react' import { useQuery } from '@tanstack/react-query' import { useNavigate } from '@tanstack/react-router' import { fetchLocalMeetings, fetchSeasons, fetchWeekend } from '../api' import { formatCoverageHint, sessionTypeAbbrev } from '../lib/coverage' import { countryDecal, formatGpDateRange } from '../lib/gpIdentity' interface Props { currentMeetingKey?: number currentSessionKey?: number onClose: () => void } export function WeekendSwitcher({ currentMeetingKey, currentSessionKey, onClose }: Props) { const navigate = useNavigate() const seasonsQuery = useQuery({ queryKey: ['seasons'], queryFn: fetchSeasons }) const [year, setYear] = useState(null) const [openMeetingKey, setOpenMeetingKey] = useState( currentMeetingKey ?? null, ) useEffect(() => { if (year == null && seasonsQuery.data?.length) { setYear(seasonsQuery.data[0]) } }, [seasonsQuery.data, year]) const meetingsQuery = useQuery({ queryKey: ['meetings', year], queryFn: () => fetchLocalMeetings(year!), enabled: year != null, }) const weekendQuery = useQuery({ queryKey: ['weekend', openMeetingKey], queryFn: () => fetchWeekend(openMeetingKey!), enabled: openMeetingKey != null, }) const seasons = seasonsQuery.data ?? [] const meetings = meetingsQuery.data ?? [] const weekend = weekendQuery.data function openSession(sessionKey: number) { navigate({ to: '/race-hub', search: { session_key: sessionKey } }) onClose() } return (
Switch Weekend
{seasons.map((y) => ( ))}
{meetingsQuery.isLoading && (
loading meetings…
)} {!meetingsQuery.isLoading && meetings.length === 0 && (
No meetings ingested for {year}.
)} {meetings.length > 0 && (
{meetings.map((m) => { const expanded = m.meeting_key === openMeetingKey const isCurrent = m.meeting_key === currentMeetingKey return (
{expanded && (
{weekendQuery.isLoading && (
loading sessions…
)} {weekend && weekend.meeting_key === m.meeting_key && weekend.sessions.map(({ session, source, datasets }) => { const active = session.session_key === currentSessionKey return ( ) })}
)}
) })}
)}
) }