import PropTypes from 'prop-types'; import { Activity, useEffect, useState } from 'react'; import { matchPath, useLocation } from 'react-router-dom'; // material-ui import Divider from '@mui/material/Divider'; import List from '@mui/material/List'; import Typography from '@mui/material/Typography'; // project imports import NavCollapse from '../NavCollapse'; import NavItem from '../NavItem'; import { useGetMenuMaster } from 'api/menu'; // ==============================|| SIDEBAR MENU LIST GROUP ||============================== // export default function NavGroup({ item, lastItem, remItems, lastItemId, setSelectedID }) { const { pathname } = useLocation(); const { menuMaster } = useGetMenuMaster(); const drawerOpen = menuMaster.isDashboardDrawerOpened; const [anchorEl, setAnchorEl] = useState(null); const [currentItem, setCurrentItem] = useState(item); const openMini = Boolean(anchorEl); useEffect(() => { if (lastItem) { if (item.id === lastItemId) { const localItem = { ...item }; const elements = remItems.map((ele) => ele.elements); localItem.children = elements.flat(1); setCurrentItem(localItem); } else { setCurrentItem(item); } } }, [item, lastItem, remItems, lastItemId]); const checkOpenForParent = (child, id) => { child.forEach((ele) => { if (ele.children?.length) { checkOpenForParent(ele.children, currentItem.id); } if (ele?.url && !!matchPath({ path: ele?.link ? ele.link : ele.url, end: true }, pathname)) { setSelectedID(id); } }); }; const checkSelectedOnload = (data) => { const childrens = data.children ? data.children : []; childrens.forEach((itemCheck) => { if (itemCheck?.children?.length) { checkOpenForParent(itemCheck.children, currentItem.id); } if (itemCheck?.url && !!matchPath({ path: itemCheck?.link ? itemCheck.link : itemCheck.url, end: true }, pathname)) { setSelectedID(currentItem.id); } }); if (data?.url && !!matchPath({ path: data?.link ? data.link : data.url, end: true }, pathname)) { setSelectedID(currentItem.id); } }; // keep selected-menu on page load and use for horizontal menu close on change routes useEffect(() => { checkSelectedOnload(currentItem); if (openMini) setAnchorEl(null); // eslint-disable-next-line react-hooks/exhaustive-deps }, [pathname, currentItem]); // menu list collapse & items const items = currentItem.children?.map((menu) => { switch (menu?.type) { case 'collapse': return ; case 'item': return ; default: return ( Menu Items Error ); } }); return ( <> {currentItem.title} {currentItem.caption && ( {currentItem.caption} )} ) } > {items} {/* group divider */} ); } NavGroup.propTypes = { item: PropTypes.any, lastItem: PropTypes.number, remItems: PropTypes.array, lastItemId: PropTypes.string, selectedID: PropTypes.oneOfType([PropTypes.any, PropTypes.string]), setSelectedID: PropTypes.oneOfType([PropTypes.any, PropTypes.func]) };