All files / src/components/Sidebar/ListPopMenu index.tsx

0% Statements 0/306
0% Branches 0/1
0% Functions 0/1
0% Lines 0/306

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
import * as React from 'react';
import _ from 'lodash';
import { styled } from '@mui/material/styles';
import Tooltip from '@mui/material/Tooltip';
import Box from '@mui/material/Box';
import List from '@mui/material/List';
import ListItem from '@mui/material/ListItem';
import ListItemButton from '@mui/material/ListItemButton';
import ListItemIcon from '@mui/material/ListItemIcon';
import MenuItem from '@mui/material/MenuItem';
import Menu from '@mui/material/Menu';
import Badge from '@mui/material/Badge';
import Chip from '@mui/material/Chip';
import { SvgIconProps } from '@mui/material/SvgIcon';
import Divider from '@mui/material/Divider';
import {
  menuIcons,
  ISidebarMenuGroup,
  ISidebarMenuItem,
  ISelectedAppMenuItem,
  ISidebarMenuItemChild
} from '../menuIcons';
import { Theme } from '@/theme';
import { getInfoCount } from '../utils';

const GroupWrapper = styled('div')(({ theme }: { theme: Theme }) => ({
  '.Mui-selected': {
    backgroundColor: `${theme.palette.primary.lighter} !important`
  }
}));

const PopMenu = styled(Menu)(({ theme }: { theme: Theme }) => ({
  '.Mui-selected': {
    backgroundColor: `${theme.palette.primary.lighter} !important`
  }
}));

const PopMenuBorder = styled('div')(({ theme }: { theme: Theme }) => ({
  marginRight: theme.spacing(1),
  marginLeft: theme.spacing(1)
}));

const PopMenuItem = styled(MenuItem)(({ theme }: { theme: Theme }) => ({
  borderRadius: theme.shape.borderRadius
}));

const PopMenuItemInner: any = styled('div')(
  ({ theme, selected }: { theme: Theme; selected: boolean }) => ({
    color: selected ? theme.palette.primary.main : theme.palette.grey[600],
    fontSize: theme.typography.sidebar.fontSize,
    fontWeight: selected ? theme.typography.sidebar.fontWeight : theme.typography.fontWeightRegular,
    width: '100%',
    display: 'flex',
    justifyContent: 'space-between'
  })
);

const MenuListItem = styled(ListItemButton)(({ theme }: { theme: Theme }) => ({
  borderRadius: theme.shape.borderRadius
}));

interface MenuPopProps {
  selectedMenuNodeId: string;
  curClickItem: ISidebarMenuItem | null;
  menuItemEl: Element | null;
  isMenuOpen: boolean;
  handleMenuClose: () => void;
  handleItemClick: (
    fatherItem: ISidebarMenuItem,
    nodeId: string,
    childItem: ISidebarMenuItemChild
  ) => void;
}
function MenuPop(props: MenuPopProps) {
  const {
    selectedMenuNodeId,
    curClickItem,
    menuItemEl,
    isMenuOpen,
    handleMenuClose,
    handleItemClick
  } = props;

  const uiSize = 'small';

  return (
    <PopMenu
      anchorEl={menuItemEl}
      id="app-list-menu-pop"
      open={isMenuOpen}
      onClose={handleMenuClose}
      transformOrigin={{ horizontal: 24, vertical: 'top' }}
      anchorOrigin={{ horizontal: 'right', vertical: 10 }}
    >
      <PopMenuBorder>
        {curClickItem
          && Array.isArray(curClickItem.children)
          && curClickItem.children.map((child: ISidebarMenuItemChild) => {
            const childSelected = child.nodeId === selectedMenuNodeId;
            const childInfoCount = getInfoCount(child.labelInfo);
            return (
              <PopMenuItem
                key={_.uniqueId(`apppopmenuitem_${child.nodeId}`)}
                selected={childSelected}
                onClick={() => {
                  handleItemClick(curClickItem, child.nodeId, child);
                }}
              >
                <PopMenuItemInner selected={childSelected}>
                  {child.labelText}
                  {childInfoCount > 0 && (
                    <Chip
                      size={uiSize}
                      label={childInfoCount > 999 ? '999+' : childInfoCount}
                      sx={(theme: Theme) => ({
                        ml: theme.spacing(1),
                        fontWeight: childSelected
                          ? theme.typography.fontWeightBold
                          : theme.typography.fontWeightRegular,
                        color: childSelected
                          ? theme.palette.common.white
                          : theme.palette.primary.bright,
                        backgroundColor: childSelected
                          ? theme.palette.primary.main
                          : theme.palette.primary.lighter
                      })}
                    />
                  )}
                </PopMenuItemInner>
              </PopMenuItem>
            );
          })}
      </PopMenuBorder>
    </PopMenu>
  );
}

interface StyledMenuItemProps {
  selected: boolean;
  labelIcon: React.ElementType<SvgIconProps>;
}
function StyledMenuItem(props: StyledMenuItemProps) {
  const { selected, labelIcon: LabelIcon } = props;

  return (
    <Box
      component={LabelIcon}
      color="inherit"
      sx={[
        { mr: 1 },
        (theme: Theme) => ({
          fontWeight: selected ? theme.typography.sidebar.fontWeight : 'inherit',
          color: selected ? theme.palette.primary.main : theme.palette.grey[600]
        })
      ]}
    />
  );
}

export interface IListPopMenuProps {
  menuData: ISidebarMenuGroup[];
  onSelected: (selectedItem: ISelectedAppMenuItem) => void;
  selectedNodeId: string;
  setSelectedNodeId: (nodeId: string) => void;
  selectedMenuNodeId: string;
  setSelectedMenuNodeId: (nodeId: string) => void;
}

export default function ListPopMenu({
  menuData,
  onSelected,
  selectedNodeId,
  setSelectedNodeId,
  selectedMenuNodeId,
  setSelectedMenuNodeId
}: IListPopMenuProps) {
  const [curClickItem, setCurClickItem] = React.useState<ISidebarMenuItem | null>(null);
  const [menuItemEl, setMenuItemEl] = React.useState<null | HTMLElement>(null);
  const isMenuOpen = Boolean(menuItemEl);

  const handleFatherClick = (
    hasChildren: boolean,
    nodeItem: ISidebarMenuItem,
    currentTarget: any
  ) => {
    if (hasChildren) {
      setCurClickItem(nodeItem);
      setMenuItemEl(currentTarget);
    } else {
      setMenuItemEl(null);
      setSelectedNodeId(nodeItem.nodeId);
      setSelectedMenuNodeId('');
      onSelected({ nodeItem });
    }
  };

  const handleMenuClose = React.useCallback(() => {
    setMenuItemEl(null);
  }, []);

  const handleMenuItemClick = React.useCallback(
    (fatherItem: ISidebarMenuItem, nodeId: string, childItem: ISidebarMenuItemChild) => {
      setMenuItemEl(null);
      setSelectedNodeId(fatherItem.nodeId);
      setSelectedMenuNodeId(nodeId);
      onSelected({ nodeItem: fatherItem, childItem });
    },
    []
  );

  return (
    <GroupWrapper>
      <List component="nav" aria-label="Device settings">
        {Array.isArray(menuData)
          && menuData.map((group: ISidebarMenuGroup, groupIndex: number) => {
            const nodeGroupKey = `menuicongroup_${group.nodeId}`;

            const renderItems = group.children.length > 0
              && group.children.map((item: ISidebarMenuItem) => {
                const hasChildren = Array.isArray(item.children) && item.children.length > 0;
                const itemSelected = selectedNodeId === item.nodeId;
                const nodeKey = `menuiconitems_${item.nodeId}`;
                const infoCount = getInfoCount(item.labelInfo);
                return (
                  <React.Fragment key={nodeKey}>
                    <ListItem
                      onClick={(event: React.MouseEvent<HTMLElement>) => {
                        handleFatherClick(hasChildren, item, event.currentTarget);
                      }}
                      id={_.uniqueId(`menuitemiconid_${item.nodeId}`)}
                      key={item.nodeId}
                      disablePadding
                      sx={{ display: 'block' }}
                      aria-haspopup="listbox"
                      aria-controls="lock-menu"
                      aria-label="when device is locked"
                      aria-expanded={isMenuOpen ? 'true' : undefined}
                    >
                      <MenuListItem
                        selected={itemSelected}
                        sx={{
                          minHeight: 48,
                          justifyContent: 'center',
                          px: 2.5
                        }}
                      >
                        <Tooltip title={item.labelText}>
                          <ListItemIcon
                            sx={{
                              minWidth: 0,
                              mr: 'auto',
                              justifyContent: 'center'
                            }}
                          >
                            {!hasChildren && infoCount > 0 ? (
                              <Badge color="secondary" badgeContent={infoCount} max={999}>
                                <StyledMenuItem
                                  selected={itemSelected}
                                  labelIcon={
                                    !item.labelIconName
                                      ? undefined
                                      : (menuIcons as any)[item.labelIconName]
                                  }
                                />
                              </Badge>
                            ) : (
                              <StyledMenuItem
                                selected={itemSelected}
                                labelIcon={
                                  !item.labelIconName
                                    ? undefined
                                    : (menuIcons as any)[item.labelIconName]
                                }
                              />
                            )}
                          </ListItemIcon>
                        </Tooltip>
                      </MenuListItem>
                    </ListItem>
                  </React.Fragment>
                );
              });

            return (
              <React.Fragment key={nodeGroupKey}>
                {groupIndex > 0 && (
                  <Divider
                    sx={(theme: Theme) => ({ mt: theme.spacing(1), mb: theme.spacing(1) })}
                  />
                )}
                {renderItems}
              </React.Fragment>
            );
          })}
      </List>
      <MenuPop
        selectedMenuNodeId={selectedMenuNodeId}
        curClickItem={curClickItem}
        isMenuOpen={isMenuOpen}
        menuItemEl={menuItemEl}
        handleMenuClose={handleMenuClose}
        handleItemClick={handleMenuItemClick}
      />
    </GroupWrapper>
  );
}