import * as React from 'react'; import { Box, Drawer, AppBar, Toolbar, List, Typography, Divider, ListItem, ListItemButton, ListItemIcon, ListItemText, CssBaseline, IconButton, Tooltip, } from '@mui/material'; import TableViewIcon from '@mui/icons-material/TableView'; import DashboardIcon from '@mui/icons-material/Dashboard'; import LogoutIcon from '@mui/icons-material/Logout'; import ChevronLeftIcon from '@mui/icons-material/ChevronLeft'; import ChevronRightIcon from '@mui/icons-material/ChevronRight'; import { ResourceConfig } from '../types/config'; import { useLocation, useNavigate } from 'react-router-dom'; const drawerWidth = 240; const collapsedWidth = 64; interface AdminLayoutProps { children: React.ReactNode; onSelectResource: (resourceName: string | null) => void; onLogout: () => void; username?: string; resources: ResourceConfig[]; } export default function AdminLayout({ children, onSelectResource, onLogout, username, resources, }: AdminLayoutProps) { const location = useLocation(); const navigate = useNavigate(); const [isCollapsed, setIsCollapsed] = React.useState(false); const activeResourceName = location.pathname.split('/')[1] || null; // AUTO-TOGGLE LOGIC React.useEffect(() => { if (location.pathname === '/' || location.pathname === '') { setIsCollapsed(false); } else { setIsCollapsed(true); } }, [location.pathname]); const currentWidth = isCollapsed ? collapsedWidth : drawerWidth; const handleToggle = () => { setIsCollapsed(!isCollapsed); }; return ( theme.zIndex.drawer + 1, backdropFilter: 'blur(8px)', backgroundColor: 'rgba(255, 255, 255, 0.8)', // Adjust based on theme in real app color: 'text.primary', boxShadow: 'none', borderBottom: '1px solid', borderColor: 'divider', }} > Admin Panel {username} theme.transitions.create('width', { easing: theme.transitions.easing.sharp, duration: theme.transitions.duration.enteringScreen, }), [`& .MuiDrawer-paper`]: { width: currentWidth, boxSizing: 'border-box', overflowX: 'hidden', transition: (theme) => theme.transitions.create('width', { easing: theme.transitions.easing.sharp, duration: theme.transitions.duration.enteringScreen, }), }, }} > {isCollapsed ? : } navigate('/')} sx={{ minHeight: 48, justifyContent: isCollapsed ? 'center' : 'initial', px: 2.5, }} > {!isCollapsed && } {resources.map((res) => ( onSelectResource(res.name)} sx={{ minHeight: 48, justifyContent: isCollapsed ? 'center' : 'initial', px: 2.5, }} > {!isCollapsed && } ))} theme.transitions.create(['margin', 'width'], { easing: theme.transitions.easing.sharp, duration: theme.transitions.duration.enteringScreen, }), }} > {children} ); }