import PropTypes from 'prop-types'; import { Fragment, useEffect, useState } from 'react'; import { useLocation } from 'react-router'; // material-ui import { styled, useTheme } from '@mui/material/styles'; import { Box, ClickAwayListener, List, ListItemButton, ListItemIcon, ListItemText, Paper, Popper, Typography, useMediaQuery } from '@mui/material'; // third-party import { FormattedMessage } from 'react-intl'; // project import import NavItem from './NavItem'; import NavCollapse from './NavCollapse'; import SimpleBar from 'components/third-party/SimpleBar'; import Transitions from 'components/@extended/Transitions'; import { MenuOrientation } from 'config'; import useConfig from 'hooks/useConfig'; import { dispatch, useSelector } from 'store'; import { activeID } from 'store/reducers/menu'; // assets import { DownOutlined, RightOutlined } from '@ant-design/icons'; // ==============================|| NAVIGATION - LIST GROUP ||============================== // const PopperStyled = styled(Popper)(({ theme }) => ({ overflow: 'visible', zIndex: 1202, minWidth: 180, '&:before': { content: '""', display: 'block', position: 'absolute', top: 5, left: 32, width: 12, height: 12, transform: 'translateY(-50%) rotate(45deg)', zIndex: 120, borderWidth: '6px', borderStyle: 'solid', borderColor: `${theme.palette.background.paper} transparent transparent ${theme.palette.background.paper}` } })); const NavGroup = ({ item, lastItem, remItems, lastItemId, setSelectedItems, selectedItems, setSelectedLevel, selectedLevel }) => { const theme = useTheme(); const { pathname } = useLocation(); const { menuOrientation } = useConfig(); const menu = useSelector((state) => state.menu); const { drawerOpen, selectedID } = menu; const downLG = useMediaQuery(theme.breakpoints.down('lg')); 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); } } // eslint-disable-next-line react-hooks/exhaustive-deps }, [item, lastItem, downLG]); const checkOpenForParent = (child, id) => { child.forEach((ele) => { if (ele.children?.length) { checkOpenForParent(ele.children, currentItem.id); } if (ele.url === pathname) { dispatch(activeID(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 === pathname) { dispatch(activeID(currentItem.id)); } }); }; useEffect(() => { checkSelectedOnload(currentItem); if (openMini) setAnchorEl(null); // eslint-disable-next-line react-hooks/exhaustive-deps }, [pathname, currentItem]); const handleClick = (event) => { if (!openMini) { setAnchorEl(event?.currentTarget); } }; const handleClose = () => { setAnchorEl(null); }; const Icon = currentItem?.icon; const itemIcon = currentItem?.icon ? ( ) : null; const navCollapse = item.children?.map((menuItem) => { switch (menuItem.type) { case 'collapse': return ( ); case 'item': return ; default: } }); const moreItems = remItems.map((itemRem, i) => ( {itemRem.title && ( {itemRem.title} )} {itemRem?.elements?.map((menu) => { switch (menu.type) { case 'collapse': return ( ); case 'item': return ; default: return ( Menu Items Error ); } })} )); // menu list collapse & items const items = currentItem.children?.map((menu) => { switch (menu.type) { case 'collapse': return ( ); case 'item': return ; default: return ( Menu Items Error ); } }); const popperId = openMini ? `group-pop-${item.id}` : undefined; return ( <> {menuOrientation === MenuOrientation.VERTICAL || downLG ? ( {item.title} {item.caption && ( {item.caption} )} ) } sx={{ mt: drawerOpen && item.title ? 1.5 : 0, py: 0, zIndex: 0 }} > {navCollapse} ) : ( {itemIcon && ( {currentItem.id === lastItemId ? : itemIcon} )} {currentItem.id === lastItemId ? : currentItem.title} } /> {openMini ? ( ) : ( )} {anchorEl && ( {({ TransitionProps }) => ( {currentItem.id !== lastItemId ? items : moreItems} )} )} )} ); }; NavGroup.propTypes = { item: PropTypes.object, lastItem: PropTypes.number, remItems: PropTypes.array, lastItemId: PropTypes.string, setSelectedItems: PropTypes.func, selectedItems: PropTypes.string, setSelectedLevel: PropTypes.func, selectedLevel: PropTypes.number }; export default NavGroup;