'use client'; import { useState } from 'react'; import { Modal } from '@/ui/Modal'; import { Button } from '@/ui/Button'; import { Box } from '@/ui/Box'; import { Text } from '@/ui/Text'; import { Stack } from '@/ui/Stack'; import { Icon } from '@/ui/Icon'; import { InfoBox } from '@/ui/InfoBox'; import { Input } from '@/ui/Input'; import { TextArea } from '@/ui/TextArea'; import { Select } from '@/ui/Select'; import { useFileProtest } from "@/hooks/race/useFileProtest"; import { AlertTriangle, CheckCircle2, } from 'lucide-react'; export interface ProtestParticipant { id: string; name: string; } interface FileProtestModalProps { isOpen: boolean; onClose: () => void; raceId: string; leagueId?: string; protestingDriverId: string; participants: ProtestParticipant[]; } export function FileProtestModal({ isOpen, onClose, raceId, protestingDriverId, participants, }: FileProtestModalProps) { const fileProtestMutation = useFileProtest(); const [errorMessage, setErrorMessage] = useState(null); // Form state const [accusedDriverId, setAccusedDriverId] = useState(''); const [lap, setLap] = useState(''); const [timeInRace, setTimeInRace] = useState(''); const [description, setDescription] = useState(''); const [comment, setComment] = useState(''); const [proofVideoUrl, setProofVideoUrl] = useState(''); const otherParticipants = participants.filter(p => p.id !== protestingDriverId); const handleSubmit = async () => { if (!accusedDriverId || !lap || !description) { setErrorMessage('Please fill in all required fields'); return; } try { setErrorMessage(null); fileProtestMutation.mutate({ raceId, protestingDriverId, accusedDriverId, incident: { lap: parseInt(lap, 10), description, timeInRace: timeInRace ? parseInt(timeInRace, 10) : undefined, }, comment, proofVideoUrl, }, { onSuccess: () => { // Reset form state on success setAccusedDriverId(''); setLap(''); setTimeInRace(''); setDescription(''); setComment(''); setProofVideoUrl(''); }, onError: (error: Error) => { setErrorMessage(error.message || 'Failed to file protest'); }, }); } catch (error) { const errorMessage = error instanceof Error ? error.message : 'Failed to validate protest input'; setErrorMessage(errorMessage); } }; const handleClose = () => { // Reset form state setErrorMessage(null); setAccusedDriverId(''); setLap(''); setTimeInRace(''); setDescription(''); setComment(''); setProofVideoUrl(''); fileProtestMutation.reset(); onClose(); }; // Show success state when mutation is successful if (fileProtestMutation.isSuccess) { return ( Your protest has been submitted The stewards will review your protest and make a decision. You'll be notified of the outcome. ); } return ( {errorMessage && ( )} {/* Driver Selection */} setLap(e.target.value)} disabled={fileProtestMutation.isPending} placeholder="e.g. 5" /> setTimeInRace(e.target.value)} disabled={fileProtestMutation.isPending} placeholder="Optional" /> {/* Incident Description */}