* Fix custom chess-clock timer detection and save settings blocker
- Extract getInitialDraftSpeed() helper so both useState init and
resetSettingsFormState use the same logic; unrecognized presets
now produce a "custom:{bank}:{incr}" string instead of falling
back to "standard", so the custom section auto-expands correctly
(e.g. 8 hr bank + 30 min increment is no longer shown as Standard)
- resetSettingsFormState was not resetting draftSpeed at all; fixed
- Clear hasUnsavedSettingsChanges in the Form's onSubmit so the
useBlocker check is false at the moment the save navigation starts,
preventing the "Leave without saving?" dialog from firing on save
- Replace the cleared-on-success useEffect with one that re-marks
dirty when the action returns an error, so the warning reappears
after a failed save
https://claude.ai/code/session_01DgHNkvJXGizx41CE2tkVS1
* Address code review: single source of truth for presets, useEffect guard, tests
- Extract CHESS_CLOCK_PRESETS (pure data) into draft-timer.ts as the
canonical source of truth; DraftSpeedPicker.tsx now spreads those
values into CHESS_PRESETS rather than duplicating the numbers
- getInitialDraftSpeed moved to draft-timer.ts and rewritten to use
CHESS_CLOCK_PRESETS.find() — no more hardcoded bankSec/incrSec values
in three separate places
- parseDraftSpeed switch replaced with CHESS_CLOCK_PRESETS.find() and
an explicit fallback comment; eliminates the silent default-handles-
"standard" fragility called out in review
- useEffect that re-marks settings dirty now guards against non-settings
errors (draft-order, commissioner actions) which carry a `section`
field — previously any error actionData would spuriously set
hasUnsavedSettingsChanges=true
- Add parseDraftSpeed and getInitialDraftSpeed test suites to
draft-timer.test.ts, including parametrised preset coverage via
it.each(CHESS_CLOCK_PRESETS)
https://claude.ai/code/session_01DgHNkvJXGizx41CE2tkVS1
* Fix lint: use !== null/undefined instead of != null
oxlint enforces eqeqeq; the null check in getInitialDraftSpeed used
!= which triggered two errors.
https://claude.ai/code/session_01DgHNkvJXGizx41CE2tkVS1
---------
Co-authored-by: Claude <noreply@anthropic.com>
131 lines
5 KiB
TypeScript
131 lines
5 KiB
TypeScript
/**
|
|
* Pure utility functions for the draft clock/timer system.
|
|
* Extracted here to be independently testable and shared across
|
|
* route handlers and UI components.
|
|
*
|
|
* Clock model (chess-clock / Fischer increment):
|
|
* - Draft start: every team receives `initialTime` as their bank.
|
|
* - While on the clock: the active team's bank counts down each second.
|
|
* - After a pick is made: the picker's bank += `incrementTime`.
|
|
* - Between turns: all other teams' banks are left untouched.
|
|
*/
|
|
|
|
/**
|
|
* Formats a HH:MM time string as 12-hour time with AM/PM.
|
|
* e.g. "23:00" → "11:00 PM", "07:30" → "7:30 AM"
|
|
*/
|
|
export function formatTime12h(t: string): string {
|
|
const [h, m] = t.split(":").map(Number);
|
|
const ap = (h || 0) >= 12 ? "PM" : "AM";
|
|
return `${(h || 0) % 12 || 12}:${String(m || 0).padStart(2, "0")} ${ap}`;
|
|
}
|
|
|
|
/**
|
|
* Formats a seconds value for display.
|
|
* undefined → "--:--"
|
|
* < 3600 s → "m:ss"
|
|
* >= 3600 s → "h:mm:ss"
|
|
*/
|
|
export function formatClockTime(seconds: number | undefined): string {
|
|
if (seconds === undefined) return "--:--";
|
|
const clamped = Math.max(0, seconds); // guard against negative values from timer drift
|
|
const h = Math.floor(clamped / 3600);
|
|
const m = Math.floor((clamped % 3600) / 60);
|
|
const s = clamped % 60;
|
|
if (h > 0)
|
|
return `${h}:${String(m).padStart(2, "0")}:${String(s).padStart(2, "0")}`;
|
|
return `${m}:${String(s).padStart(2, "0")}`;
|
|
}
|
|
|
|
/**
|
|
* Returns a team's new bank time after they complete a pick.
|
|
* The increment is the post-pick reward (chess clock model).
|
|
*
|
|
* @param bankTime Current remaining seconds in the team's bank
|
|
* @param incrementTime Bonus seconds awarded after each pick
|
|
*/
|
|
export function calculateTimeAfterPick(
|
|
bankTime: number,
|
|
incrementTime: number
|
|
): number {
|
|
return bankTime + incrementTime;
|
|
}
|
|
|
|
/** Pure preset data for chess-clock mode — the single source of truth shared with DraftSpeedPicker. */
|
|
export const CHESS_CLOCK_PRESETS: { value: string; bankSec: number; incrSec: number }[] = [
|
|
{ value: "fast", bankSec: 60, incrSec: 10 },
|
|
{ value: "standard", bankSec: 120, incrSec: 15 },
|
|
{ value: "slow", bankSec: 28800, incrSec: 3600 },
|
|
{ value: "very-slow", bankSec: 43200, incrSec: 3600 },
|
|
];
|
|
|
|
/**
|
|
* Converts a draftSpeed form value + timerMode into DB time fields.
|
|
*
|
|
* Standard mode: speed is raw seconds (the per-pick time); both fields equal it.
|
|
* Chess clock mode: speed is a named preset that maps to (initial bank, increment).
|
|
*/
|
|
export function parseDraftSpeed(
|
|
draftSpeed: string | null,
|
|
draftTimerMode: "chess_clock" | "standard"
|
|
): { draftInitialTime: number; draftIncrementTime: number } {
|
|
if (draftTimerMode === "standard") {
|
|
const seconds = parseInt(draftSpeed ?? "", 10);
|
|
const time = isNaN(seconds) ? 90 : seconds;
|
|
return { draftInitialTime: time, draftIncrementTime: time };
|
|
}
|
|
|
|
if (draftSpeed?.startsWith("custom:")) {
|
|
const [, bankStr, incrStr] = draftSpeed.split(":");
|
|
const bank = parseInt(bankStr ?? "", 10);
|
|
const incr = parseInt(incrStr ?? "", 10);
|
|
if (!isNaN(bank) && !isNaN(incr)) return { draftInitialTime: bank, draftIncrementTime: incr };
|
|
}
|
|
|
|
const preset = CHESS_CLOCK_PRESETS.find((p) => p.value === draftSpeed);
|
|
if (preset) return { draftInitialTime: preset.bankSec, draftIncrementTime: preset.incrSec };
|
|
|
|
// Unknown/null speed — fall back to "standard" preset
|
|
return { draftInitialTime: 120, draftIncrementTime: 15 };
|
|
}
|
|
|
|
type DraftSpeedSource = {
|
|
draftTimerMode?: "chess_clock" | "standard" | null;
|
|
draftInitialTime?: number | null;
|
|
draftIncrementTime?: number | null;
|
|
} | null | undefined;
|
|
|
|
/**
|
|
* Derives the draftSpeed UI value (preset key or "custom:{bank}:{incr}") from
|
|
* saved DB fields. Used to initialise and reset the DraftSpeedPicker.
|
|
*/
|
|
export function getInitialDraftSpeed(season: DraftSpeedSource): string {
|
|
const mode = season?.draftTimerMode ?? "chess_clock";
|
|
if (mode === "standard") return season?.draftIncrementTime?.toString() ?? "90";
|
|
const preset = CHESS_CLOCK_PRESETS.find(
|
|
(p) => p.bankSec === season?.draftInitialTime && p.incrSec === season?.draftIncrementTime
|
|
);
|
|
if (preset) return preset.value;
|
|
if (season?.draftInitialTime !== null && season?.draftInitialTime !== undefined &&
|
|
season?.draftIncrementTime !== null && season?.draftIncrementTime !== undefined) {
|
|
return `custom:${season.draftInitialTime}:${season.draftIncrementTime}`;
|
|
}
|
|
return "standard";
|
|
}
|
|
|
|
/**
|
|
* Returns the Tailwind colour class(es) for a timer value.
|
|
*
|
|
* > 60 s → green (plenty of time)
|
|
* > 30 s → amber (getting tight)
|
|
* > 10 s → coral (urgent)
|
|
* ≤ 10 s → coral + animate-pulse (critical)
|
|
* undefined → muted (clock not running)
|
|
*/
|
|
export function getTimerColorClass(seconds: number | undefined): string {
|
|
if (seconds === undefined) return "text-muted-foreground";
|
|
if (seconds > 60) return "text-emerald-400";
|
|
if (seconds > 30) return "text-amber-accent";
|
|
if (seconds > 10) return "text-coral-accent";
|
|
return "text-coral-accent animate-pulse";
|
|
}
|