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 { userCreatedListener } from "../../utils/echo/listeners/userCreatedListener"; import { userNotificationsListener } from "../../utils/echo/listeners/userNotificationsListener.js"; import NotificationCard from "../NotificationCard/NotificationCard.jsx"; import { EventContext } from "../../contexts/Events/EventContext.js"; import {AuthContext} from "../../contexts/Auth/AuthContext.js"; import { PendingMembersContext } from "../../contexts/PendingMembers/PendingMembersContext"; function Header() { const { update, user } = useContext(AuthContext); const { updateEvent } = useContext(EventContext); const [notificationMenu, setnotificationMenu] = useState(false); const [unreadNotification, setunreadNotification] = useState(false); const [mobileMenu, setMobileMenu] = useState(false); const [notifications, setNotifications] = useState([]); const notificationRef = useRef(null); const { updatePendingMembers } = useContext(PendingMembersContext); useEffect(() => { updateEvent(); let echoInstance = null; let channelsToLeave = []; async function initializeHeader() { try { const notifData = await getUserNotifications(); const data = notifData || []; setNotifications(data); const hasUnread = data.some(n => n.pivot && n.pivot.unread === 1); setunreadNotification(hasUnread); const echo = initEcho(); echoInstance = echo; const activeChannels = [ userCreatedListener(echo, user, setNotifications, setunreadNotification, updatePendingMembers), userNotificationsListener(echo, user, setNotifications, setunreadNotification), ]; channelsToLeave = activeChannels.filter(name => name !== null); } catch (err) { console.error("Erreur initialisation Header:", err); } } initializeHeader(); const handleClickOutside = (event) => { if ( notificationRef.current && !notificationRef.current.contains(event.target) && !event.target.closest(`.${styles.bellBtn}`) ) { setnotificationMenu(false); } }; document.addEventListener("mousedown", handleClickOutside); return () => { document.removeEventListener("mousedown", handleClickOutside); if (echoInstance) { channelsToLeave.forEach(chan => echoInstance.leave(chan)); } }; }, []); 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) { 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 (
Vous n'avez aucune notification !
) : (