import ExpandMore from "@mui/icons-material/ExpandMore"; import { useEffect, useRef, useState } from "react"; export function Sidebar({ activePath, isCollapsed, items, onNavigate }) { const [expandedGroupId, setExpandedGroupId] = useState(() => findParentGroupId(activePath, items)); const [flyoutGroupId, setFlyoutGroupId] = useState(null); const sidebarRef = useRef(null); useEffect(() => { setExpandedGroupId(findParentGroupId(activePath, items)); setFlyoutGroupId(null); }, [activePath, items]); useEffect(() => { setFlyoutGroupId(null); }, [isCollapsed]); useEffect(() => { if (!flyoutGroupId) { return undefined; } const closeFlyout = (event) => { if (sidebarRef.current?.contains(event.target)) { return; } setFlyoutGroupId(null); }; const closeOnEscape = (event) => { if (event.key === "Escape") { setFlyoutGroupId(null); } }; document.addEventListener("pointerdown", closeFlyout); document.addEventListener("keydown", closeOnEscape); return () => { document.removeEventListener("pointerdown", closeFlyout); document.removeEventListener("keydown", closeOnEscape); }; }, [flyoutGroupId]); const handleNavigate = (path) => { if (path) { onNavigate(path); } setFlyoutGroupId(null); }; return ( ); } function NavGroup({ activePath, isCollapsed, isExpanded, isFlyoutOpen, item, onNavigate, onToggleExpanded, onToggleFlyout }) { const Icon = item.icon; const children = item.children || []; const hasChildren = children.length > 0; const isActive = Boolean(item.path && activePath.startsWith(item.path)); const isChildActive = children.some((child) => child.path && activePath.startsWith(child.path)); const isOpen = isCollapsed ? isFlyoutOpen : isExpanded; const handleParentClick = () => { if (hasChildren && isCollapsed) { onToggleFlyout(); return; } if (hasChildren) { onToggleExpanded(); return; } onNavigate(item.path); }; return (