import { useState } from 'react'; import { useLocation, useNavigate } from 'react-router-dom'; import { Drawer, Box, List, ListItemButton, ListItemIcon, ListItemText, Typography, Collapse, Tooltip, Toolbar } from '@mui/material'; import ExpandLess from '@mui/icons-material/ExpandLess'; import ExpandMore from '@mui/icons-material/ExpandMore'; import FiberManualRecordIcon from '@mui/icons-material/FiberManualRecord'; import navItems from '@/menu/navItems'; import Logo from '@/components/Logo'; export const DRAWER_WIDTH = 264; export const MINI_WIDTH = 78; const RED = '#C01227'; function NavLeaf({ item, open, active, depth = 0, onClick }) { const Icon = item.icon; const button = ( {depth > 0 && !Icon ? : Icon ? : null} {open && ( )} ); return open ? button : {button}; } export default function Sidebar({ open, mobileOpen, onMobileClose, isMobile }) { const location = useLocation(); const navigate = useNavigate(); const isActive = (url) => url && location.pathname.startsWith(url); const expanded = open || isMobile; const initialOpen = navItems .flatMap((g) => g.items) .filter((i) => i.children && i.children.some((c) => isActive(c.url))) .map((i) => i.id); const [collapse, setCollapse] = useState(initialOpen); const go = (url) => { navigate(url); if (isMobile) onMobileClose(); }; const content = ( {navItems.map((grp) => ( {expanded && ( {grp.group} )} {grp.items.map((item) => { if (item.children) { const opened = collapse.includes(item.id); const childActive = item.children.some((c) => isActive(c.url)); const Icon = item.icon; const head = ( expanded ? setCollapse((p) => (p.includes(item.id) ? p.filter((x) => x !== item.id) : [...p, item.id])) : go(item.children[0].url) } sx={{ minHeight: 44, my: 0.25, mx: expanded ? 1 : 0.75, px: expanded ? 1.5 : 0, justifyContent: expanded ? 'flex-start' : 'center', borderRadius: 2, color: childActive ? '#fff' : 'rgba(255,255,255,0.78)', bgcolor: childActive && !opened ? 'rgba(255,255,255,0.12)' : 'transparent', '&:hover': { bgcolor: 'rgba(255,255,255,0.12)', color: '#fff' } }} > {expanded && ( <> {opened ? : } )} ); return ( {expanded ? head : {head}} {expanded && ( {item.children.map((c) => ( go(c.url)} /> ))} )} ); } return ( go(item.url)} /> ); })} ))} {expanded && ( Doormile CRM v1.0 )} ); if (isMobile) { return ( {content} ); } return ( t.transitions.create('width', { duration: t.transitions.duration.standard }) } }} open={open} > {content} ); }