'use client'; import { useEffect, useState } from 'react'; import { useRouter } from 'next/navigation'; import DriverCard from '@/components/drivers/DriverCard'; import RankBadge from '@/components/drivers/RankBadge'; import Input from '@/components/ui/Input'; import Card from '@/components/ui/Card'; import { getDriverRepository, getDriverStats, getAllDriverRankings } from '@/lib/di-container'; type SkillLevel = 'beginner' | 'intermediate' | 'advanced' | 'pro'; type DriverListItem = { id: string; name: string; rating: number; skillLevel: SkillLevel; nationality: string; racesCompleted: number; wins: number; podiums: number; isActive: boolean; rank: number; }; export default function DriversPage() { const router = useRouter(); const [drivers, setDrivers] = useState([]); const [loading, setLoading] = useState(true); const [searchQuery, setSearchQuery] = useState(''); const [selectedSkill, setSelectedSkill] = useState<'all' | SkillLevel>('all'); const [selectedNationality, setSelectedNationality] = useState('all'); const [activeOnly, setActiveOnly] = useState(false); const [sortBy, setSortBy] = useState<'rank' | 'rating' | 'wins' | 'podiums'>('rank'); useEffect(() => { const load = async () => { const driverRepo = getDriverRepository(); const allDrivers = await driverRepo.findAll(); const rankings = getAllDriverRankings(); const items: DriverListItem[] = allDrivers.map((driver) => { const stats = getDriverStats(driver.id); const rating = stats?.rating ?? 0; const wins = stats?.wins ?? 0; const podiums = stats?.podiums ?? 0; const totalRaces = stats?.totalRaces ?? 0; let effectiveRank = Number.POSITIVE_INFINITY; if (typeof stats?.overallRank === 'number' && stats.overallRank > 0) { effectiveRank = stats.overallRank; } else { const indexInGlobal = rankings.findIndex( (entry) => entry.driverId === driver.id, ); if (indexInGlobal !== -1) { effectiveRank = indexInGlobal + 1; } } const skillLevel: SkillLevel = rating >= 3000 ? 'pro' : rating >= 2500 ? 'advanced' : rating >= 1800 ? 'intermediate' : 'beginner'; const isActive = rankings.some((r) => r.driverId === driver.id); return { id: driver.id, name: driver.name, rating, skillLevel, nationality: driver.country, racesCompleted: totalRaces, wins, podiums, isActive, rank: effectiveRank, }; }); setDrivers(items); setLoading(false); }; void load(); }, []); const nationalities = Array.from( new Set(drivers.map((d) => d.nationality).filter(Boolean)), ).sort(); const filteredDrivers = drivers.filter((driver) => { const matchesSearch = driver.name .toLowerCase() .includes(searchQuery.toLowerCase()); const matchesSkill = selectedSkill === 'all' || driver.skillLevel === selectedSkill; const matchesNationality = selectedNationality === 'all' || driver.nationality === selectedNationality; const matchesActive = !activeOnly || driver.isActive; return matchesSearch && matchesSkill && matchesNationality && matchesActive; }); const sortedDrivers = [...filteredDrivers].sort((a, b) => { const rankA = Number.isFinite(a.rank) && a.rank > 0 ? a.rank : Number.POSITIVE_INFINITY; const rankB = Number.isFinite(b.rank) && b.rank > 0 ? b.rank : Number.POSITIVE_INFINITY; switch (sortBy) { case 'rank': return rankA - rankB || b.rating - a.rating || a.name.localeCompare(b.name); case 'rating': return b.rating - a.rating; case 'wins': return b.wins - a.wins; case 'podiums': return b.podiums - a.podiums; default: return 0; } }); const handleDriverClick = (driverId: string) => { router.push(`/drivers/${driverId}`); }; if (loading) { return (
Loading drivers...
); } return (

Drivers

Browse driver profiles and stats

setSearchQuery(e.target.value)} />

{sortedDrivers.length} {sortedDrivers.length === 1 ? 'driver' : 'drivers'} found

{sortedDrivers.map((driver) => ( handleDriverClick(driver.id)} /> ))}
{sortedDrivers.length === 0 && (

No drivers found matching your filters.

)}
); }