'use client'; import { useState, useEffect, use } from 'react'; import Link from 'next/link'; import { useRouter, useParams } from 'next/navigation'; import { getDriverRepository } from '@/lib/di-container'; import DriverProfile from '@/components/drivers/DriverProfile'; import Button from '@/components/ui/Button'; import Card from '@/components/ui/Card'; import Breadcrumbs from '@/components/layout/Breadcrumbs'; import { EntityMappers } from '@gridpilot/racing/application/mappers/EntityMappers'; import type { DriverDTO } from '@gridpilot/racing/application/dto/DriverDTO'; export default function DriverDetailPage({ searchParams, }: { searchParams: any; }) { const router = useRouter(); const params = useParams(); const driverId = params.id as string; const [driver, setDriver] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const unwrappedSearchParams = use(searchParams) as URLSearchParams | undefined; const from = typeof unwrappedSearchParams?.get === 'function' ? unwrappedSearchParams.get('from') ?? undefined : undefined; const leagueId = typeof unwrappedSearchParams?.get === 'function' ? unwrappedSearchParams.get('leagueId') ?? undefined : undefined; const raceId = typeof unwrappedSearchParams?.get === 'function' ? unwrappedSearchParams.get('raceId') ?? undefined : undefined; let backLink: string | null = null; if (from === 'league-standings' && leagueId) { backLink = `/leagues/${leagueId}/standings`; } else if (from === 'league' && leagueId) { backLink = `/leagues/${leagueId}`; } else if (from === 'league-members' && leagueId) { backLink = `/leagues/${leagueId}`; } else if (from === 'league-race' && leagueId && raceId) { backLink = `/leagues/${leagueId}/races/${raceId}`; } else { backLink = null; } useEffect(() => { loadDriver(); // eslint-disable-next-line react-hooks/exhaustive-deps }, [driverId]); const loadDriver = async () => { try { const driverRepo = getDriverRepository(); const driverEntity = await driverRepo.findById(driverId); if (!driverEntity) { setError('Driver not found'); setLoading(false); return; } const driverDto = EntityMappers.toDriverDTO(driverEntity); if (!driverDto) { setError('Driver not found'); setLoading(false); return; } setDriver(driverDto); } catch (err) { setError(err instanceof Error ? err.message : 'Failed to load driver'); } finally { setLoading(false); } }; if (loading) { return (
Loading driver profile...
); } if (error || !driver) { return (
{error || 'Driver not found'}
); } return (
{backLink && (
← Back to league
)} {/* Breadcrumb */} {/* Driver Profile Component */}
); }