Files
litek/src/router.tsx
typist 3b31ce9ddf feat: add-tool network (#9)
Co-authored-by: typist <git@mail.typist.cc>
Reviewed-on: #9
2025-10-29 05:51:11 +08:00

58 lines
1.1 KiB
TypeScript

import {
createBrowserRouter,
redirect,
RouterProvider,
type RouteObject,
} from "react-router-dom";
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([
{
path: "",
element: <Layout />,
children: [
{
path: "tool",
children: [
...buildToolRoutes(tools),
{
index: true,
loader: () => redirect("/tool/uuid"),
},
],
},
],
},
{
index: true,
loader: () => redirect("/tool"),
},
{
path: "*",
loader: () => redirect("/tool"),
},
]);
// 路由提供者组件
export const AppRouter = () => <RouterProvider router={router} />;