const KEY = "voiced.settingsTab"; export const SETTINGS_TABS = [ { id: "business", label: "Business" }, { id: "branding", label: "Branding" }, { id: "bank", label: "Bank & payments" }, { id: "defaults", label: "Invoice defaults" }, { id: "numbering", label: "Numbering" }, { id: "templates", label: "Templates & export" }, { id: "fonts", label: "Fonts" }, { id: "data", label: "Data" }, { id: "integrations", label: "Integrations" }, ] as const; export type SettingsTabId = (typeof SETTINGS_TABS)[number]["id"]; /** Index of a stored tab id; 0 (Business) for anything unknown. */ export function parseSettingsTab(value: string | null | undefined): number { const index = SETTINGS_TABS.findIndex((t) => t.id === value); return index < 0 ? 0 : index; } /** A tab another screen asked for; used once by the next Settings mount, so it works without localStorage. */ let requested: SettingsTabId | null = null; /** Makes the next opening of Settings land on `id` (for "Set up in Settings" links). */ export function requestSettingsTab(id: SettingsTabId): void { requested = id; } /** The last Settings tab this viewer used. A per-viewer convenience only. */ export function loadSettingsTab(): number { if (requested) { const index = parseSettingsTab(requested); requested = null; return index; } try { return parseSettingsTab(localStorage.getItem(KEY)); } catch { return 0; } } export function saveSettingsTab(index: number): void { try { const tab = SETTINGS_TABS[index]; if (tab) localStorage.setItem(KEY, tab.id); } catch { // Not remembering is harmless. } }