Pages corrections pages health
This commit is contained in:
parent
93f9e04c1f
commit
e23965b0fd
@ -24,22 +24,6 @@ export function useTaskNotifications() {
|
|||||||
const tasksRef = useRef<Task[]>([]);
|
const tasksRef = useRef<Task[]>([]);
|
||||||
const checkIntervalRef = useRef<NodeJS.Timeout | null>(null);
|
const checkIntervalRef = useRef<NodeJS.Timeout | null>(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
|
// Save notified task IDs to localStorage whenever it changes
|
||||||
const saveNotifiedTaskIds = useCallback(() => {
|
const saveNotifiedTaskIds = useCallback(() => {
|
||||||
try {
|
try {
|
||||||
@ -56,6 +40,21 @@ export function useTaskNotifications() {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
console.log('[useTaskNotifications] 🎧 Hook initialized, listening for tasks-updated');
|
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
|
// Listen for tasks updates
|
||||||
const handleTasksUpdate = (event: CustomEvent) => {
|
const handleTasksUpdate = (event: CustomEvent) => {
|
||||||
const tasks = event.detail?.tasks || [];
|
const tasks = event.detail?.tasks || [];
|
||||||
@ -193,11 +192,12 @@ export function useTaskNotifications() {
|
|||||||
const dueTasksWithDates: TaskWithParsedDate[] = [];
|
const dueTasksWithDates: TaskWithParsedDate[] = [];
|
||||||
|
|
||||||
for (const task of tasksRef.current) {
|
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)) {
|
if (notifiedTaskIdsRef.current.has(task.id)) {
|
||||||
console.log('[useTaskNotifications] ⏭️ Task already notified', {
|
console.log('[useTaskNotifications] ⏭️ Task already notified (skipping)', {
|
||||||
id: task.id,
|
id: task.id,
|
||||||
title: task.headline,
|
title: task.headline,
|
||||||
|
notifiedIds: Array.from(notifiedTaskIdsRef.current),
|
||||||
});
|
});
|
||||||
continue;
|
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);
|
setTaskNotification(notification);
|
||||||
notifiedTaskIdsRef.current.add(task.id);
|
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
|
// Clean up old notified tasks (older than 1 hour) to allow re-notification if needed
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
notifiedTaskIdsRef.current.delete(task.id);
|
notifiedTaskIdsRef.current.delete(task.id);
|
||||||
saveNotifiedTaskIds(); // Update localStorage after cleanup
|
saveNotifiedTaskIds(); // Update localStorage after cleanup
|
||||||
|
console.log('[useTaskNotifications] 🧹 Cleaned up old notification for task', {
|
||||||
|
taskId: task.id,
|
||||||
|
});
|
||||||
}, 60 * 60 * 1000); // 1 hour
|
}, 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
|
// Check immediately and then every 10 seconds for more responsive notifications
|
||||||
checkForDueTasks();
|
checkForDueTasks();
|
||||||
checkIntervalRef.current = setInterval(checkForDueTasks, 10000); // Every 10 seconds
|
checkIntervalRef.current = setInterval(checkForDueTasks, 10000); // Every 10 seconds
|
||||||
|
|
||||||
|
// Clean up localStorage every 5 minutes
|
||||||
|
const cleanupInterval = setInterval(cleanupLocalStorage, 5 * 60 * 1000);
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
window.removeEventListener('tasks-updated', handleTasksUpdate as EventListener);
|
window.removeEventListener('tasks-updated', handleTasksUpdate as EventListener);
|
||||||
if (checkIntervalRef.current) {
|
if (checkIntervalRef.current) {
|
||||||
clearInterval(checkIntervalRef.current);
|
clearInterval(checkIntervalRef.current);
|
||||||
}
|
}
|
||||||
|
if (cleanupInterval) {
|
||||||
|
clearInterval(cleanupInterval);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}, []);
|
}, [saveNotifiedTaskIds]);
|
||||||
|
|
||||||
const handleDismiss = () => {
|
const handleDismiss = () => {
|
||||||
setTaskNotification(null);
|
setTaskNotification(null);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user