import { ForwardRefExoticComponent, RefAttributes, forwardRef, useEffect } from 'react';
import { Link } from 'react-router-dom';
import { useDispatch, useSelector } from 'react-redux';
// material-ui
import { useTheme } from '@mui/material/styles';
import { Avatar, Chip, ListItemButton, ListItemIcon, ListItemText, Typography, useMediaQuery } from '@mui/material';
// project imports
import { MENU_OPEN, SET_MENU } from 'store/actions';
import { DefaultRootStateProps, LinkTarget, NavItemType } from 'types';
// assets
import FiberManualRecordIcon from '@mui/icons-material/FiberManualRecord';
export interface NavItemProps {
item: NavItemType;
level: number;
}
// ==============================|| SIDEBAR MENU LIST ITEMS ||============================== //
const NavItem = ({ item, level }: NavItemProps) => {
const theme = useTheme();
const dispatch = useDispatch();
const customization = useSelector((state: DefaultRootStateProps) => state.customization);
const matchesSM = useMediaQuery(theme.breakpoints.down('lg'));
const Icon = item?.icon!;
const itemIcon = item?.icon ? (
) : (
id === item?.id) > -1 ? 8 : 6,
height: customization.isOpen.findIndex((id) => id === item?.id) > -1 ? 8 : 6
}}
fontSize={level > 0 ? 'inherit' : 'medium'}
/>
);
let itemTarget: LinkTarget = '_self';
if (item.target) {
itemTarget = '_blank';
}
let listItemProps: {
component: ForwardRefExoticComponent> | string;
href?: string;
target?: LinkTarget;
} = { component: forwardRef((props, ref) => ) };
if (item?.external) {
listItemProps = { component: 'a', href: item.url, target: itemTarget };
}
const itemHandler = (id: string) => {
dispatch({ type: MENU_OPEN, id });
matchesSM && dispatch({ type: SET_MENU, opened: false });
};
// active menu item on page load
useEffect(() => {
const currentIndex = document.location.pathname
.toString()
.split('/')
.findIndex((id) => id === item.id);
if (currentIndex > -1) {
dispatch({ type: MENU_OPEN, id: item.id });
}
// eslint-disable-next-line
}, []);
return (
1 ? 'transparent !important' : 'inherit',
py: level > 1 ? 1 : 1.25,
pl: `${level * 24}px`
}}
selected={customization.isOpen.findIndex((id) => id === item.id) > -1}
onClick={() => itemHandler(item.id!)}
>
{itemIcon}
id === item.id) > -1 ? 'h5' : 'body1'} color="inherit">
{item.title}
}
secondary={
item.caption && (
{item.caption}
)
}
/>
{item.chip && (
{item.chip.avatar}}
/>
)}
);
};
export default NavItem;