import { formatClock, type RoomState } from '@two-keys/shared'; import { useNow } from '../lib/hooks'; /** Elapsed time plus penalties, or a countdown when the game has a time limit. Runs locally between server updates. */ export function Timer({ timer, receivedAt }: { timer: RoomState['timer']; receivedAt: number }) { const now = useNow(1000); if (timer.startedAt === null) return null; const serverNow = timer.serverNow + (now - receivedAt); const elapsed = ((timer.finishedAt ?? serverNow) - timer.startedAt) / 1000; if (timer.limitSeconds !== null) { const used = elapsed + timer.penaltySeconds; const left = Math.max(0, timer.limitSeconds - used); const label = timer.limitLabel ?? 'Time left'; return (
{label} {formatClock(left)}
{/* Fills up as the time (and every hint penalty) runs out. */}
); } return (
{formatClock(elapsed + timer.penaltySeconds)} {timer.penaltySeconds > 0 && ( +{formatClock(timer.penaltySeconds)} )}
); }