Files
game-scoreboard/src/components/admin/scoring-grid.tsx
T
joshuaandClaude Opus 5.5 2702491096 Phase 7: judges — links, phone page, open link, judged control room
- Setup: nominate judges, copy personal links, QR codes (generated on
  demand in the browser), revoke/restore, and an open-link toggle.
- GET /api/judge/[token] returns only that judge's own scores; PUT
  .../scores runs every check from ARCHITECTURE.md in checkJudgeWrite
  (pure, unit-tested) after one parallel lookup, plus a rate limit.
- /j/[token]: mobile cards per team, spotlight pinned first, autosave
  with per-card status, read-only when locked/done, link-off when revoked.
- /jo/[token]: name form -> personal link, remembered in localStorage so
  a re-scan doesn't create a second judge.
- Control room: averages with on-request overrides, judge progress
  matrix, per-judge breakdown, spotlight select and lock toggle.
- usePendingEdits shares the optimistic autosave logic between the admin
  grid and the judge page.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-03 15:29:33 +02:00

182 lines
7.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"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<string, number> = {};
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<string | null>(null);
if (game.criteria.length === 0) {
return <p>This game has no criteria yet. Add some on the setup page.</p>;
}
if (teams.length === 0) {
return <p>No teams yet. Add teams on the setup page.</p>;
}
const judged = game.scoringMode === "judged";
const error = errorFor();
return (
<div className="flex flex-col gap-2">
<div className="overflow-x-auto">
<table className="w-full border-separate border-spacing-y-1 text-left">
<thead>
<tr className="text-sm">
<th scope="col" className="pr-3 font-bold">
Team
</th>
{game.criteria.map((criterion) => (
<th key={criterion.id} scope="col" className="px-2 align-bottom font-bold">
{criterion.label}
{criterion.description && (
<span className="block font-normal text-ink/70">{criterion.description}</span>
)}
</th>
))}
<th scope="col" className="px-2 text-right font-bold">
Total
</th>
<th scope="col" className="pl-2 text-right font-bold">
Points
</th>
</tr>
</thead>
<tbody>
{teams.map((team) => {
const result = game.results[team.id];
const spotlit = game.spotlightTeamId === team.id;
return (
<tr key={team.id} className={spotlit ? "bg-floodlight/25" : "bg-chalk"}>
<th scope="row" className="rounded-l-md py-2 pr-3 pl-2 font-bold whitespace-nowrap">
<span
aria-hidden
className="mr-2 inline-block size-3 rounded-full align-middle"
style={{ backgroundColor: team.color }}
/>
{team.name}
{spotlit && <span className="block text-sm font-normal">★ Now performing</span>}
</th>
{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 (
<td key={criterion.id} className="px-2 py-2 align-top">
{judged && (
<span className="mb-1 block text-sm">
{cell?.judgeAverage == null ? (
<span className="text-ink/70">No judge scores yet</span>
) : (
<>
<strong
className={`text-lg tabular-nums ${value !== null ? "text-ink/50 line-through" : ""}`}
>
{formatScore(cell.judgeAverage)}
</strong>{" "}
<span className="text-ink/70">
avg of {cell.judgeCount} {cell.judgeCount === 1 ? "judge" : "judges"}
</span>
</>
)}
{value !== null && (
<strong className="ml-1 rounded bg-alert px-1.5 text-white">
Override {formatScore(value)}
</strong>
)}
</span>
)}
{judged && value === null && overriding !== key ? (
<Button
variant="secondary"
size="sm"
aria-label={`Override ${criterion.label} for ${team.name}`}
onClick={() => setOverriding(key)}
>
Override
</Button>
) : criterion.inputType === "blocks" ? (
<BlockInput compact color={team.color} {...inputProps} />
) : (
<NumberStepper {...inputProps} />
)}
</td>
);
})}
<td className="px-2 py-2 text-right font-bold tabular-nums">
{result?.raw == null ? "–" : formatScore(result.raw)}
</td>
<td className="rounded-r-md py-2 pr-2 pl-2 text-right font-bold tabular-nums">
{result?.raw == null ? "–" : formatScore(result.points)}
{game.pointsMode === "placement" && result?.placement != null && (
<span className="block text-sm font-normal text-ink/70">place {result.placement}</span>
)}
</td>
</tr>
);
})}
</tbody>
</table>
</div>
<p role="status" className="text-sm font-bold">
{isSaving() ? "Saving…" : error ? "" : "All scores saved ✓"}
</p>
<ErrorText>{error}</ErrorText>
{judged && (
<p className="text-sm text-ink/70">
&ldquo;Override&rdquo; replaces the judges&apos; average for that cell until you clear it
(tap the selected block again, or empty the number).
</p>
)}
</div>
);
}