import { Link, useLocation } from "wouter"; import { useAuthSimple } from "@/hooks/useAuthSimple"; import { Button } from "@/components/ui/button"; import { useStore } from "./Layout"; import { Store, Calendar, BarChart3, Package, Truck, Building, Users, UserCog, LogOut, FileText, Megaphone, Database, ShoppingCart, Clock, ListTodo, ChevronLeft, ChevronRight } from "lucide-react"; export default function Sidebar() { const { user, isLoading, error } = useAuthSimple(); const [location] = useLocation(); const { sidebarCollapsed, setSidebarCollapsed } = useStore(); // Debug logging pour production console.log('Sidebar - User:', user); console.log('Sidebar - isLoading:', isLoading); console.log('Sidebar - error:', error); const handleLogout = async () => { try { // Force logout via fetch to ensure session is destroyed await fetch('/api/logout', { method: 'POST', credentials: 'include' }); } catch (error) { console.error('Logout error:', error); } finally { // Force redirect to auth page regardless of API response window.location.href = "/auth"; } }; const toggleSidebar = () => { const newCollapsed = !sidebarCollapsed; setSidebarCollapsed(newCollapsed); localStorage.setItem('sidebarCollapsed', JSON.stringify(newCollapsed)); }; const getInitials = (firstName?: string, lastName?: string) => { if (!firstName && !lastName) return "U"; return `${firstName?.[0] || ""}${lastName?.[0] || ""}`.toUpperCase(); }; const isActive = (path: string) => { if (path === "/" && location === "/") return true; if (path !== "/" && location.startsWith(path)) return true; return false; }; const menuItems = [ { path: "/dashboard", label: "Tableau de bord", icon: BarChart3, roles: ["admin", "manager", "employee"] }, { path: "/calendar", label: "Calendrier", icon: Calendar, roles: ["admin", "manager", "employee"] }, { path: "/orders", label: "Commandes", icon: Package, roles: ["admin", "manager"] }, { path: "/deliveries", label: "Livraisons", icon: Truck, roles: ["admin", "manager"] }, { path: "/bl-reconciliation", label: "Rapprochement", icon: FileText, roles: ["admin", "manager"] }, { path: "/publicities", label: "Publicités", icon: Megaphone, roles: ["admin", "manager"] }, { path: "/customer-orders", label: "Commandes Client", icon: ShoppingCart, roles: ["admin", "manager", "employee"] }, { path: "/dlc", label: "Gestion DLC", icon: Clock, roles: ["admin", "manager", "employee"] }, { path: "/tasks", label: "Tâches", icon: ListTodo, roles: ["admin", "manager", "employee"] }, ]; const managementItems = [ { path: "/suppliers", label: "Fournisseurs", icon: Building, roles: ["admin", "manager"] }, { path: "/groups", label: "Magasins", icon: Users, roles: ["admin", "manager"] }, ]; const adminItems = [ { path: "/users", label: "Utilisateurs", icon: UserCog, roles: ["admin"] }, { path: "/nocodb-config", label: "Configuration NocoDB", icon: Database, roles: ["admin"] }, { path: "/database-debug", label: "Debug Base de Données", icon: Database, roles: ["admin"] }, ]; const hasPermission = (roles: string[]) => { const hasRole = user?.role && roles.includes(user.role); // Debug uniquement en développement pour éviter spam console if (import.meta.env.MODE === 'development') { console.log('hasPermission check:', { userRole: user?.role, roles, hasRole }); } return hasRole; }; // Si l'utilisateur n'est pas encore chargé, afficher un état de chargement if (isLoading) { return ( ); } // Si l'utilisateur n'est pas authentifié, afficher seulement le logo if (!user) { return ( ); } return ( ); }