import styles from './SidebarStyles/SidebarSubmenu.module.css'; import { Link, useLocation } from 'react-router-dom'; import { motion, AnimatePresence } from 'framer-motion'; import React, { useRef, useEffect, useState } from 'react'; import { SidebarSubmenuProps, SidebarSubmenuItemData } from './sidebarTypes'; // Separate component for submenu item to properly use hooks interface SubmenuItemProps { subitem: SidebarSubmenuItemData; isActive: boolean; } const SubmenuItem: React.FC = ({ subitem, isActive }) => { const textRef = useRef(null); const containerRef = useRef(null); const [isOverflowing, setIsOverflowing] = useState(false); useEffect(() => { const checkOverflow = () => { if (textRef.current && containerRef.current) { const textWidth = textRef.current.scrollWidth; const containerWidth = containerRef.current.clientWidth; setIsOverflowing(textWidth > containerWidth); } }; checkOverflow(); // Also check on window resize window.addEventListener('resize', checkOverflow); return () => window.removeEventListener('resize', checkOverflow); }, [subitem.name]); const SubIcon = subitem.icon as React.ComponentType>; return (
  • {SubIcon && } {subitem.name}
  • ); }; const SidebarSubmenu: React.FC = ({ item, isOpen, isMinimized = false }) => { const location = useLocation(); // Check if a submenu item is active const isSubmenuItemActive = (itemPath?: string) => { if (!itemPath) return false; const currentPath = location.pathname; // Exact match or prefix match at path segment boundary if (currentPath === itemPath) return true; if (currentPath.startsWith(itemPath)) { const nextChar = currentPath[itemPath.length]; if (nextChar === '/' || nextChar === undefined) return true; } return false; }; if (!item.submenu) return null; return ( {isOpen && ( {isMinimized ? ( // Horizontal layout for minimized sidebar
      {item.submenu.map(subitem => { const SubIcon = subitem.icon as React.ComponentType>; const isActive = isSubmenuItemActive(subitem.link); return (
    • {SubIcon && ( )}
    • ); })}
    ) : ( // Vertical layout for expanded sidebar
      {item.submenu.map(subitem => ( ))}
    )}
    )}
    ); }; export default SidebarSubmenu;