prevent double broadcast notification

This commit is contained in:
2026-03-19 17:12:38 +01:00
parent 72d2cfae27
commit 4d4df673d0
+7 -13
View File
@@ -25,30 +25,25 @@ function Header() {
const notificationRef = useRef<HTMLDivElement>(null); const notificationRef = useRef<HTMLDivElement>(null);
useEffect(() => { useEffect(() => {
if (!user) return;
updateEvent(); updateEvent();
let echoInstance: ReturnType<typeof initEcho> | null = null; let echoInstance: ReturnType<typeof initEcho> | null = null;
let channelsToLeave: string[] = [];
async function initializeHeader() { async function initializeHeader() {
try { try {
const echo = initEcho();
echoInstance = echo;
adminNotificationsListener(echo, user, setNotifications, setunreadNotification, updatePendingMembers);
userNotificationsListener(echo, user, setNotifications, setunreadNotification);
const notifData = await getUserNotifications(); const notifData = await getUserNotifications();
const data: Notification[] = notifData || []; const data: Notification[] = notifData || [];
setNotifications(data); setNotifications(data);
const hasUnread = data.some(n => n.pivot && n.pivot.unread === 1); const hasUnread = data.some(n => n.pivot && n.pivot.unread === 1);
setunreadNotification(hasUnread); setunreadNotification(hasUnread);
const echo = initEcho();
echoInstance = echo;
const activeChannels: (string | null)[] = [
adminNotificationsListener(echo, user, setNotifications, setunreadNotification, updatePendingMembers),
userNotificationsListener(echo, user, setNotifications, setunreadNotification),
];
channelsToLeave = activeChannels.filter((name): name is string => name !== null);
} catch (err) { } catch (err) {
console.error("Erreur initialisation Header:", err); console.error("Erreur initialisation Header:", err);
} }
@@ -65,13 +60,12 @@ function Header() {
setnotificationMenu(false); setnotificationMenu(false);
} }
}; };
document.addEventListener("mousedown", handleClickOutside); document.addEventListener("mousedown", handleClickOutside);
return () => { return () => {
document.removeEventListener("mousedown", handleClickOutside); document.removeEventListener("mousedown", handleClickOutside);
if (echoInstance) { if (echoInstance) {
channelsToLeave.forEach(chan => echoInstance!.leave(chan)); echoInstance.disconnect();
} }
}; };
}, []); }, []);