"use client"; import { useRouter } from "next/navigation"; import { useCallback, useMemo, useState } from "react"; import { saveAdminScore } from "@/app/admin/live-actions"; import { Button } from "@/components/ui/button"; import { BlockInput, NumberStepper } from "@/components/ui/score-inputs"; import { formatScore } from "@/lib/criteria"; import { usePendingEdits } from "@/lib/hooks/use-pending-edits"; import type { LiveGame, LiveView } from "@/lib/live-view"; import { ErrorText } from "./error-text"; const cellKey = (teamId: string, criterionId: string) => `${teamId}|${criterionId}`; export function ScoringGrid({ eventId, game, teams, }: { eventId: string; game: LiveGame; teams: LiveView["teams"]; }) { const router = useRouter(); // The admin's own rows: the scores in a direct game, the overrides in a judged game. const adminValues = useMemo(() => { const values: Record = {}; for (const [teamId, cells] of Object.entries(game.cells)) { for (const [criterionId, cell] of Object.entries(cells)) { if (cell.adminValue !== null) values[cellKey(teamId, criterionId)] = cell.adminValue; } } return values; }, [game.cells]); const save = useCallback( (key: string, value: number | null) => { const [teamId, criterionId] = key.split("|"); return saveAdminScore(eventId, game.id, teamId, criterionId, value); }, [eventId, game.id], ); const refresh = useCallback(() => router.refresh(), [router]); const { edit, valueOf, isSaving, errorFor } = usePendingEdits(adminValues, save, refresh); // Judged games show averages; the override input for a cell only opens on request, // which keeps a 5-criteria grid on screen without sideways scrolling. const [overriding, setOverriding] = useState(null); if (game.criteria.length === 0) { return

This game has no criteria yet. Add some on the setup page.

; } if (teams.length === 0) { return

No teams yet. Add teams on the setup page.

; } const judged = game.scoringMode === "judged"; const error = errorFor(); return (
{game.criteria.map((criterion) => ( ))} {teams.map((team) => { const result = game.results[team.id]; const spotlit = game.spotlightTeamId === team.id; return ( {game.criteria.map((criterion) => { const cell = game.cells[team.id]?.[criterion.id]; const key = cellKey(team.id, criterion.id); const value = valueOf(key); const inputProps = { range: criterion, value, label: `${judged ? "Override value: " : ""}${criterion.label} for ${team.name}`, onChange: (next: number | null) => edit(key, next), }; return ( ); })} ); })}
Team {criterion.label} {criterion.description && ( {criterion.description} )} Total Points
{team.name} {spotlit && ★ Now performing} {judged && ( {cell?.judgeAverage == null ? ( No judge scores yet ) : ( <> {formatScore(cell.judgeAverage)} {" "} avg of {cell.judgeCount} {cell.judgeCount === 1 ? "judge" : "judges"} )} {value !== null && ( Override {formatScore(value)} )} )} {judged && value === null && overriding !== key ? ( ) : criterion.inputType === "blocks" ? ( ) : ( )} {result?.raw == null ? "–" : formatScore(result.raw)} {result?.raw == null ? "–" : formatScore(result.points)} {game.pointsMode === "placement" && result?.placement != null && ( place {result.placement} )}

{isSaving() ? "Saving…" : error ? "" : "All scores saved ✓"}

{error} {judged && (

“Override” replaces the judges' average for that cell until you clear it (tap the selected block again, or empty the number).

)}
); }