import { useState, useEffect, useMemo } from "react"; import { Button } from "~/components/ui/button"; import { Badge } from "~/components/ui/badge"; import type { DraftIneligibilityReason } from "~/lib/draft-eligibility"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "~/components/ui/dialog"; interface ParticipantSelectionDialogProps { open: boolean; onOpenChange: (open: boolean) => void; title: string; description: string; participants: Array<{ id: string; name: string; sport: { id: string; name: string }; }>; draftedParticipantIds: Set; /** Pass the old participant's ID to keep them in the list (replace-pick dialog) */ allowParticipantId?: string; eligibility: { eligibleSportIds: Set; ineligibleReasons: Record; } | null; /** Marks a row with "Current" badge and "Keep" button label */ currentParticipantId?: string; uniqueSports: string[]; onSelect: (participantId: string) => void; } export function ParticipantSelectionDialog({ open, onOpenChange, title, description, participants, draftedParticipantIds, allowParticipantId, eligibility, currentParticipantId, uniqueSports, onSelect, }: ParticipantSelectionDialogProps) { const [searchQuery, setSearchQuery] = useState(""); const [sportFilter, setSportFilter] = useState("all"); // Reset filters each time the dialog opens useEffect(() => { if (open) { setSearchQuery(""); setSportFilter("all"); } }, [open]); const filtered = useMemo( () => participants.filter((p) => { if (p.id !== allowParticipantId && draftedParticipantIds.has(p.id)) return false; if ( searchQuery && !p.name.toLowerCase().includes(searchQuery.toLowerCase()) ) return false; if (sportFilter !== "all" && p.sport.name !== sportFilter) return false; return true; }), [participants, draftedParticipantIds, allowParticipantId, searchQuery, sportFilter] ); return ( {title} {description}
setSearchQuery(e.target.value)} />
{filtered.map((participant) => { const isCurrentPick = participant.id === currentParticipantId; const isEligible = eligibility ? eligibility.eligibleSportIds.has(participant.sport.id) : true; const ineligibleReason = eligibility?.ineligibleReasons[participant.sport.id] ?? null; return ( ); })}
Participant Sport Action
{participant.name} {isCurrentPick && ( Current )} {!isEligible && ( Ineligible )}
{!isEligible && ineligibleReason && (

{ineligibleReason.message}

)}
{participant.sport.name}
); }