import { useState, useEffect, useCallback } from 'react'; import { NotificationPreferences } from '../types'; const DEFAULT_PREFERENCES: NotificationPreferences = { channels: { toast: { enabled: true, types: [], sound: false, vibrate: false, priority: 'all', }, bell: { enabled: true, types: [], sound: true, vibrate: false, priority: 'all', }, push: { enabled: false, types: [], sound: true, vibrate: true, priority: 'high', }, email: { enabled: true, types: [], sound: false, vibrate: false, priority: 'high', }, }, categories: {}, schedule: { quietHours: { enabled: false, start: '22:00', end: '08:00', allowUrgent: true, }, }, delivery: { batching: { enabled: false, window: 5, maxSize: 10, }, }, }; export function useNotificationPreferences() { const [preferences, setPreferences] = useState(DEFAULT_PREFERENCES); const [loading, setLoading] = useState(true); // Load preferences from storage useEffect(() => { const loadPreferences = async () => { try { // Load from localStorage or API const stored = localStorage.getItem('notification_preferences'); if (stored) { setPreferences(JSON.parse(stored)); } } catch (error) { console.error('Failed to load notification preferences:', error); } finally { setLoading(false); } }; loadPreferences(); }, []); const updatePreferences = useCallback(async ( updates: Partial ) => { const newPreferences = { ...preferences, ...updates, }; setPreferences(newPreferences); // Save to storage try { localStorage.setItem('notification_preferences', JSON.stringify(newPreferences)); } catch (error) { console.error('Failed to save notification preferences:', error); } }, [preferences]); const updateChannelPreference = useCallback(( channel: keyof NotificationPreferences['channels'], updates: any ) => { updatePreferences({ channels: { ...preferences.channels, [channel]: { ...preferences.channels[channel], ...updates, }, }, }); }, [preferences, updatePreferences]); const resetToDefaults = useCallback(() => { setPreferences(DEFAULT_PREFERENCES); localStorage.removeItem('notification_preferences'); }, []); return { preferences, loading, updatePreferences, updateChannelPreference, resetToDefaults, }; }