import React from "react"; import { Button, Menu, MenuItem, ListItemText, ListItemIcon, Divider, } from "@mui/material"; import { useSelector } from "react-redux"; import { useRouter } from "next/navigation"; import KeyboardArrowRightIcon from "@mui/icons-material/KeyboardArrowRight"; import KeyboardArrowDownIcon from "@mui/icons-material/KeyboardArrowDown"; import { selectNavigationSidebar } from "@/app/redux/metadata/selectors"; import { SidebarLink } from "@/app/redux/metadata/metadataSlice"; import { resolveIcon } from "@/app/utils/iconMap"; import "./DropDown.scss"; interface Props { onChange?: (path: string) => void; } export default function SidebarDropdown({ onChange }: Props) { const [anchorEl, setAnchorEl] = React.useState(null); const [openMenus, setOpenMenus] = React.useState>({}); const sidebar = useSelector(selectNavigationSidebar); const router = useRouter(); const open = Boolean(anchorEl); const handleClick = (event: React.MouseEvent) => { setAnchorEl(event.currentTarget); }; const handleClose = () => { setAnchorEl(null); setOpenMenus({}); }; const toggleMenu = (title: string) => { setOpenMenus(prev => ({ ...prev, [title]: !prev[title] })); }; const handleNavigation = (path: string) => { router.push(path); onChange?.(path); handleClose(); }; const renderMenuItem = ( link: SidebarLink, level: number = 0 ): React.ReactNode => { const Icon = link.icon ? resolveIcon(link.icon as string) : undefined; const hasChildren = link.children && link.children.length > 0; const isOpen = openMenus[link.title]; const indent = level * 24; if (hasChildren) { return ( toggleMenu(link.title)} sx={{ pl: `${8 + indent}px`, }} > {Icon && ( )} {isOpen ? ( ) : ( )} {isOpen && link.children?.map(child => renderMenuItem(child, level + 1))} ); } return ( handleNavigation(link.path)} sx={{ pl: `${8 + indent}px`, }} > {Icon && ( )} ); }; return (
{sidebar?.map(link => renderMenuItem(link))}
); }