import { useState } from 'react' import { useQuery } from '@tanstack/react-query' import { Link } from 'react-router-dom' import { LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer } from 'recharts' import { format } from 'date-fns' import api from '../utils/api' import { formatDuration, formatDate } from '../utils/format' const SPORTS = ['running', 'cycling', 'swimming'] const DISTANCE_ORDER = [ '400m', '800m', '1k', '1 mile', '3k', '5k', '10k', 'Half marathon', 'Marathon', '50k', '100k', ] export default function RecordsPage() { const [sport, setSport] = useState('running') const [selectedDistance, setSelectedDistance] = useState(null) const { data: records } = useQuery({ queryKey: ['records', sport], queryFn: () => api.get('/records/', { params: { sport_type: sport } }).then(r => r.data), }) const { data: history } = useQuery({ queryKey: ['record-history', selectedDistance, sport], queryFn: () => api.get(`/records/history/${encodeURIComponent(selectedDistance)}`, { params: { sport_type: sport }, }).then(r => r.data), enabled: !!selectedDistance, }) // Sort by standard distance order const sortedRecords = records?.slice().sort((a, b) => { const ai = DISTANCE_ORDER.indexOf(a.distance_label) const bi = DISTANCE_ORDER.indexOf(b.distance_label) return (ai === -1 ? 999 : ai) - (bi === -1 ? 999 : bi) }) return (

Personal Records

{/* Sport selector */}
{SPORTS.map(s => ( ))}
{sortedRecords?.length === 0 && (

🏆

No records yet — import activities to track your best times

)}
{/* Records table */}
{sortedRecords?.map(rec => ( setSelectedDistance(rec.distance_label)} className={`border-b border-gray-800/50 cursor-pointer transition-colors ${ selectedDistance === rec.distance_label ? 'bg-blue-900/20' : 'hover:bg-gray-800/40' }`} > ))}
Distance Best time Date
{rec.distance_label} {formatDuration(rec.duration_s)} {formatDate(rec.achieved_at)} e.stopPropagation()} className="text-xs text-blue-400 hover:underline" > View →
{/* Progress chart */}
{selectedDistance && history ? ( <>

{selectedDistance} progression

Lower is faster

{history.length > 1 ? ( ({ date: h.achieved_at, time: h.duration_s, }))} margin={{ top: 4, right: 4, bottom: 4, left: 8 }} > format(new Date(d), 'MMM yy')} /> format(new Date(d), 'MMM d, yyyy')} formatter={v => [formatDuration(v), 'Time']} /> ) : (
Only one record — complete more activities to see progression
)} ) : (
Select a distance to see your progression
)}
) }