import { memo, useMemo } from "react"; import { Badge } from "~/components/ui/badge"; interface TeamsDraftedGridProps { draftSlots: Array<{ id: string; team: { id: string; name: string; }; }>; ownerMap?: Record; picks: Array<{ id: string; team: { id: string; name: string; }; participant: { id: string; name: string; }; sport: { id: string; name: string; }; }>; sports: Array<{ id: string; name: string; }>; season: { numFlexPicks: number; }; } export const TeamsDraftedGrid = memo(function TeamsDraftedGrid({ draftSlots, picks, sports, season, ownerMap = {}, }: TeamsDraftedGridProps) { // 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) ?? new Map(); if (!map.has(pick.team.id)) map.set(pick.team.id, teamMap); 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(); draftSlots.forEach((slot) => { const teamPicks = picks.filter((p) => p.team.id === slot.team.id); const uniqueSportsPicked = new Set(teamPicks.map((p) => p.sport.id)).size; const flexUsed = Math.max(0, teamPicks.length - uniqueSportsPicked); map.set(slot.team.id, flexUsed); }); return map; }, [picks, draftSlots]); if (sports.length === 0) { return (

No sports have been configured for this season.

); } 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
{ownerMap[slot.team.id] || slot.team.name}
{flexUsed} of {season.numFlexPicks} flex
{sport.name} {teamSportPicks.length > 0 ? (
{teamSportPicks.map((pick, i) => (
{pick.participant.name} {hasMultiplePicks && ( {i + 1} )}
))}
) : null}
); });