import { useState, useCallback } from 'react'; export function useLocalStorage(key: string, initialValue: T): [T, (value: T | ((prev: T) => T)) => void] { const [storedValue, setStoredValue] = useState(() => { try { const item = localStorage.getItem(key); return item ? (JSON.parse(item) as T) : initialValue; } catch { return initialValue; } }); const setValue = useCallback( (value: T | ((prev: T) => T)) => { setStoredValue((prev) => { const next = value instanceof Function ? value(prev) : value; localStorage.setItem(key, JSON.stringify(next)); return next; }); }, [key], ); return [storedValue, setValue]; }