const isBrowser = typeof window !== 'undefined'; const rootKey = 'uooc-progress'; export const storageKeys = { accessToken: `${rootKey}:access-token`, selectedPlatformId: (userId: number) => `${rootKey}:user:${userId}:selected-platform-id`, selectedCourseIds: (userId: number) => `${rootKey}:user:${userId}:selected-course-map`, courseOptions: (userId: number) => `${rootKey}:user:${userId}:course-options`, progressSnapshots: (userId: number) => `${rootKey}:user:${userId}:progress-snapshots`, }; export function readText(key: string, fallback = ''): string { if (!isBrowser) { return fallback; } return window.localStorage.getItem(key) ?? fallback; } export function writeText(key: string, value: string): void { if (!isBrowser) { return; } window.localStorage.setItem(key, value); } export function removeKey(key: string): void { if (!isBrowser) { return; } window.localStorage.removeItem(key); } export function readJson(key: string, fallback: T): T { if (!isBrowser) { return fallback; } const raw = window.localStorage.getItem(key); if (!raw) { return fallback; } try { return JSON.parse(raw) as T; } catch { return fallback; } } export function writeJson(key: string, value: T): void { if (!isBrowser) { return; } window.localStorage.setItem(key, JSON.stringify(value)); }