import { type Dispatch, type SetStateAction, useEffect, useState } from "react"; export function usePersistentState(key: string, initialValue: T | (() => T)): [T, Dispatch>] { const [value, setValue] = useState(() => { const fallback = typeof initialValue === "function" ? (initialValue as () => T)() : initialValue; if (typeof window === "undefined") return fallback; try { const stored = window.localStorage.getItem(key); return stored ? JSON.parse(stored) as T : fallback; } catch { return fallback; } }); useEffect(() => { try { window.localStorage.setItem(key, JSON.stringify(value)); } catch { // Storage can be unavailable in private mode; in-memory state still works. } }, [key, value]); return [value, setValue]; }