diff --git a/src/components/Header/Header.tsx b/src/components/Header/Header.tsx index a7188b9..41cba77 100644 --- a/src/components/Header/Header.tsx +++ b/src/components/Header/Header.tsx @@ -25,30 +25,25 @@ function Header() { const notificationRef = useRef(null); useEffect(() => { + if (!user) return; updateEvent(); let echoInstance: ReturnType | null = null; - let channelsToLeave: string[] = []; async function initializeHeader() { try { + const echo = initEcho(); + echoInstance = echo; + adminNotificationsListener(echo, user, setNotifications, setunreadNotification, updatePendingMembers); + userNotificationsListener(echo, user, setNotifications, setunreadNotification); + const notifData = await getUserNotifications(); const data: Notification[] = notifData || []; setNotifications(data); const hasUnread = data.some(n => n.pivot && n.pivot.unread === 1); 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) { console.error("Erreur initialisation Header:", err); } @@ -65,13 +60,12 @@ function Header() { setnotificationMenu(false); } }; - document.addEventListener("mousedown", handleClickOutside); return () => { document.removeEventListener("mousedown", handleClickOutside); if (echoInstance) { - channelsToLeave.forEach(chan => echoInstance!.leave(chan)); + echoInstance.disconnect(); } }; }, []);