brackt/app/lib/wizard-state.ts
Chris Parsons 0ba6a2a408 Add auto-start draft at scheduled time (#437)
Commissioners can now opt in to having a draft start automatically at
its scheduled draftDateTime rather than requiring a manual "Start Draft"
click. The 1-second timer loop checks for eligible seasons each tick and
calls the new shared startDraft() service, which is also used by the
existing commissioner HTTP route.

- Add autoStartDraft boolean to seasons table (migration 0108)
- Extract core start logic into app/services/draft-autostart.ts so both
  the API route and the timer can call it without AsyncLocalStorage
- Add checkAndAutoStartDrafts() to the timer tick; disables autoStartDraft
  and stops retrying if no draft order is set at fire time
- Guard against null draftDateTime in both the timer query (isNotNull)
  and server actions (autoStartDraft forced false when datetime is null)
- Add "Auto-start at scheduled time" checkbox to league creation wizard
  (step 3) and league settings, gated on date+time being set
- Show countdown + "Start Now" button in draft room when auto-start is
  scheduled; reuses the existing nowForPauseCheck 1-second ticker
- Show orange warning banner on league homepage to commissioners when
  auto-start is within 1 hour and no draft order has been configured

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-16 23:31:51 -07:00

77 lines
2.7 KiB
TypeScript

import type { ScoringRules } from "~/lib/scoring-types";
export const WIZARD_SS_KEY = "brackt_new_league_wizard";
export type WizardSnapshot = {
leagueName: string;
teamCount: number;
joinAsPlayer: boolean;
selectedTemplate: string;
selectedSports: string[];
draftRounds: number;
draftDate: string;
draftTime: string;
autoStartDraft: boolean;
timerMode: "chess_clock" | "standard";
draftSpeed: string;
overnightMode: "none" | "league" | "per_user";
overnightStart: string;
overnightEnd: string;
overnightTimezone: string;
scoringPreset: "brackt" | "omnifantasy" | "custom";
scoringRules: ScoringRules;
userTimezone: string;
};
type SS = { getItem(k: string): string | null; setItem(k: string, v: string): void; removeItem(k: string): void };
function ss(): SS | null {
const g = globalThis as Record<string, unknown>;
return (typeof g["sessionStorage"] !== "undefined" ? g["sessionStorage"] : null) as SS | null;
}
export function saveWizardState(snapshot: WizardSnapshot) {
try { ss()?.setItem(WIZARD_SS_KEY, JSON.stringify(snapshot)); } catch {}
}
export function loadWizardState(): WizardSnapshot | null {
try {
const raw = ss()?.getItem(WIZARD_SS_KEY);
if (!raw) return null;
const parsed = JSON.parse(raw) as Partial<WizardSnapshot>;
return { autoStartDraft: false, ...parsed } as WizardSnapshot;
} catch { return null; }
}
export function clearWizardState() {
try { ss()?.removeItem(WIZARD_SS_KEY); } catch {}
}
export function buildFormDataFromSnapshot(s: WizardSnapshot): FormData {
const fd = new FormData();
fd.append("name", s.leagueName);
fd.append("teamCount", s.teamCount.toString());
if (s.selectedTemplate && s.selectedTemplate !== "customize") {
fd.append("templateId", s.selectedTemplate);
}
fd.append("draftRounds", s.draftRounds.toString());
if (s.draftDate && s.draftTime) {
fd.append("draftDateTime", new Date(`${s.draftDate}T${s.draftTime}`).toISOString());
}
if (s.autoStartDraft) fd.append("autoStartDraft", "on");
fd.append("draftTimerMode", s.timerMode);
fd.append("draftSpeed", s.draftSpeed);
fd.append("overnightPauseMode", s.overnightMode);
if (s.overnightMode !== "none") {
fd.append("overnightPauseStart", s.overnightStart);
fd.append("overnightPauseEnd", s.overnightEnd);
fd.append("overnightPauseTimezone", s.overnightTimezone);
}
(["pointsFor1st", "pointsFor2nd", "pointsFor3rd", "pointsFor4th",
"pointsFor5th", "pointsFor6th", "pointsFor7th", "pointsFor8th"] as const
).forEach((k) => fd.append(k, s.scoringRules[k].toString()));
for (const id of s.selectedSports) fd.append("sportsSeasons", id);
if (s.joinAsPlayer) fd.append("joinAsPlayer", "on");
if (s.userTimezone) fd.append("userTimezone", s.userTimezone);
return fd;
}