From 79c4462bfddb17edbab967494a5847030359b6aa Mon Sep 17 00:00:00 2001 From: typist Date: Wed, 29 Oct 2025 05:35:37 +0800 Subject: [PATCH] feat: enhance tool routing and add network tools - Refactored routing logic in src/router.tsx to utilize a buildToolRoutes function for better scalability. - Updated tools in src/components/tool/index.tsx to include a new "Network Tools" category with DNS, Ping, TCPing, and Speed Test components. --- src/components/tool/index.tsx | 3 ++- src/router.tsx | 32 +++++++++++++++++++++++--------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/components/tool/index.tsx b/src/components/tool/index.tsx index 735a1d8..b0701be 100644 --- a/src/components/tool/index.tsx +++ b/src/components/tool/index.tsx @@ -10,7 +10,8 @@ export interface Tool { name: string; icon: ReactNode; description: string; - component: ReactNode; + component?: ReactNode; + children?: Tool[]; } export const tools: Tool[] = [ diff --git a/src/router.tsx b/src/router.tsx index c73043d..0760224 100644 --- a/src/router.tsx +++ b/src/router.tsx @@ -2,11 +2,30 @@ import { createBrowserRouter, redirect, RouterProvider, + type RouteObject, } from "react-router-dom"; -import { tools } from "@/components/tool"; +import { tools, type Tool } from "@/components/tool"; import { Layout } from "./layout"; +const buildToolRoutes = (tools: Tool[]): RouteObject[] => { + return tools.map((tool) => { + const route: RouteObject = { + path: tool.path, + }; + + if (tool.component) { + route.element = tool.component; + } + + if (tool.children && tool.children.length > 0) { + route.children = buildToolRoutes(tool.children); + } + + return route; + }); +}; + // 路由配置 const router = createBrowserRouter([ { @@ -16,19 +35,14 @@ const router = createBrowserRouter([ { path: "tool", children: [ - ...tools.map((tool) => ( - { - path: tool.path, - element: tool.component, - } - )), + ...buildToolRoutes(tools), { index: true, loader: () => redirect("/tool/uuid"), }, - ] + ], }, - ] + ], }, { index: true,