refactor Notifications agenda

This commit is contained in:
alma 2026-01-16 02:44:01 +01:00
parent 66503008ca
commit f23fd45c4c
2 changed files with 68 additions and 21 deletions

View File

@ -144,17 +144,28 @@ export function CalendarWidget() {
setEvents(upcomingEvents.slice(0, 5)); // Keep only 5 for display setEvents(upcomingEvents.slice(0, 5)); // Keep only 5 for display
// Dispatch event for Outlook-style notifications (when events start) // Dispatch event for Outlook-style notifications (when events start)
const eventsForNotification = upcomingEvents.map(evt => ({
id: evt.id,
title: evt.title,
start: evt.start,
end: evt.end,
isAllDay: evt.isAllDay,
calendarName: evt.calendarName,
calendarColor: evt.calendarColor,
}));
console.log('[Calendar Widget] 📅 Dispatching calendar events update', {
eventsCount: eventsForNotification.length,
events: eventsForNotification.map(e => ({
id: e.id,
title: e.title,
start: e.start instanceof Date ? e.start.toISOString() : e.start,
})),
});
window.dispatchEvent(new CustomEvent('calendar-events-updated', { window.dispatchEvent(new CustomEvent('calendar-events-updated', {
detail: { detail: {
events: upcomingEvents.map(evt => ({ events: eventsForNotification,
id: evt.id,
title: evt.title,
start: evt.start,
end: evt.end,
isAllDay: evt.isAllDay,
calendarName: evt.calendarName,
calendarColor: evt.calendarColor,
})),
} }
})); }));

View File

@ -29,6 +29,16 @@ export function useCalendarEventNotifications() {
const handleEventsUpdate = (event: CustomEvent) => { const handleEventsUpdate = (event: CustomEvent) => {
const events = event.detail?.events || []; const events = event.detail?.events || [];
console.log('[useCalendarEventNotifications] 📅 Received calendar events update', {
eventsCount: events.length,
events: events.map((e: any) => ({
id: e.id,
title: e.title,
start: e.start,
isAllDay: e.isAllDay,
})),
});
if (!events || events.length === 0) { if (!events || events.length === 0) {
eventsRef.current = []; eventsRef.current = [];
return; return;
@ -46,8 +56,13 @@ export function useCalendarEventNotifications() {
})); }));
eventsRef.current = calendarEvents; eventsRef.current = calendarEvents;
console.log('[useCalendarEventNotifications] Events updated', { console.log('[useCalendarEventNotifications] Events stored', {
count: calendarEvents.length, count: calendarEvents.length,
events: calendarEvents.map(e => ({
id: e.id,
title: e.title,
start: e.start.toISOString(),
})),
}); });
}; };
@ -58,23 +73,43 @@ export function useCalendarEventNotifications() {
const now = new Date(); const now = new Date();
const currentTime = now.getTime(); const currentTime = now.getTime();
// Check events that start within the next 2 minutes (to catch events that just started) console.log('[useCalendarEventNotifications] 🔍 Checking for starting events', {
const upcomingWindow = 2 * 60 * 1000; // 2 minutes in milliseconds now: now.toISOString(),
eventsCount: eventsRef.current.length,
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
const startingEvents = eventsRef.current.filter((event) => { const startingEvents = eventsRef.current.filter((event) => {
// Skip if already notified // Skip if already notified
if (notifiedEventIdsRef.current.has(event.id)) { if (notifiedEventIdsRef.current.has(event.id)) {
console.log('[useCalendarEventNotifications] ⏭️ Event already notified', {
id: event.id,
title: event.title,
});
return false; return false;
} }
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);
// Event is starting now or within the next 2 minutes console.log('[useCalendarEventNotifications] ⏰ Checking event', {
// And hasn't started more than 5 minutes ago (to avoid old notifications) id: event.id,
title: event.title,
start: event.start.toISOString(),
now: now.toISOString(),
timeUntilStartMinutes,
inWindow: timeUntilStart >= -2 * 60 * 1000 && timeUntilStart <= upcomingWindow,
});
// 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 ( return (
timeUntilStart >= -5 * 60 * 1000 && // Not more than 5 minutes ago timeUntilStart >= -2 * 60 * 1000 && // Not more than 2 minutes ago (just started)
timeUntilStart <= upcomingWindow // Within next 2 minutes timeUntilStart <= upcomingWindow // Within next 5 minutes
); );
}); });
@ -85,10 +120,11 @@ export function useCalendarEventNotifications() {
// Show notification for the first event starting // Show notification for the first event starting
const event = startingEvents[0]; const event = startingEvents[0];
console.log('[useCalendarEventNotifications] 📅 Event starting detected', { console.log('[useCalendarEventNotifications] 📅 Event starting detected!', {
title: event.title, title: event.title,
start: event.start, start: event.start.toISOString(),
now: now, now: now.toISOString(),
timeUntilStart: Math.round((event.start.getTime() - currentTime) / 1000 / 60),
}); });
const timeStr = event.isAllDay const timeStr = event.isAllDay
@ -130,9 +166,9 @@ export function useCalendarEventNotifications() {
} }
}; };
// Check immediately and then every minute // Check immediately and then every 10 seconds for more responsive notifications
checkForStartingEvents(); checkForStartingEvents();
checkIntervalRef.current = setInterval(checkForStartingEvents, 60000); // Every minute checkIntervalRef.current = setInterval(checkForStartingEvents, 10000); // Every 10 seconds
return () => { return () => {
window.removeEventListener('calendar-events-updated', handleEventsUpdate as EventListener); window.removeEventListener('calendar-events-updated', handleEventsUpdate as EventListener);