From 3f7c81e89221c9b1b4294adcf41b8e7d5b0e3fd1 Mon Sep 17 00:00:00 2001 From: typist Date: Wed, 29 Oct 2025 05:45:09 +0800 Subject: [PATCH] feat: refactor AppSidebar to support nested tool menus with collapsible items - Enhanced the AppSidebar component to recursively render tools with potential submenus. - Introduced a buildFullPath function for dynamic routing based on tool hierarchy. - Utilized the Collapsible component for better organization of tools with children. - Maintained existing footer and header structure for consistency. --- src/components/sidebar/index.tsx | 130 ++++++++++++++++++++++--------- 1 file changed, 94 insertions(+), 36 deletions(-) diff --git a/src/components/sidebar/index.tsx b/src/components/sidebar/index.tsx index a6dde77..a43c742 100644 --- a/src/components/sidebar/index.tsx +++ b/src/components/sidebar/index.tsx @@ -1,40 +1,98 @@ -import { Sidebar, SidebarContent, SidebarFooter, SidebarGroup, SidebarGroupContent, SidebarGroupLabel, SidebarHeader, SidebarMenuButton, SidebarMenuItem } from "@/components/ui/sidebar"; -import { tools } from "@/components/tool"; +import type { ReactNode } from "react"; +import { Sidebar, SidebarContent, SidebarFooter, SidebarGroup, SidebarGroupContent, SidebarGroupLabel, SidebarHeader, SidebarMenuButton, SidebarMenuItem, SidebarMenu, SidebarMenuSub, SidebarMenuSubItem, SidebarMenuSubButton } from "@/components/ui/sidebar"; +import { Collapsible, CollapsibleTrigger, CollapsibleContent } from "@/components/ui/collapsible"; +import { tools, type Tool } from "@/components/tool"; import { Link } from "react-router-dom"; import { ModeToggle } from "@/components/theme/toggle"; import { Button } from "../ui/button"; +import { ChevronRight } from "lucide-react"; -export const AppSidebar = () => ( - - - Lite Kit - - - - - Tools - - - { - tools.map((tool) => ( - - - - {tool.icon} - {tool.name} - - - - )) - } - - - - - - - - -) \ No newline at end of file +export const AppSidebar = () => { + // 递归构建完整路径 + const buildFullPath = (pathSegments: string[]): string => { + return `/tool/${pathSegments.join("/")}`; + }; + + // 递归渲染菜单项 + const renderMenuItem = (tool: Tool, parentPaths: string[] = []): ReactNode => { + const currentPaths = [...parentPaths, tool.path]; + + if (tool.children) { + // 有子菜单的项目 + return ( + + + + + {tool.icon} + {tool.name} + + + + + + {tool.children.map((child) => ( + + {child.children ? ( + renderMenuItem(child, currentPaths) + ) : ( + + + {child.icon} + {child.name} + + + )} + + ))} + + + + + ); + } + + // 没有子菜单的项目 + return ( + + + + {tool.icon} + {tool.name} + + + + ); + }; + + return ( + + + Lite Kit + + + + Tools + + + {tools.map((tool) => renderMenuItem(tool))} + + + + + + + + + + ); +}; \ No newline at end of file