import * as React from 'react'; import { styled, Theme, CSSObject } from '@mui/material/styles'; import MuiDrawer from '@mui/material/Drawer'; import List from '@mui/material/List'; import Box from '@mui/material/Box'; import Divider from '@mui/material/Divider'; import Alert from '@mui/material/Alert'; import Typography from '@mui/material/Typography'; import Avatar from '@mui/material/Avatar'; import { CategorizedMenu, KOSMenuProps, MenuItem } from './KOSMenuProps'; import KOSMenuElement from './KOSMenuElement'; import KOSMenuModuleName from './KOSMenuModuleName'; import KOSMenuSkeleton from './KOSMenuSkeleton'; const drawerWidth = 280; const openedMixin = (theme: Theme): CSSObject => ({ width: drawerWidth, transition: theme.transitions.create('width', { easing: theme.transitions.easing.sharp, duration: theme.transitions.duration.enteringScreen, }), overflowX: 'hidden', }); const closedMixin = (theme: Theme): CSSObject => ({ transition: theme.transitions.create('width', { easing: theme.transitions.easing.sharp, duration: 0, }), overflowX: 'hidden', width: `${theme.spacing(0)}`, paddingLeft: 0, paddingRight: 0, [theme.breakpoints.up('lg')]: { width: `calc(${theme.spacing(10)} + 1px)`, paddingLeft: '2px', paddingRight: '2px', }, }); const Drawer = styled(MuiDrawer, { shouldForwardProp: prop => prop !== 'open', })(({ theme, open }) => ({ width: drawerWidth, flexShrink: 0, whiteSpace: 'nowrap', boxSizing: 'border-box', ...(open && { ...openedMixin(theme), '& .MuiDrawer-paper': { ...openedMixin(theme), paddingTop: theme.mixins.toolbar.height, paddingLeft: '12px', paddingRight: '12px', border: 0, }, '& .user-info': { opacity: 1, }, }), ...(!open && { ...closedMixin(theme), '& .MuiDrawer-paper': { ...closedMixin(theme), paddingTop: theme.mixins.toolbar.height, border: 0, }, '& .user-info': { opacity: 0, }, }), })); const KOSMenu = (props: KOSMenuProps) => { const { user, drawerOpen, items, loading, error, onBackAction, onClickMenuItem, homeUrl, isBackButtonEnable, sx = {}, } = props; const homePath = homeUrl ?? '/'; const [ categorizedMenu, setCategorizedMenu, ] = React.useState(null); React.useEffect(() => { if (items) { const newCategorizedUserMenu: CategorizedMenu = { 'kitchen-display': [], 'kitchen-staff': [], 'kitchen-monitor': [], }; items.forEach((userMenuElement: MenuItem) => { if (!newCategorizedUserMenu[userMenuElement.module]) { newCategorizedUserMenu[userMenuElement.module] = []; } newCategorizedUserMenu[userMenuElement.module].push(userMenuElement); }); setCategorizedMenu(newCategorizedUserMenu); } }, [items]); return ( {drawerOpen && user && ( {user.profileImage && ( )} {user.profile?.firstName && ( {user.profile?.firstName} )} {user.profile?.lastName && ( {user.profile?.lastName} )} {user.rolName && ( {user.rolName} )} {user.profile?.position && ( {user.profile?.position} )} )} {onBackAction && ( )} {loading && ( <> )} {error && } {!loading && categorizedMenu && Object.keys(categorizedMenu).map(module => ( {categorizedMenu[module].length > 0 && ( <> {drawerOpen && ( )} {!drawerOpen && ( )} {categorizedMenu[module].map((menuItem: MenuItem) => ( ))} )} ))} ); }; KOSMenu.defaultProps = { user: null, drawerOpen: false, loading: false, error: false, isBackButtonEnable: true, onBackAction: undefined, onClickMenuItem: undefined, sx: {}, }; export default KOSMenu;