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

0% Statements 0/24
0% Branches 0/35
0% Functions 0/4
0% Lines 0/23

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                                                                                                                                                                                                                                                                                                   
import { ReactNode } from 'react';
import { NavLink } from 'react-router';
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 ListItemSecondaryAction from '@mui/material/ListItemSecondaryAction';
import Tooltip from '@mui/material/Tooltip';
import Box from '@mui/material/Box';
import type { SidebarMenuItem } from '../../containers/Sidebar';
 
interface SidebarFlatItemProps {
  item: SidebarMenuItem;
  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 SidebarFlatItem({ item, collapsed = false }: SidebarFlatItemProps) {
  let icon: ReactNode = null;
  let secondaryAction: ReactNode = null;
  let secondaryActionMenu: ReactNode = null;
 
  if (item.icon) {
    icon = <ListItemIcon sx={{ minWidth: collapsed ? 36 : 40 }}>{item.icon}</ListItemIcon>;
  }
 
  if (item.secondaryMenu && !collapsed) {
    secondaryActionMenu = item.secondaryMenu;
    secondaryAction = (
      <ListItemSecondaryAction>{item.secondaryButton}</ListItemSecondaryAction>
    );
  }
 
  // Render content for the button
  const renderButtonContent = () => (
    <>
      {icon}
      {collapsed ? (
        <Box
          sx={{
            fontSize: '0.75rem',
            fontWeight: 500,
            textTransform: 'uppercase',
            color: 'text.secondary',
            ml: -1,
          }}
        >
          {getAbbreviation(item.label)}
        </Box>
      ) : (
        <ListItemText primary={item.label} />
      )}
      {!collapsed && secondaryActionMenu}
    </>
  );
 
  // Render with NavLink if `to` prop is provided
  if (item.to) {
    const buttonElement = (
      <ListItem
        key={'itemFlat' + item.label}
        disablePadding
        secondaryAction={!collapsed ? secondaryAction : undefined}
      >
        <NavLink
          to={item.to}
          end={item.exact}
          style={{
            textDecoration: 'none',
            color: 'inherit',
            display: 'block',
            width: '100%',
          }}
        >
          {({ isActive }) => (
            <ListItemButton
              selected={isActive}
              onClick={item.onClick}
              sx={{
                px: collapsed ? 1 : 2,
                justifyContent: collapsed ? 'center' : 'flex-start',
              }}
            >
              {renderButtonContent()}
            </ListItemButton>
          )}
        </NavLink>
      </ListItem>
    );
 
    // Wrap in Tooltip when collapsed
    if (collapsed) {
      return (
        <Tooltip title={item.label} placement="right" arrow>
          {buttonElement}
        </Tooltip>
      );
    }
 
    return buttonElement;
  }
 
  // Render with onClick if no `to` prop
  const buttonElement = (
    <ListItem
      key={'itemFlat' + item.label}
      disablePadding
      secondaryAction={!collapsed ? secondaryAction : undefined}
    >
      <ListItemButton
        selected={item.selected}
        onClick={item.onClick}
        sx={{
          px: collapsed ? 1 : 2,
          justifyContent: collapsed ? 'center' : 'flex-start',
        }}
      >
        {renderButtonContent()}
      </ListItemButton>
    </ListItem>
  );
 
  // Wrap in Tooltip when collapsed
  if (collapsed) {
    return (
      <Tooltip title={item.label} placement="right" arrow>
        {buttonElement}
      </Tooltip>
    );
  }
 
  return buttonElement;
}
 
export default SidebarFlatItem;