import { Badge } from "~/components/ui/badge"; import { useMemo } from "react"; interface TeamsDraftedGridProps { draftSlots: Array<{ id: string; draftOrder: number; team: { id: string; name: string; seasonId: string; }; }>; picks: Array<{ id: string; team: { id: string; name: string; }; participant: { id: string; name: string; }; sport: { id: string; name: string; type: string; }; }>; availableParticipants: Array<{ id: string; name: string; sport: { id: string; name: string; }; }>; season: { id: string; name: string; year: number; numFlexPicks: number; sports?: Array<{ id: string; name: string; }>; }; } export function TeamsDraftedGrid({ draftSlots, picks, availableParticipants, season, }: TeamsDraftedGridProps) { // Get unique sports from all available participants (not just picked ones) const sports = useMemo(() => { const sportMap = new Map(); availableParticipants.forEach((participant) => { if (!sportMap.has(participant.sport.id)) { sportMap.set(participant.sport.id, { id: participant.sport.id, name: participant.sport.name, }); } }); return Array.from(sportMap.values()).sort((a, b) => a.name.localeCompare(b.name) ); }, [availableParticipants]); // Calculate picks by team and sport const picksByTeamAndSport = useMemo(() => { const map = new Map>(); picks.forEach((pick) => { if (!map.has(pick.team.id)) { map.set(pick.team.id, new Map()); } const teamMap = map.get(pick.team.id)!; if (!teamMap.has(pick.sport.id)) { teamMap.set(pick.sport.id, []); } teamMap.get(pick.sport.id)!.push(pick); }); return map; }, [picks]); // Calculate flex picks used by each team const flexPicksByTeam = useMemo(() => { const map = new Map(); picks.forEach((pick) => { // Flex picks are counted based on sport type or a different logic // For now, we'll just count total picks and subtract sport-specific limits // This is a simplified version - adjust based on your actual flex logic if (!map.has(pick.team.id)) { map.set(pick.team.id, 0); } }); // Calculate flex usage based on season settings draftSlots.forEach((slot) => { const teamPicks = picks.filter((p) => p.team.id === slot.team.id); const totalPicks = teamPicks.length; // This is simplified - you may need to implement actual flex calculation // based on sport-specific limits in your season settings const flexUsed = Math.max(0, totalPicks - sports.length); map.set(slot.team.id, flexUsed); }); return map; }, [picks, draftSlots, sports]); if (sports.length === 0) { return (

No picks have been made yet

); } return (
{draftSlots.map((slot, index) => { const flexUsed = flexPicksByTeam.get(slot.team.id) || 0; const isLast = index === draftSlots.length - 1; return ( ); })} {sports.map((sport, sportIndex) => { const isLastRow = sportIndex === sports.length - 1; return ( {draftSlots.map((slot, slotIndex) => { const teamSportPicks = picksByTeamAndSport .get(slot.team.id) ?.get(sport.id) || []; const hasMultiplePicks = teamSportPicks.length > 1; const isLastCol = slotIndex === draftSlots.length - 1; return ( ); })} ); })}
Sport
{slot.team.name}
{flexUsed} of {season.numFlexPicks} flex
{sport.name} {teamSportPicks.length > 0 ? (
{teamSportPicks.map((pick) => (
{pick.participant.name} {hasMultiplePicks && ( {teamSportPicks.indexOf(pick) + 1} )}
))}
) : null}
); }