import { useAuth } from "@/_core/hooks/useAuth"; import { Avatar, AvatarFallback } from "@/components/ui/avatar"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; import { Sidebar, SidebarContent, SidebarFooter, SidebarHeader, SidebarInset, SidebarMenu, SidebarMenuButton, SidebarMenuItem, SidebarProvider, SidebarTrigger, useSidebar, } from "@/components/ui/sidebar"; import { APP_LOGO, APP_TITLE, getLoginUrl } from "@/const"; import { useIsMobile } from "@/hooks/useMobile"; import { LayoutDashboard, LogOut, PanelLeft, Users, GraduationCap, Calendar, CalendarDays, Bell, BarChart3, UserCog, Mail, Settings, TrendingUp, Building2 } from "lucide-react"; import { CSSProperties, useEffect, useRef, useState } from "react"; import { useLocation } from "wouter"; import { DashboardLayoutSkeleton } from './DashboardLayoutSkeleton'; import { Button } from "./ui/button"; const menuSections = [ { title: "Tableau de bord", items: [ { icon: LayoutDashboard, label: "Tableau de bord", path: "/admin" }, ] }, { title: "Gestion", items: [ { icon: GraduationCap, label: "Formations", path: "/admin/formations" }, { icon: Calendar, label: "Séquences", path: "/admin/sequences" }, { icon: CalendarDays, label: "Calendrier", path: "/admin/calendrier" }, { icon: Users, label: "Apprenants", path: "/admin/apprenants" }, ] }, { title: "Configuration", items: [ { icon: UserCog, label: "Utilisateurs", path: "/admin/users" }, { icon: Bell, label: "Rappels", path: "/admin/rappels" }, { icon: Mail, label: "Templates d'emails", path: "/admin/email-templates" }, { icon: Settings, label: "Configuration SMTP", path: "/admin/email-config" }, ] }, { title: "Analyses", items: [ { icon: Building2, label: "Établissements", path: "/admin/etablissements" }, { icon: TrendingUp, label: "Tableau de bord analytique", path: "/admin/analytics" }, { icon: BarChart3, label: "Rapport Public Cible", path: "/admin/rapport-public-cible" }, ] }, ]; const SIDEBAR_WIDTH_KEY = "sidebar-width"; const DEFAULT_WIDTH = 280; const MIN_WIDTH = 200; const MAX_WIDTH = 480; export default function DashboardLayout({ children, }: { children: React.ReactNode; }) { const [sidebarWidth, setSidebarWidth] = useState(() => { const saved = localStorage.getItem(SIDEBAR_WIDTH_KEY); return saved ? parseInt(saved, 10) : DEFAULT_WIDTH; }); const { loading, user } = useAuth(); useEffect(() => { localStorage.setItem(SIDEBAR_WIDTH_KEY, sidebarWidth.toString()); }, [sidebarWidth]); if (loading) { return } if (!user) { return (
{APP_TITLE}

{APP_TITLE}

Veuillez vous connecter pour continuer

); } return ( {children} ); } type DashboardLayoutContentProps = { children: React.ReactNode; setSidebarWidth: (width: number) => void; }; function DashboardLayoutContent({ children, setSidebarWidth, }: DashboardLayoutContentProps) { const { user, logout } = useAuth(); const [location, setLocation] = useLocation(); const { state, toggleSidebar } = useSidebar(); const isCollapsed = state === "collapsed"; const [isResizing, setIsResizing] = useState(false); const sidebarRef = useRef(null); const activeMenuItem = menuSections.flatMap(section => section.items).find(item => item.path === location); const isMobile = useIsMobile(); useEffect(() => { if (isCollapsed) { setIsResizing(false); } }, [isCollapsed]); useEffect(() => { const handleMouseMove = (e: MouseEvent) => { if (!isResizing) return; const sidebarLeft = sidebarRef.current?.getBoundingClientRect().left ?? 0; const newWidth = e.clientX - sidebarLeft; if (newWidth >= MIN_WIDTH && newWidth <= MAX_WIDTH) { setSidebarWidth(newWidth); } }; const handleMouseUp = () => { setIsResizing(false); }; if (isResizing) { document.addEventListener("mousemove", handleMouseMove); document.addEventListener("mouseup", handleMouseUp); document.body.style.cursor = "col-resize"; document.body.style.userSelect = "none"; } return () => { document.removeEventListener("mousemove", handleMouseMove); document.removeEventListener("mouseup", handleMouseUp); document.body.style.cursor = ""; document.body.style.userSelect = ""; }; }, [isResizing, setSidebarWidth]); return ( <>
{isCollapsed ? (
Logo
) : ( <>
Logo {APP_TITLE}
)}
{menuSections.map((section, sectionIndex) => (
{sectionIndex > 0 && (
)}

{section.title}

{section.items.map(item => { const isActive = location === item.path; return ( setLocation(item.path)} tooltip={item.label} className={`h-10 transition-all font-normal`} > {item.label} ); })}
))} { await logout(); setLocation('/'); }} className="cursor-pointer text-destructive focus:text-destructive" > Déconnexion
{ if (isCollapsed) return; setIsResizing(true); }} style={{ zIndex: 50 }} />
{isMobile && (
{activeMenuItem?.label ?? APP_TITLE}
)}
{children}
); }