From ab6846b3f749384d6c834d1c5fcc0cd2d22026de Mon Sep 17 00:00:00 2001 From: alma Date: Fri, 16 Jan 2026 02:59:02 +0100 Subject: [PATCH] refactor Notifications agenda --- hooks/use-calendar-event-notifications.ts | 32 +++++++++++++++-------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/hooks/use-calendar-event-notifications.ts b/hooks/use-calendar-event-notifications.ts index 04d56ff..4a1e3b4 100644 --- a/hooks/use-calendar-event-notifications.ts +++ b/hooks/use-calendar-event-notifications.ts @@ -70,7 +70,7 @@ export function useCalendarEventNotifications() { window.addEventListener('calendar-events-updated', handleEventsUpdate as EventListener); - // Check for events that are starting now (every minute) + // Check for events that are starting soon (every 10 seconds) const checkForStartingEvents = () => { const now = new Date(); const currentTime = now.getTime(); @@ -81,8 +81,12 @@ export function useCalendarEventNotifications() { notifiedCount: notifiedEventIdsRef.current.size, }); - // Check events that start within the next 5 minutes (to catch events that just started or are about to start) - const upcomingWindow = 5 * 60 * 1000; // 5 minutes in milliseconds + // Show notification 3 minutes before event starts + // Window: between 3 minutes 30 seconds and 2 minutes 30 seconds before start + // This gives a 1-minute window to catch the notification + const notificationTimeBefore = 3 * 60 * 1000; // 3 minutes before + const windowStart = notificationTimeBefore + 30 * 1000; // 3 min 30 sec before (start of window) + const windowEnd = notificationTimeBefore - 30 * 1000; // 2 min 30 sec before (end of window) const startingEvents = eventsRef.current.filter((event) => { // Skip if already notified @@ -97,6 +101,10 @@ export function useCalendarEventNotifications() { const eventStartTime = event.start.getTime(); const timeUntilStart = eventStartTime - currentTime; const timeUntilStartMinutes = Math.round(timeUntilStart / 1000 / 60); + const timeUntilStartSeconds = Math.round(timeUntilStart / 1000); + + // Check if event is in the notification window (3 minutes before, with 1-minute tolerance) + const inNotificationWindow = timeUntilStart <= windowStart && timeUntilStart >= windowEnd; console.log('[useCalendarEventNotifications] ⏰ Checking event', { id: event.id, @@ -104,15 +112,14 @@ export function useCalendarEventNotifications() { start: event.start.toISOString(), now: now.toISOString(), timeUntilStartMinutes, - inWindow: timeUntilStart >= -2 * 60 * 1000 && timeUntilStart <= upcomingWindow, + timeUntilStartSeconds, + inWindow: inNotificationWindow, + windowStart: Math.round(windowStart / 1000), + windowEnd: Math.round(windowEnd / 1000), }); - // Event is starting now (within last 2 minutes) or within the next 5 minutes - // This allows catching events that just started or are about to start - return ( - timeUntilStart >= -2 * 60 * 1000 && // Not more than 2 minutes ago (just started) - timeUntilStart <= upcomingWindow // Within next 5 minutes - ); + // Event should be notified 3 minutes before start (with 1-minute window for detection) + return inNotificationWindow; }); if (startingEvents.length > 0) { @@ -132,12 +139,15 @@ export function useCalendarEventNotifications() { const timeStr = event.isAllDay ? 'Toute la journée' : format(event.start, 'HH:mm', { locale: fr }); + + const timeUntilStart = event.start.getTime() - now.getTime(); + const minutesUntilStart = Math.round(timeUntilStart / 1000 / 60); const notification: OutlookNotificationData = { id: `calendar-${event.id}-${Date.now()}`, source: 'calendar', title: 'Agenda', - subtitle: event.isAllDay ? 'Événement aujourd\'hui' : 'Événement maintenant', + subtitle: event.isAllDay ? 'Événement dans 3 minutes' : `Événement dans ${minutesUntilStart} min`, message: `${event.title}${event.isAllDay ? '' : ` à ${timeStr}`}${event.calendarName ? ` (${event.calendarName})` : ''}`, icon: Calendar, iconColor: 'text-orange-600',