diff --git a/hooks/use-calendar-event-notifications.ts b/hooks/use-calendar-event-notifications.ts index 4a1e3b4..f64fb30 100644 --- a/hooks/use-calendar-event-notifications.ts +++ b/hooks/use-calendar-event-notifications.ts @@ -1,4 +1,4 @@ -import { useState, useEffect, useRef } from 'react'; +import { useState, useEffect, useRef, useCallback } from 'react'; import { Calendar } from 'lucide-react'; import { OutlookNotificationData } from '@/components/outlook-notification'; import { format } from 'date-fns'; @@ -24,6 +24,35 @@ export function useCalendarEventNotifications() { const eventsRef = useRef([]); const checkIntervalRef = useRef(null); + // Load notified event IDs from localStorage on mount + useEffect(() => { + try { + const stored = localStorage.getItem('notified-event-ids'); + if (stored) { + const ids = JSON.parse(stored); + notifiedEventIdsRef.current = new Set(ids); + console.log('[useCalendarEventNotifications] 📦 Loaded notified event IDs from localStorage', { + count: ids.length, + }); + } + } catch (error) { + console.error('[useCalendarEventNotifications] ❌ Error loading notified event IDs from localStorage', error); + } + }, []); + + // Save notified event IDs to localStorage whenever it changes + const saveNotifiedEventIds = useCallback(() => { + try { + const ids = Array.from(notifiedEventIdsRef.current); + localStorage.setItem('notified-event-ids', JSON.stringify(ids)); + console.log('[useCalendarEventNotifications] 💾 Saved notified event IDs to localStorage', { + count: ids.length, + }); + } catch (error) { + console.error('[useCalendarEventNotifications] ❌ Error saving notified event IDs to localStorage', error); + } + }, []); + useEffect(() => { console.log('[useCalendarEventNotifications] 🎧 Hook initialized, listening for calendar-events-updated'); @@ -170,10 +199,12 @@ export function useCalendarEventNotifications() { setEventNotification(notification); notifiedEventIdsRef.current.add(event.id); + saveNotifiedEventIds(); // Persist to localStorage // Clean up old notified events (older than 1 hour) to allow re-notification if needed setTimeout(() => { notifiedEventIdsRef.current.delete(event.id); + saveNotifiedEventIds(); // Update localStorage after cleanup }, 60 * 60 * 1000); // 1 hour } }; diff --git a/hooks/use-task-notifications.ts b/hooks/use-task-notifications.ts index f573767..df46368 100644 --- a/hooks/use-task-notifications.ts +++ b/hooks/use-task-notifications.ts @@ -1,4 +1,4 @@ -import { useState, useEffect, useRef } from 'react'; +import { useState, useEffect, useRef, useCallback } from 'react'; import { CheckSquare } from 'lucide-react'; import { OutlookNotificationData } from '@/components/outlook-notification'; import { format } from 'date-fns'; @@ -24,6 +24,35 @@ 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 { + const ids = Array.from(notifiedTaskIdsRef.current); + localStorage.setItem('notified-task-ids', JSON.stringify(ids)); + console.log('[useTaskNotifications] 💾 Saved notified task IDs to localStorage', { + count: ids.length, + }); + } catch (error) { + console.error('[useTaskNotifications] ❌ Error saving notified task IDs to localStorage', error); + } + }, []); + useEffect(() => { console.log('[useTaskNotifications] 🎧 Hook initialized, listening for tasks-updated'); @@ -295,10 +324,12 @@ export function useTaskNotifications() { setTaskNotification(notification); notifiedTaskIdsRef.current.add(task.id); + saveNotifiedTaskIds(); // Persist to localStorage // 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 }, 60 * 60 * 1000); // 1 hour } };