/** * Mode Storage * Safe localStorage wrapper for mode persistence * Handles localStorage failures gracefully */ export type DocDoeMode = "tuition" | "study-chat" | "first-hour" | "learn-anything" | "exam-coach" | "home"; const STORAGE_KEYS = { MODE: "docdoe_selected_mode", LAST_QUERY: "docdoe_last_query", LAST_EXAM_DATE: "docdoe_last_exam_date", LAST_DURATION: "docdoe_last_duration", } as const; /** * Safely get an item from localStorage * Returns null if localStorage is unavailable or item doesn't exist */ export function getStorageItem(key: string): T | null { try { if (typeof window === "undefined") { return null; } const item = window.localStorage.getItem(key); return item ? JSON.parse(item) : null; } catch { // Silently fail if localStorage is not available return null; } } /** * Safely set an item in localStorage * Returns false if localStorage is unavailable */ export function setStorageItem(key: string, value: T): boolean { try { if (typeof window === "undefined") { return false; } window.localStorage.setItem(key, JSON.stringify(value)); return true; } catch { // Silently fail if localStorage is not available return false; } } /** * Get the current selected mode */ export function getMode(): DocDoeMode | null { return getStorageItem(STORAGE_KEYS.MODE); } /** * Set the current selected mode */ export function setMode(mode: DocDoeMode): boolean { return setStorageItem(STORAGE_KEYS.MODE, mode); } /** * Get the last query the user submitted */ export function getLastQuery(): string | null { return getStorageItem(STORAGE_KEYS.LAST_QUERY); } /** * Set the last query the user submitted */ export function setLastQuery(query: string): boolean { return setStorageItem(STORAGE_KEYS.LAST_QUERY, query); } /** * Get the last exam date the user entered */ export function getLastExamDate(): string | null { return getStorageItem(STORAGE_KEYS.LAST_EXAM_DATE); } /** * Set the last exam date the user entered */ export function setLastExamDate(date: string): boolean { return setStorageItem(STORAGE_KEYS.LAST_EXAM_DATE, date); } /** * Get the last duration the user selected */ export function getLastDuration(): number | null { return getStorageItem(STORAGE_KEYS.LAST_DURATION); } /** * Set the last duration the user selected (in minutes) */ export function setLastDuration(duration: number): boolean { return setStorageItem(STORAGE_KEYS.LAST_DURATION, duration); } /** * Clear all mode-related storage */ export function clearModeStorage(): boolean { try { if (typeof window === "undefined") { return false; } Object.values(STORAGE_KEYS).forEach((key) => { window.localStorage.removeItem(key); }); return true; } catch { return false; } }