Files
game-scoreboard/src/components/scoreboard/count-up.tsx
T
joshuaandClaude Opus 5.5 17eb76d036 Phase 4: scoreboard page, public API and realtime refresh
- buildScoreboardPayload is the single place deciding what the public may
  see: visible, frozen (snapshot scores, live current game), hidden (no
  scores, alphabetical) and reveal (unrevealed teams carry no score/order).
- GET /api/public/scoreboard/[token] validates the token, then loads the
  event with the service client in one parallel round trip.
- notify() pings sb/admin/game channels over Realtime's REST endpoint with
  no data; useRefreshChannel refetches immediately on a ping, throttles
  bursts, and falls back to a 10s poll, focus and reconnect.
- /s/[token]: FLIP rank animation, count-up totals, reduced motion,
  fullscreen, cursor hide and a connection indicator.
- Admin setup actions now notify, so renames and reorders show up live.

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

37 lines
1.4 KiB
TypeScript

"use client";
import { useEffect, useRef, useState } from "react";
/** Decimals needed to show `value` exactly, capped at 2 (totals are rounded to 2dp). */
const decimalsOf = (value: number) => {
const [, fraction = ""] = value.toFixed(2).replace(/0+$/, "").split(".");
return fraction.length;
};
/** Shows a number, counting up (or down) to each new value. Instant with reduced motion. */
export function CountUp({ value, durationMs }: { value: number; durationMs: number }) {
const [shown, setShown] = useState(value);
// Where the display currently is, so an update mid-count continues from there.
const shownRef = useRef(value);
useEffect(() => {
const from = shownRef.current;
if (from === value) return;
const reduced = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
const start = performance.now();
let frame = requestAnimationFrame(function tick(now) {
const progress = reduced ? 1 : Math.min(1, (now - start) / durationMs);
const eased = 1 - (1 - progress) ** 3;
const next = progress === 1 ? value : from + (value - from) * eased;
shownRef.current = next;
setShown(next);
if (progress < 1) frame = requestAnimationFrame(tick);
});
return () => cancelAnimationFrame(frame);
}, [value, durationMs]);
// Count in whole numbers when the target is whole, so digits don't flicker with decimals.
return <>{shown.toFixed(decimalsOf(value))}</>;
}