{
- subMenuRefs.current[`${menuType}-${index}`] = el;
- }}
+ ref={(el) => { subMenuRefs.current[`${menuType}-${index}`] = el; }}
className="overflow-hidden transition-all duration-300"
- style={{
- height:
- openSubmenu?.type === menuType && openSubmenu?.index === index
- ? `${subMenuHeight[`${menuType}-${index}`]}px`
- : "0px",
- }}
+ style={{ height: openSubmenu?.type === menuType && openSubmenu?.index === index ? `${subMenuHeight[`${menuType}-${index}`]}px` : "0px" }}
>
{nav.subItems.map((subItem) => (
-
-
+
{subItem.name}
- {subItem.new && (
-
- new
-
- )}
- {subItem.pro && (
-
- pro
-
- )}
+ {subItem.new && new}
+ {subItem.pro && pro}
@@ -228,160 +256,44 @@ const AppSidebar: React.FC = () => {
);
- const [openSubmenu, setOpenSubmenu] = useState<{
- type: "main" | "others";
- index: number;
- } | null>(null);
- const [subMenuHeight, setSubMenuHeight] = useState
>(
- {},
- );
- const subMenuRefs = useRef>({});
-
- // const isActive = (path: string) => path === pathname;
- const isActive = useCallback((path: string) => path === pathname, [pathname]);
-
- useEffect(() => {
- // Check if the current path matches any submenu item
- let submenuMatched = false;
- ["main", "others"].forEach((menuType) => {
- const items = menuType === "main" ? navItems : othersItems;
- items.forEach((nav, index) => {
- if (nav.subItems) {
- nav.subItems.forEach((subItem) => {
- if (isActive(subItem.path)) {
- setOpenSubmenu({
- type: menuType as "main" | "others",
- index,
- });
- submenuMatched = true;
- }
- });
- }
- });
- });
-
- // If no submenu item matches, close the open submenu
- if (!submenuMatched) {
- setOpenSubmenu(null);
- }
- }, [pathname, isActive]);
-
- useEffect(() => {
- // Set the height of the submenu items when the submenu is opened
- if (openSubmenu !== null) {
- const key = `${openSubmenu.type}-${openSubmenu.index}`;
- if (subMenuRefs.current[key]) {
- setSubMenuHeight((prevHeights) => ({
- ...prevHeights,
- [key]: subMenuRefs.current[key]?.scrollHeight || 0,
- }));
- }
- }
- }, [openSubmenu]);
-
- const handleSubmenuToggle = (index: number, menuType: "main" | "others") => {
- setOpenSubmenu((prevOpenSubmenu) => {
- if (
- prevOpenSubmenu &&
- prevOpenSubmenu.type === menuType &&
- prevOpenSubmenu.index === index
- ) {
- return null;
- }
- return { type: menuType, index };
- });
- };
-
return (