diff --git a/hooks/use-task-notifications.ts b/hooks/use-task-notifications.ts index df46368..60746b3 100644 --- a/hooks/use-task-notifications.ts +++ b/hooks/use-task-notifications.ts @@ -24,22 +24,6 @@ export function useTaskNotifications() { const tasksRef = useRef([]); const checkIntervalRef = useRef(null); - // Load notified task IDs from localStorage on mount - useEffect(() => { - try { - const stored = localStorage.getItem('notified-task-ids'); - if (stored) { - const ids = JSON.parse(stored); - notifiedTaskIdsRef.current = new Set(ids); - console.log('[useTaskNotifications] ๐Ÿ“ฆ Loaded notified task IDs from localStorage', { - count: ids.length, - }); - } - } catch (error) { - console.error('[useTaskNotifications] โŒ Error loading notified task IDs from localStorage', error); - } - }, []); - // Save notified task IDs to localStorage whenever it changes const saveNotifiedTaskIds = useCallback(() => { try { @@ -56,6 +40,21 @@ export function useTaskNotifications() { useEffect(() => { console.log('[useTaskNotifications] ๐ŸŽง Hook initialized, listening for tasks-updated'); + // Load notified task IDs from localStorage synchronously at the start + try { + const stored = localStorage.getItem('notified-task-ids'); + if (stored) { + const ids = JSON.parse(stored); + notifiedTaskIdsRef.current = new Set(ids); + console.log('[useTaskNotifications] ๐Ÿ“ฆ Loaded notified task IDs from localStorage', { + count: ids.length, + ids: Array.from(ids), + }); + } + } catch (error) { + console.error('[useTaskNotifications] โŒ Error loading notified task IDs from localStorage', error); + } + // Listen for tasks updates const handleTasksUpdate = (event: CustomEvent) => { const tasks = event.detail?.tasks || []; @@ -193,11 +192,12 @@ export function useTaskNotifications() { const dueTasksWithDates: TaskWithParsedDate[] = []; for (const task of tasksRef.current) { - // Skip if already notified + // Skip if already notified (check both in-memory and localStorage) if (notifiedTaskIdsRef.current.has(task.id)) { - console.log('[useTaskNotifications] โญ๏ธ Task already notified', { + console.log('[useTaskNotifications] โญ๏ธ Task already notified (skipping)', { id: task.id, title: task.headline, + notifiedIds: Array.from(notifiedTaskIdsRef.current), }); continue; } @@ -322,29 +322,74 @@ export function useTaskNotifications() { ], }; + console.log('[useTaskNotifications] ๐ŸŽฏ Setting task notification', { + taskId: task.id, + title: task.headline, + beforeAdd: Array.from(notifiedTaskIdsRef.current), + }); + setTaskNotification(notification); notifiedTaskIdsRef.current.add(task.id); - saveNotifiedTaskIds(); // Persist to localStorage + saveNotifiedTaskIds(); // Persist to localStorage immediately + + console.log('[useTaskNotifications] โœ… Task notification set and saved', { + taskId: task.id, + afterAdd: Array.from(notifiedTaskIdsRef.current), + }); // Clean up old notified tasks (older than 1 hour) to allow re-notification if needed setTimeout(() => { notifiedTaskIdsRef.current.delete(task.id); saveNotifiedTaskIds(); // Update localStorage after cleanup + console.log('[useTaskNotifications] ๐Ÿงน Cleaned up old notification for task', { + taskId: task.id, + }); }, 60 * 60 * 1000); // 1 hour } }; + // Clean up localStorage periodically (remove IDs for tasks that no longer exist or are too old) + const cleanupLocalStorage = () => { + try { + const stored = localStorage.getItem('notified-task-ids'); + if (stored) { + const storedIds = JSON.parse(stored); + const currentTaskIds = new Set(tasksRef.current.map(t => t.id)); + + // Remove IDs that are no longer in the current tasks list + const validIds = storedIds.filter((id: string) => currentTaskIds.has(id)); + + if (validIds.length !== storedIds.length) { + localStorage.setItem('notified-task-ids', JSON.stringify(validIds)); + notifiedTaskIdsRef.current = new Set(validIds); + console.log('[useTaskNotifications] ๐Ÿงน Cleaned up localStorage', { + removed: storedIds.length - validIds.length, + remaining: validIds.length, + }); + } + } + } catch (error) { + console.error('[useTaskNotifications] โŒ Error cleaning up localStorage', error); + } + }; + // Check immediately and then every 10 seconds for more responsive notifications checkForDueTasks(); checkIntervalRef.current = setInterval(checkForDueTasks, 10000); // Every 10 seconds + + // Clean up localStorage every 5 minutes + const cleanupInterval = setInterval(cleanupLocalStorage, 5 * 60 * 1000); return () => { window.removeEventListener('tasks-updated', handleTasksUpdate as EventListener); if (checkIntervalRef.current) { clearInterval(checkIntervalRef.current); } + if (cleanupInterval) { + clearInterval(cleanupInterval); + } }; - }, []); + }, [saveNotifiedTaskIds]); const handleDismiss = () => { setTaskNotification(null);