import * as React from 'react'; import PropTypes from 'prop-types'; import Avatar from '@mui/material/Avatar'; import Box from '@mui/material/Box'; import Collapse from '@mui/material/Collapse'; import Grow from '@mui/material/Grow'; import ListItem from '@mui/material/ListItem'; import ListItemButton from '@mui/material/ListItemButton'; import ListItemIcon from '@mui/material/ListItemIcon'; import ListItemText from '@mui/material/ListItemText'; import Paper from '@mui/material/Paper'; import Typography from '@mui/material/Typography'; import ExpandMoreIcon from '@mui/icons-material/ExpandMore'; import { Link } from 'react-router'; import DashboardSidebarContext from '../context/DashboardSidebarContext'; import { MINI_DRAWER_WIDTH } from '../constants'; function DashboardSidebarPageItem({ id, title, icon, href, action, defaultExpanded = false, expanded = defaultExpanded, selected = false, disabled = false, nestedNavigation, }) { const sidebarContext = React.useContext(DashboardSidebarContext); if (!sidebarContext) { throw new Error('Sidebar context was used without a provider.'); } const { onPageItemClick, mini = false, fullyExpanded = true, fullyCollapsed = false, } = sidebarContext; const [isHovered, setIsHovered] = React.useState(false); const handleClick = React.useCallback(() => { if (onPageItemClick) { onPageItemClick(id, !!nestedNavigation); } }, [onPageItemClick, id, nestedNavigation]); let nestedNavigationCollapseSx = { display: 'none' }; if (mini && fullyCollapsed) { nestedNavigationCollapseSx = { fontSize: 18, position: 'absolute', top: '41.5%', right: '2px', transform: 'translateY(-50%) rotate(-90deg)', }; } else if (!mini && fullyExpanded) { nestedNavigationCollapseSx = { ml: 0.5, fontSize: 20, transform: `rotate(${expanded ? 0 : -90}deg)`, transition: (theme) => theme.transitions.create('transform', { easing: theme.transitions.easing.sharp, duration: 100, }), }; } const hasExternalHref = href ? href.startsWith('http://') || href.startsWith('https://') : false; const LinkComponent = hasExternalHref ? 'a' : Link; const miniNestedNavigationSidebarContextValue = React.useMemo(() => { return { onPageItemClick: onPageItemClick ?? (() => {}), mini: false, fullyExpanded: true, fullyCollapsed: false, hasDrawerTransitions: false, }; }, [onPageItemClick]); return ( { setIsHovered(true); }, onMouseLeave: () => { setIsHovered(false); }, } : {})} sx={{ display: 'block', py: 0, px: 1, overflowX: 'hidden', }} > {icon || mini ? ( {icon ?? null} {!icon && mini ? ( {title .split(' ') .slice(0, 2) .map((titleWord) => titleWord.charAt(0).toUpperCase())} ) : null} {mini ? ( {title} ) : null} ) : null} {!mini ? ( ) : null} {action && !mini && fullyExpanded ? action : null} {nestedNavigation ? ( ) : null} {nestedNavigation && mini ? ( {nestedNavigation} ) : null} {nestedNavigation && !mini ? ( {nestedNavigation} ) : null} ); } DashboardSidebarPageItem.propTypes = { action: PropTypes.node, defaultExpanded: PropTypes.bool, disabled: PropTypes.bool, expanded: PropTypes.bool, href: PropTypes.string.isRequired, icon: PropTypes.node, id: PropTypes.string.isRequired, nestedNavigation: PropTypes.node, selected: PropTypes.bool, title: PropTypes.string.isRequired, }; export default DashboardSidebarPageItem;