'use client'; import PenaltyFAB from '@/components/leagues/PenaltyFAB'; import QuickPenaltyModal from '@/components/leagues/QuickPenaltyModal'; import { ReviewProtestModal } from '@/components/leagues/ReviewProtestModal'; import StewardingStats from '@/components/leagues/StewardingStats'; import Button from '@/components/ui/Button'; import Card from '@/components/ui/Card'; import { useLeagueStewardingMutations } from '@/hooks/league/useLeagueStewardingMutations'; import { AlertCircle, AlertTriangle, Calendar, ChevronRight, Flag, Gavel, MapPin, Video } from 'lucide-react'; import Link from 'next/link'; import { useMemo, useState } from 'react'; interface StewardingData { totalPending: number; totalResolved: number; totalPenalties: number; racesWithData: Array<{ race: { id: string; track: string; scheduledAt: Date }; pendingProtests: any[]; resolvedProtests: any[]; penalties: any[]; }>; allDrivers: any[]; driverMap: Record; } interface StewardingTemplateProps { data: StewardingData; leagueId: string; currentDriverId: string; onRefetch: () => void; } export function StewardingTemplate({ data, leagueId, currentDriverId, onRefetch }: StewardingTemplateProps) { const [activeTab, setActiveTab] = useState<'pending' | 'history'>('pending'); const [selectedProtest, setSelectedProtest] = useState(null); const [expandedRaces, setExpandedRaces] = useState>(new Set()); const [showQuickPenaltyModal, setShowQuickPenaltyModal] = useState(false); // Mutations using domain hook const { acceptProtestMutation, rejectProtestMutation } = useLeagueStewardingMutations(onRefetch); // Filter races based on active tab const filteredRaces = useMemo(() => { return activeTab === 'pending' ? data.racesWithData.filter(r => r.pendingProtests.length > 0) : data.racesWithData.filter(r => r.resolvedProtests.length > 0 || r.penalties.length > 0); }, [data, activeTab]); const handleAcceptProtest = async ( protestId: string, penaltyType: string, penaltyValue: number, stewardNotes: string ) => { // Find the protest to get details for penalty let foundProtest: any | undefined; data.racesWithData.forEach(raceData => { const p = raceData.pendingProtests.find(pr => pr.id === protestId) || raceData.resolvedProtests.find(pr => pr.id === protestId); if (p) foundProtest = { ...p, raceId: raceData.race.id }; }); if (foundProtest) { acceptProtestMutation.mutate({ protestId, penaltyType, penaltyValue, stewardNotes, raceId: foundProtest.raceId, accusedDriverId: foundProtest.accusedDriverId, reason: foundProtest.incident.description, }); } }; const handleRejectProtest = async (protestId: string, stewardNotes: string) => { rejectProtestMutation.mutate({ protestId, stewardNotes, }); }; const toggleRaceExpanded = (raceId: string) => { setExpandedRaces(prev => { const next = new Set(prev); if (next.has(raceId)) { next.delete(raceId); } else { next.add(raceId); } return next; }); }; 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; } }; return (

Stewarding

Quick overview of protests and penalties across all races

{/* Stats summary */} {/* Tab navigation */}
{/* Content */} {filteredRaces.length === 0 ? (

{activeTab === 'pending' ? 'All Clear!' : 'No History Yet'}

{activeTab === 'pending' ? 'No pending protests to review' : 'No resolved protests or penalties'}

) : (
{filteredRaces.map(({ race, pendingProtests, resolvedProtests, penalties }) => { const isExpanded = expandedRaces.has(race.id); const displayProtests = activeTab === 'pending' ? pendingProtests : resolvedProtests; return (
{/* Race Header */} {/* Expanded Content */} {isExpanded && (
{displayProtests.length === 0 && penalties.length === 0 ? (

No items to display

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

{protest.incident.description}

{protest.decisionNotes && (

Steward: {protest.decisionNotes}

)}
{(protest.status === 'pending' || protest.status === 'under_review') && ( )}
); })} {activeTab === 'history' && penalties.map((penalty) => { const driver = data.driverMap[penalty.driverId]; return (
{driver?.name || 'Unknown'} {penalty.type.replace('_', ' ')}

{penalty.reason}

{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`}
); })} )}
)}
); })}
)}
{activeTab === 'history' && ( setShowQuickPenaltyModal(true)} /> )} {selectedProtest && ( setSelectedProtest(null)} onAccept={handleAcceptProtest} onReject={handleRejectProtest} /> )} {showQuickPenaltyModal && ( setShowQuickPenaltyModal(false)} adminId={currentDriverId || ''} races={data.racesWithData.map(r => ({ id: r.race.id, track: r.race.track, scheduledAt: r.race.scheduledAt }))} /> )}
); }