brackt/app/lib/wizard-state.ts
Chris Parsons e201ecd28a
Extract reusable league wizard components; wire settings page (#350)
* Extract reusable league wizard components; wire settings page (#103)

Extracts 9 domain components from new.tsx and $leagueId.settings.tsx into
app/components/league/ (each with a Storybook story), consolidates wizard
form-building into wizard-state.ts, and updates the settings page to use
the shared components instead of hand-rolled duplicates. new.tsx shrinks
from ~2000 → ~1280 lines.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* Fix sports-season test: add participants to mock data

findDraftableSportsSeasons now returns participantCount, which requires
participants in the mock db response.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* Fix sports-season test: loosen makeMockDb type to Record<string, unknown>[]

typeof mockSeasons became too strict after adding participants to the mock
data, breaking inline arrays in other tests that don't include participants.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-28 22:24:13 -07:00

71 lines
2.5 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;
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);
return raw ? (JSON.parse(raw) as WizardSnapshot) : null;
} 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());
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());
}
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;
}