'use client'; import { useRouter, usePathname } from 'next/navigation'; import { useState } from 'react'; import Image from 'next/image'; import Link from 'next/link'; import useSWR from 'swr'; interface User { id: string; email: string; name: string | null; roleId?: string | null; } interface DashboardLayoutProps { user: User; children: React.ReactNode; } interface NavItem { label: string; href: string; icon: React.ReactNode; } const fetcher = (url: string) => fetch(url).then((res) => res.json()); export default function DashboardLayout({ user, children }: DashboardLayoutProps) { const router = useRouter(); const pathname = usePathname(); const [loading, setLoading] = useState(false); const [showNotifications, setShowNotifications] = useState(false); const [showProfileMenu, setShowProfileMenu] = useState(false); // Récupérer les conversations pour compter les messages non lus const { data: conversations } = useSWR>( '/api/conversations', fetcher, { refreshInterval: 3000, // Rafraîchir toutes les 3 secondes } ); // Récupérer les notifications const { data: notificationsData, mutate: mutateNotifications } = useSWR<{ notifications: Array<{ id: string; type: string; title: string; message: string; read: boolean; link: string | null; createdAt: string; }>; unreadCount: number; }>( '/api/notifications', fetcher, { refreshInterval: 3000, // Rafraîchir toutes les 3 secondes } ); const notifications = notificationsData?.notifications || []; const unreadNotificationsCount = notificationsData?.unreadCount || 0; // Récupérer les pages accessibles pour l'utilisateur const { data: userPagesData } = useSWR<{ pages: string[] }>( '/api/user/pages', fetcher ); const accessiblePages = userPagesData?.pages || []; // Calculer le nombre total de messages non lus const totalUnreadCount = Array.isArray(conversations) ? conversations.reduce((sum, conv) => sum + (conv.unreadCount || 0), 0) : 0; const getUserInitials = () => { if (user.name) { const names = user.name.split(' '); if (names.length >= 2) { return `${names[0].charAt(0)}${names[1].charAt(0)}`.toUpperCase(); } return user.name.charAt(0).toUpperCase(); } return user.email.charAt(0).toUpperCase(); }; const navItems: NavItem[] = [ { label: 'Tableau de Board', href: '/dashboard', icon: ( ), }, { label: 'Calendrier', href: '/dashboard/calendrier', icon: ( ), }, { label: 'Chauffeurs', href: '/dashboard/chauffeurs', icon: ( ), }, { label: 'Adhérents', href: '/dashboard/adherents', icon: ( ), }, { label: 'Univers Pro', href: '/dashboard/univers-pro', icon: ( ), }, { label: 'Messagerie', href: '/dashboard/messagerie', icon: (
), }, { label: 'Factures', href: '/dashboard/factures', icon: ( ), }, { label: 'Archives', href: '/dashboard/archives', icon: ( ), }, ]; const handleLogout = async () => { setLoading(true); try { await fetch('/api/auth/logout', { method: 'POST' }); router.push('/login'); router.refresh(); } catch (error) { console.error('Logout error:', error); } finally { setLoading(false); } }; return (
{/* Sidebar */} {/* Main Content Area */}
{/* Top Navbar */}

{/* Notification Button */}
{/* Dropdown Notifications */} {showNotifications && (

Notifications

{unreadNotificationsCount > 0 && ( )}
{notifications.length === 0 ? (
Aucune notification
) : (
{notifications.map((notification) => ( ))}
)}
)}
{/* Profile Avatar */}
{/* Dropdown Profile Menu */} {showProfileMenu && (
{user.name || 'Utilisateur'}
{user.email}
)}
{/* Page Content */}
{children}
{/* Overlay pour fermer les menus */} {(showNotifications || showProfileMenu) && (
{ setShowNotifications(false); setShowProfileMenu(false); }} /> )}
); }