import { useMemo } from 'react'; import { useLocalStorage } from '@/hooks/useLocalStorage'; import { DEFAULT_NOTIFICATION_PREFS, NOTIFICATION_PREFS_KEY, type NotificationPrefs, } from '@/lib/notificationPrefs'; /** * Read/write the device-local notification preferences. * * Returns a memoized object (stable identity until the stored value changes) so * callers can safely put it in effect dependency arrays — an object literal that * is fresh every render would otherwise re-run those effects on every render. * Missing keys fall back to the defaults, so adding a new category later does * not leave existing users with `undefined`. */ export function useNotificationPrefs() { const [stored, setPrefs] = useLocalStorage( NOTIFICATION_PREFS_KEY, DEFAULT_NOTIFICATION_PREFS, ); const prefs = useMemo( () => ({ ...DEFAULT_NOTIFICATION_PREFS, ...(stored ?? {}) }), [stored], ); return [prefs, setPrefs] as const; }