All files / frontend/src/components/Sidebar SidebarNestedItem.tsx

0% Statements 0/20
0% Branches 0/33
0% Functions 0/5
0% Lines 0/19

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                                                                                                                                                                                                                                                                                                             
import { Fragment } from 'react';
import { NavLink } from 'react-router';
import ListItem from '@mui/material/ListItem';
import ListItemButton from '@mui/material/ListItemButton';
import ListItemText from '@mui/material/ListItemText';
import List from '@mui/material/List';
import Collapse from '@mui/material/Collapse';
import Tooltip from '@mui/material/Tooltip';
import Box from '@mui/material/Box';
import ExpandLess from '@mui/icons-material/ExpandLess';
import ExpandMore from '@mui/icons-material/ExpandMore';
import type { SidebarMenuItem } from '../../containers/Sidebar';
 
interface SidebarNestedItemProps {
  item: SidebarMenuItem;
  index: number;
  openIndex: number | null;
  onToggle: (index: number | null) => void;
  collapsed?: boolean;
}
 
/**
 * Get abbreviated text (first 1-2 characters) for collapsed state
 */
function getAbbreviation(label: string): string {
  const trimmed = label.trim();
  if (!trimmed) return '';
 
  // Take first 2 characters, or 1 if it's a short word
  return trimmed.length === 1 ? trimmed[0] : trimmed.substring(0, 2);
}
 
function SidebarNestedItem({
  item,
  index,
  openIndex,
  onToggle,
  collapsed = false,
}: SidebarNestedItemProps) {
  // Determine if this nested item should be initially open
  let initOpen: boolean | undefined;
  const childItems = item.childItems!.map((itemChild) => {
    if (itemChild.selected) {
      initOpen = true;
    }
 
    // Child item with NavLink
    if (itemChild.to) {
      return (
        <ListItem key={'itemNestChild' + itemChild.label} disablePadding>
          <NavLink
            to={itemChild.to}
            end={itemChild.exact}
            style={{
              textDecoration: 'none',
              color: 'inherit',
              display: 'block',
              width: '100%',
            }}
          >
            {({ isActive }) => (
              <ListItemButton
                selected={isActive}
                onClick={itemChild.onClick}
                sx={{ pl: collapsed ? 2 : 4 }}
              >
                {!collapsed && <ListItemText primary={itemChild.label} />}
              </ListItemButton>
            )}
          </NavLink>
        </ListItem>
      );
    }
 
    // Child item with onClick
    return (
      <ListItem key={'itemNestChild' + itemChild.label} disablePadding>
        <ListItemButton
          onClick={itemChild.onClick}
          selected={itemChild.selected}
          sx={{ pl: collapsed ? 2 : 4 }}
        >
          {!collapsed && <ListItemText primary={itemChild.label} />}
        </ListItemButton>
      </ListItem>
    );
  });
 
  const isOpen = initOpen || openIndex === index;
 
  const handleToggle = () => {
    if (openIndex === index) {
      onToggle(null);
    } else {
      onToggle(index);
    }
  };
 
  const parentButton = (
    <Fragment key={'itemNestOut' + item.label}>
      <ListItem disablePadding>
        <ListItemButton
          selected={item.selected}
          onClick={handleToggle}
          sx={{
            px: collapsed ? 1 : 2,
            justifyContent: collapsed ? 'center' : 'flex-start',
          }}
        >
          {collapsed ? (
            <Box
              sx={{
                fontSize: '0.75rem',
                fontWeight: 500,
                textTransform: 'uppercase',
                color: 'text.secondary',
              }}
            >
              {getAbbreviation(item.label)}
            </Box>
          ) : (
            <>
              <ListItemText primary={item.label} />
              {isOpen ? <ExpandLess /> : <ExpandMore />}
            </>
          )}
        </ListItemButton>
      </ListItem>
 
      <Collapse in={isOpen && !collapsed} timeout="auto" unmountOnExit>
        <List component="div" disablePadding>
          {childItems}
        </List>
      </Collapse>
    </Fragment>
  );
 
  // Wrap in Tooltip when collapsed
  if (collapsed) {
    return (
      <Tooltip title={item.label} placement="right" arrow>
        {parentButton}
      </Tooltip>
    );
  }
 
  return parentButton;
}
 
export default SidebarNestedItem;