import { Link, NavLink } from "react-router"; import { useContext, useEffect, useRef, useState } from "react"; import styles from "./Header.module.css" import getUserNotifications from "../../utils/notifications/getUserNotifications.js"; import deleteNotificationUser from "../../utils/notifications/deleteNotificationUser.js"; import initEcho from "../../utils/echo/initEcho.js" import readNotifications from "../../utils/notifications/readNotifications.js"; import { adminNotificationsListener } from "../../utils/echo/listeners/adminNotificationsListener"; import { userNotificationsListener } from "../../utils/echo/listeners/userNotificationsListener.js"; import NotificationCard from "../NotificationCard/NotificationCard"; import { EventContext } from "../../contexts/Events/EventContext.js"; import { AuthContext } from "../../contexts/Auth/AuthContext.js"; import { PendingMembersContext } from "../../contexts/PendingMembers/PendingMembersContext"; import Notification from "../../interfaces/notification.interface"; function Header() { const { update, user } = useContext(AuthContext)!; const { updateEvent } = useContext(EventContext)!; const { updatePendingMembers } = useContext(PendingMembersContext) as unknown as { updatePendingMembers: () => void }; const [notificationMenu, setnotificationMenu] = useState(false); const [unreadNotification, setunreadNotification] = useState(false); const [mobileMenu, setMobileMenu] = useState(false); const [notifications, setNotifications] = useState([]); const notificationRef = useRef(null); useEffect(() => { if (!user) return; updateEvent(); let echoInstance: ReturnType | null = null; 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); } catch (err) { console.error("Erreur initialisation Header:", err); } } initializeHeader(); const handleClickOutside = (event: MouseEvent) => { if ( notificationRef.current && !notificationRef.current.contains(event.target as Node) && !(event.target as Element).closest(`.${styles.bellBtn}`) ) { setnotificationMenu(false); } }; document.addEventListener("mousedown", handleClickOutside); return () => { document.removeEventListener("mousedown", handleClickOutside); if (echoInstance) { echoInstance.disconnect(); } }; }, []); const toggleNotificationMenu = async () => { try { setnotificationMenu(!notificationMenu); if (unreadNotification) { const result = await readNotifications(); if (result) { setunreadNotification(false); update(); } } } catch (err) { console.error('Erreur pour ouvrir/fermer le menu de notifications :', err); } } async function deleteNotification(notificationId: number) { try { const result = await deleteNotificationUser(notificationId); if (result) { setNotifications(prev => prev.filter(n => n.id !== notificationId)); update(); } } catch (err) { console.error('Erreur suppression notification :', err); } } return (
{notifications.length === 0 ? (

Vous n'avez aucune notification !

) : ( )}
); } export default Header;