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 (
🏆
No records yet — import activities to track your best times
| 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 → |
Lower is faster
{history.length > 1 ? (