import React, { useState, useEffect } from "react"; import { Link, useLocation } from "wouter"; import { Button } from "@/components/ui/button"; import { Avatar, AvatarFallback } from "@/components/ui/avatar"; import { Separator } from "@/components/ui/separator"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuLabel, DropdownMenuSeparator, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; import { ChevronDown, LogOut, Menu, Bell, HelpCircle, User, Settings, Home, Users, UserPlus, FolderClosed, Monitor, Globe, Key, Server, ShieldAlert, Filter, } from "lucide-react"; import { useMobile } from "@/hooks/use-mobile"; import { useToast } from "@/hooks/use-toast"; import { ThemeToggle } from "@/components/theme-toggle"; type MenuItem = { title: string; path: string; icon: React.ReactNode; }; type MenuSection = { title: string; items: MenuItem[]; }; type UserType = { id: number; username: string; fullName?: string; email?: string; role?: string; }; const menuSections: MenuSection[] = [ { title: "Active Directory", items: [ { title: "Users", path: "/users", icon: }, { title: "Groups", path: "/groups", icon: }, { title: "Organizational Units", path: "/organizational-units", icon: }, { title: "Computers", path: "/computers", icon: }, { title: "Domains", path: "/domains", icon: }, ], }, { title: "Administration", items: [ { title: "API Tokens", path: "/api-tokens", icon: }, { title: "LDAP Connections", path: "/ldap-connections", icon: }, { title: "LDAP Query Builder", path: "/ldap-query-builder", icon: }, { title: "Settings", path: "/settings", icon: }, { title: "User Management", path: "/user-management", icon: }, ], }, ]; interface DashboardLayoutProps { children: React.ReactNode; title: string; description?: string; } export function DashboardLayout({ children, title, description }: DashboardLayoutProps) { const [location, navigate] = useLocation(); const isMobile = useMobile(); const [sidebarOpen, setSidebarOpen] = useState(!isMobile); const [user, setUser] = useState(null); const { toast } = useToast(); // Fetch user data on component mount useEffect(() => { const fetchUserData = async () => { try { const response = await fetch('/api/user', { credentials: 'include' }); if (response.ok) { const userData = await response.json(); setUser(userData); } } catch (error) { console.error('Error fetching user data:', error); } }; fetchUserData(); }, []); const handleLogout = async () => { try { const response = await fetch('/api/logout', { method: 'POST', credentials: 'include' }); if (response.ok) { toast({ title: "Logged out", description: "You have been successfully logged out.", }); navigate('/auth'); } else { toast({ title: "Logout failed", description: "An error occurred during logout.", variant: "destructive", }); } } catch (error) { console.error('Error during logout:', error); toast({ title: "Logout failed", description: "An error occurred during logout.", variant: "destructive", }); } }; // Get user initials for avatar const getInitials = () => { if (!user) return "U"; if (user.fullName) { const nameParts = user.fullName.split(" "); if (nameParts.length > 1) { return `${nameParts[0][0]}${nameParts[nameParts.length - 1][0]}`.toUpperCase(); } return nameParts[0][0].toUpperCase(); } return user.username[0].toUpperCase(); }; return ( {/* Sidebar */} AD Management API Dashboard {menuSections.map((section, idx) => ( {section.title} {section.items.map((item, itemIdx) => ( {item.icon} {item.title} ))} ))} {/* Main Content */} {/* Top App Bar */} setSidebarOpen(!sidebarOpen)} className="lg:hidden" > {getInitials()} {user?.username} My Account Profile Settings Log out {/* Main Content Area */} {title} {description && {description}} {children} ); }
{description}