This commit is contained in:
alma 2025-05-04 22:33:10 +02:00
parent 772712e405
commit f7905e185d

View File

@ -40,17 +40,24 @@ export function useUnreadAnnouncements() {
// Mark an announcement as read // Mark an announcement as read
const markAsRead = (announcementId: string) => { const markAsRead = (announcementId: string) => {
setReadAnnouncements((prev: AnnouncementRead) => ({ setReadAnnouncements((prev: AnnouncementRead) => {
...prev, const newState = {
[announcementId]: true ...prev,
})); [announcementId]: true
};
// Check if there are still any unread announcements
const hasUnreadAnnouncements = announcements.some(announcement => { // Check if there are still any unread announcements using the updated state
return !readAnnouncements[announcement.id] && announcement.id !== announcementId; const hasUnreadAnnouncements = announcements.some(announcement => {
return !newState[announcement.id];
});
// Update the hasUnread state in the next tick to avoid the infinite loop
setTimeout(() => {
setHasUnread(hasUnreadAnnouncements);
}, 0);
return newState;
}); });
setHasUnread(hasUnreadAnnouncements);
}; };
// Mark all announcements as read // Mark all announcements as read
@ -61,12 +68,22 @@ export function useUnreadAnnouncements() {
}); });
setReadAnnouncements(allRead); setReadAnnouncements(allRead);
setHasUnread(false);
// Update the hasUnread state in the next tick to avoid the infinite loop
setTimeout(() => {
setHasUnread(false);
}, 0);
}; };
// Check for unread announcements on mount // Check for unread announcements on mount
useEffect(() => { useEffect(() => {
checkForUnreadAnnouncements(); // Run the check in the next tick to avoid unnecessary re-renders
const timer = setTimeout(() => {
checkForUnreadAnnouncements();
}, 0);
// Cleanup timer on component unmount
return () => clearTimeout(timer);
}, []); }, []);
return { return {