'use client'; import { useState, useEffect } from 'react'; import { useParams, useRouter } from 'next/navigation'; import Link from 'next/link'; import { AlertTriangle, Clock, CheckCircle, Flag, Calendar, MapPin, AlertCircle, Video, Gavel, ArrowLeft, Scale, ChevronRight, Users, Trophy, } from 'lucide-react'; import Card from '@/components/ui/Card'; import Button from '@/components/ui/Button'; import Breadcrumbs from '@/components/layout/Breadcrumbs'; import { apiClient } from '@/lib/apiClient'; import { useEffectiveDriverId } from '@/lib/currentDriver'; import { isLeagueAdminOrHigherRole } from '@/lib/leagueRoles'; import type { RaceProtestsViewModel, RacePenaltiesViewModel } from '@/lib/apiClient'; export default function RaceStewardingPage() { const params = useParams(); const router = useRouter(); const raceId = params.id as string; const currentDriverId = useEffectiveDriverId(); const [race, setRace] = useState(null); // TODO: Define proper race type const [league, setLeague] = useState(null); // TODO: Define proper league type const [protestsData, setProtestsData] = useState(null); const [penaltiesData, setPenaltiesData] = useState(null); const [loading, setLoading] = useState(true); const [isAdmin, setIsAdmin] = useState(false); const [activeTab, setActiveTab] = useState<'pending' | 'resolved' | 'penalties'>('pending'); useEffect(() => { async function loadData() { setLoading(true); try { // Get race detail for basic info const raceDetail = await apiClient.races.getDetail(raceId, currentDriverId); setRace(raceDetail.race); setLeague(raceDetail.league); if (raceDetail.league) { // TODO: Implement admin check via API setIsAdmin(true); } // Get protests and penalties const [protestsData, penaltiesData] = await Promise.all([ apiClient.races.getProtests(raceId), apiClient.races.getPenalties(raceId), ]); setProtestsData(protestsData); setPenaltiesData(penaltiesData); } catch (err) { console.error('Failed to load data:', err); } finally { setLoading(false); } } loadData(); }, [raceId, currentDriverId]); const pendingProtests = protestsData?.protests.filter( (p) => p.status === 'pending' || p.status === 'under_review', ) ?? []; const resolvedProtests = protestsData?.protests.filter( (p) => p.status === 'upheld' || p.status === 'dismissed' || p.status === 'withdrawn', ) ?? []; const getStatusBadge = (status: string) => { switch (status) { case 'pending': case 'under_review': return ( Pending ); case 'upheld': return ( Upheld ); case 'dismissed': return ( Dismissed ); case 'withdrawn': return ( Withdrawn ); default: return null; } }; const formatDate = (date: Date | string) => { const d = typeof date === 'string' ? new Date(date) : date; return d.toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: 'numeric', }); }; if (loading) { return (
); } if (!race) { return (

Race not found

The race you're looking for doesn't exist.

); } const breadcrumbItems = [ { label: 'Races', href: '/races' }, { label: race.track, href: `/races/${race.id}` }, { label: 'Stewarding' }, ]; return (
{/* Navigation */}
{/* Header */}

Stewarding

{race.track} • {formatDate(race.scheduledAt)}

{/* Stats */}
Pending
{pendingProtests.length}
Resolved
{resolvedProtests.length}
Penalties
{penaltiesData?.penalties.length ?? 0}
{/* Tab Navigation */}
{/* Content */} {activeTab === 'pending' && (
{pendingProtests.length === 0 ? (

All Clear!

No pending protests to review

) : ( pendingProtests.map((protest) => { const protester = protestsData?.driverMap[protest.protestingDriverId]; const accused = protestsData?.driverMap[protest.accusedDriverId]; const daysSinceFiled = Math.floor( (Date.now() - new Date(protest.filedAt).getTime()) / (1000 * 60 * 60 * 24) ); const isUrgent = daysSinceFiled > 2; return (
{protester?.name || 'Unknown'} vs {accused?.name || 'Unknown'} {getStatusBadge(protest.status)} {isUrgent && ( {daysSinceFiled}d old )}
Lap {protest.incident.lap} Filed {formatDate(protest.filedAt)} {protest.proofVideoUrl && ( <> )}

{protest.incident.description}

{isAdmin && league && ( )}
); }) )}
)} {activeTab === 'resolved' && (
{resolvedProtests.length === 0 ? (

No Resolved Protests

Resolved protests will appear here

) : ( resolvedProtests.map((protest) => { const protester = protestsData?.driverMap[protest.protestingDriverId]; const accused = protestsData?.driverMap[protest.accusedDriverId]; return (
{protester?.name || 'Unknown'} vs {accused?.name || 'Unknown'} {getStatusBadge(protest.status)}
Lap {protest.incident.lap} Filed {formatDate(protest.filedAt)}

{protest.incident.description}

{protest.decisionNotes && (

Steward Decision

{protest.decisionNotes}

)}
); }) )}
)} {activeTab === 'penalties' && (
{penalties.length === 0 ? (

No Penalties

Penalties issued for this race will appear here

) : ( penaltiesData?.penalties.map((penalty) => { const driver = penaltiesData?.driverMap[penalty.driverId]; return (
{driver?.name || 'Unknown'} {penalty.type.replace('_', ' ')}

{penalty.reason}

{penalty.notes && (

{penalty.notes}

)}
{penalty.type === 'time_penalty' && `+${penalty.value}s`} {penalty.type === 'grid_penalty' && `+${penalty.value} grid`} {penalty.type === 'points_deduction' && `-${penalty.value} pts`} {penalty.type === 'disqualification' && 'DSQ'} {penalty.type === 'warning' && 'Warning'} {penalty.type === 'license_points' && `${penalty.value} LP`}
); }) )}
)}
); }