/** * Pure step/panel state logic for the onboarding wizard (P2-6.4). * * Extracted from onboarding-wizard.tsx so the transitions are unit-testable; * the wizard component imports these helpers and constants unchanged. */ /** First setup-step panel index (panels 0 and 1 are welcome + telemetry consent). */ export const FIRST_SETUP_PANEL = 2; /** * Sentinel returned by getNextPanelAfterCompletedStep when the wizard has no * further panel to show and should finish (show success / close). It is not a * real panel index. */ export const FINISH_PANEL = -1; /** Mixpanel step_name for setup-step panels (matches stepDefinitions order). */ export const ONBOARDING_SETUP_STEP_KEYS = [ 'llms_txt', 'robots_txt', 'first_llm_visit', ] as const; export type OnboardingStepKey = (typeof ONBOARDING_SETUP_STEP_KEYS)[number]; export type OnboardingStepsState = Record; /** Last setup-step panel index. */ export const LAST_SETUP_PANEL = FIRST_SETUP_PANEL + ONBOARDING_SETUP_STEP_KEYS.length - 1; /** 0 = pre-welcome, 1 = telemetry opt-in, then setup steps (matches Mixpanel step_number). */ export const WIZARD_PANEL_COUNT = LAST_SETUP_PANEL + 1; /** * Initial panel for the wizard: pre-welcome when nothing is done yet, * otherwise the first incomplete setup step (or the last setup panel when * all setup steps are complete). */ export function getInitialWizardStep(steps: OnboardingStepsState): number { const anyDone = ONBOARDING_SETUP_STEP_KEYS.some((key) => steps[key]); if (!anyDone) return 0; for (let i = 0; i < ONBOARDING_SETUP_STEP_KEYS.length; i++) { if (!steps[ONBOARDING_SETUP_STEP_KEYS[i]]) return i + FIRST_SETUP_PANEL; } return LAST_SETUP_PANEL; } /** * Panel shown after the user confirms a completed step with "Next": * earlier setup panels advance linearly; the last setup panel finishes the * wizard (returns FINISH_PANEL) when everything is done, or jumps back to * the first incomplete step otherwise. */ export function getNextPanelAfterCompletedStep( wizardStep: number, steps: OnboardingStepsState, ): number { if (wizardStep >= FIRST_SETUP_PANEL && wizardStep < LAST_SETUP_PANEL) { return wizardStep + 1; } if (wizardStep === LAST_SETUP_PANEL) { const allDone = ONBOARDING_SETUP_STEP_KEYS.every((key) => steps[key]); if (allDone) { return FINISH_PANEL; } for (let i = 0; i < ONBOARDING_SETUP_STEP_KEYS.length; i++) { if (!steps[ONBOARDING_SETUP_STEP_KEYS[i]]) return i + FIRST_SETUP_PANEL; } } return wizardStep; }