import { useEffect, useState } from 'react' import { useQuery } from '@tanstack/react-query' import { useNavigate } from '@tanstack/react-router' import { fetchRaceHub } from '../api' import { LocalDataNavigator } from '../components/LocalDataNavigator' import { RaceHubHeader } from '../components/RaceHubHeader' import { DatasetStrip } from '../components/DatasetStrip' import { ClassificationTable } from '../components/ClassificationTable' import { StartingGridTable } from '../components/StartingGridTable' import { TabBar, type Tab } from '../components/TabBar' import { DatasetStatusView } from '../components/DatasetStatusView' import { StrategyView } from '../components/StrategyView' import { PositionEvolutionView } from '../components/PositionEvolutionView' import { LapsView } from '../components/LapsView' import { RaceControlView } from '../components/RaceControlView' import { WeatherView } from '../components/WeatherView' interface Props { sessionKey: number } export function RaceHubPage({ sessionKey }: Props) { const navigate = useNavigate() const [inputVal, setInputVal] = useState(sessionKey > 0 ? String(sessionKey) : '') const [activeTab, setActiveTab] = useState('results') useEffect(() => { setInputVal(sessionKey > 0 ? String(sessionKey) : '') }, [sessionKey]) const { data, isLoading, isError, error } = useQuery({ queryKey: ['race-hub', sessionKey], queryFn: () => fetchRaceHub(sessionKey), enabled: sessionKey > 0, staleTime: 30_000, }) function handleLoad(e: React.FormEvent) { e.preventDefault() const key = parseInt(inputVal, 10) if (key > 0) { navigate({ to: '/race-hub', search: { session_key: key } }) } } return (
{/* Session key input */}
setInputVal(e.target.value)} /> {sessionKey > 0 && ( key {sessionKey} )}
{/* Local data browser when no session loaded */} {sessionKey === 0 && } {/* Loading */} {sessionKey > 0 && isLoading && (
loading session {sessionKey}…
)} {/* Error */} {isError && (
{error instanceof Error ? error.message : 'Failed to load race hub data'}
)} {/* Data */} {data && ( <> {activeTab === 'results' && (
Final Classification {data.results.length > 0 && ( {data.results.length} drivers )}
)} {activeTab === 'grid' && (
Starting Grid {data.starting_grid.length > 0 && ( {data.starting_grid.length} positions )}
)} {activeTab === 'strategy' && (
Race Strategy
)} {activeTab === 'positions' && (
Position Evolution
)} {activeTab === 'laps' && (
Laps {data.laps.length > 0 && ( {data.laps.length} samples )}
)} {activeTab === 'race_control' && (
Race Control {data.race_control.length > 0 && ( {data.race_control.length} messages )}
)} {activeTab === 'weather' && (
Weather {data.weather.length > 0 && ( {data.weather.length} samples )}
)} {activeTab === 'datasets' && (
Dataset Status
)} )}
) }