refactor Notifications agenda

This commit is contained in:
alma 2026-01-16 02:59:02 +01:00
parent d55cc836ed
commit ab6846b3f7

View File

@ -70,7 +70,7 @@ export function useCalendarEventNotifications() {
window.addEventListener('calendar-events-updated', handleEventsUpdate as EventListener); 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 checkForStartingEvents = () => {
const now = new Date(); const now = new Date();
const currentTime = now.getTime(); const currentTime = now.getTime();
@ -81,8 +81,12 @@ export function useCalendarEventNotifications() {
notifiedCount: notifiedEventIdsRef.current.size, notifiedCount: notifiedEventIdsRef.current.size,
}); });
// Check events that start within the next 5 minutes (to catch events that just started or are about to start) // Show notification 3 minutes before event starts
const upcomingWindow = 5 * 60 * 1000; // 5 minutes in milliseconds // 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) => { const startingEvents = eventsRef.current.filter((event) => {
// Skip if already notified // Skip if already notified
@ -97,6 +101,10 @@ export function useCalendarEventNotifications() {
const eventStartTime = event.start.getTime(); const eventStartTime = event.start.getTime();
const timeUntilStart = eventStartTime - currentTime; const timeUntilStart = eventStartTime - currentTime;
const timeUntilStartMinutes = Math.round(timeUntilStart / 1000 / 60); 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', { console.log('[useCalendarEventNotifications] ⏰ Checking event', {
id: event.id, id: event.id,
@ -104,15 +112,14 @@ export function useCalendarEventNotifications() {
start: event.start.toISOString(), start: event.start.toISOString(),
now: now.toISOString(), now: now.toISOString(),
timeUntilStartMinutes, 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 // Event should be notified 3 minutes before start (with 1-minute window for detection)
// This allows catching events that just started or are about to start return inNotificationWindow;
return (
timeUntilStart >= -2 * 60 * 1000 && // Not more than 2 minutes ago (just started)
timeUntilStart <= upcomingWindow // Within next 5 minutes
);
}); });
if (startingEvents.length > 0) { if (startingEvents.length > 0) {
@ -132,12 +139,15 @@ export function useCalendarEventNotifications() {
const timeStr = event.isAllDay const timeStr = event.isAllDay
? 'Toute la journée' ? 'Toute la journée'
: format(event.start, 'HH:mm', { locale: fr }); : format(event.start, 'HH:mm', { locale: fr });
const timeUntilStart = event.start.getTime() - now.getTime();
const minutesUntilStart = Math.round(timeUntilStart / 1000 / 60);
const notification: OutlookNotificationData = { const notification: OutlookNotificationData = {
id: `calendar-${event.id}-${Date.now()}`, id: `calendar-${event.id}-${Date.now()}`,
source: 'calendar', source: 'calendar',
title: 'Agenda', 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})` : ''}`, message: `${event.title}${event.isAllDay ? '' : ` à ${timeStr}`}${event.calendarName ? ` (${event.calendarName})` : ''}`,
icon: Calendar, icon: Calendar,
iconColor: 'text-orange-600', iconColor: 'text-orange-600',