import { useMemo } from "react"; import { DraftTimer } from "./DraftTimer"; interface Team { id: string; name: string; draftOrder: number; ownerId: string | null; } interface DraftPick { id: string; teamId: string; participantId: string; pickNumber: number; round: number; pickInRound: number; pickedByType: "owner" | "commissioner" | "auto"; } interface Participant { id: string; name: string; sport: string; } interface Timer { teamId: string; timeRemaining: number; } interface DraftGridProps { teams: Team[]; draftPicks: DraftPick[]; participants: Participant[]; draftRounds: number; currentPickNumber: number; userTeamId?: string; timers?: Timer[]; } export function DraftGrid({ teams, draftPicks, participants, draftRounds, currentPickNumber, userTeamId, timers = [], }: DraftGridProps) { // Create a map of participant IDs to participant data for quick lookup const participantMap = useMemo(() => { return new Map(participants.map((p) => [p.id, p])); }, [participants]); // Create a map of pick numbers to draft picks const pickMap = useMemo(() => { return new Map(draftPicks.map((p) => [p.pickNumber, p])); }, [draftPicks]); // Create a map of team IDs to timers const timerMap = useMemo(() => { return new Map(timers.map((t) => [t.teamId, t])); }, [timers]); // Calculate pick number for a given round and team const getPickNumber = (round: number, teamIndex: number): number => { const isOddRound = round % 2 === 1; const pickInRound = isOddRound ? teamIndex + 1 : teams.length - teamIndex; return (round - 1) * teams.length + pickInRound; }; return (
{/* Header Row - Team Names */}
Round
{teams.map((team) => { const timer = timerMap.get(team.id); // Determine if this team is on the clock const currentPick = Math.ceil(currentPickNumber / teams.length); const pickInRound = ((currentPickNumber - 1) % teams.length) + 1; const isOddRound = currentPick % 2 === 1; const currentTeamIndex = isOddRound ? pickInRound - 1 : teams.length - pickInRound; const isOnClock = teams[currentTeamIndex]?.id === team.id; return (
{team.name}
{timer && (
)}
); })}
{/* Draft Rounds */} {Array.from({ length: draftRounds }, (_, roundIndex) => { const round = roundIndex + 1; const isOddRound = round % 2 === 1; return (
{/* Round Number */}
{round}
{/* Picks for this round */} {teams.map((team, teamIndex) => { const pickNumber = getPickNumber(round, teamIndex); const pick = pickMap.get(pickNumber); const participant = pick ? participantMap.get(pick.participantId) : null; const isCurrentPick = pickNumber === currentPickNumber; const isPastPick = pickNumber < currentPickNumber; const isUserTeam = team.id === userTeamId; return (
{/* Pick Number */}
{round}.{String(isOddRound ? teamIndex + 1 : teams.length - teamIndex).padStart(2, "0")}
{/* Participant or Status */} {participant ? (
{participant.name}
{participant.sport}
{pick && pick.pickedByType !== "owner" && (
({pick.pickedByType})
)}
) : isCurrentPick ? (
On the clock
) : isPastPick ? (
Skipped
) : (
-
)}
); })}
); })}
); }