monorepo-microservice-rbac/apps/aorta/src/router/index.tsx

43 lines
1.2 KiB
TypeScript
Raw Normal View History

2023-09-12 16:19:42 +08:00
import { baseRoutes, commonRoutes } from "./router.config";
2023-09-07 16:42:34 +08:00
import { Route, RouteObject, Routes, useLocation } from "react-router-dom";
import { RouteGuard } from "./AuthGuard";
import { pathToRegexp } from "path-to-regexp";
import { defaultTitle } from "@/constant";
2023-08-27 14:37:59 +08:00
2023-09-07 16:42:34 +08:00
// export const RouterElements = () => useRoutes(routesConfig);
export type ExpandRouteProps = {
title?: string;
};
export const RouterElements = () => {
const location = useLocation();
/**
* document.title
*/
2023-09-12 16:19:42 +08:00
const currentRoute = [...baseRoutes, ...commonRoutes].find((r) =>
2023-09-07 16:42:34 +08:00
pathToRegexp(location.pathname).test(r.path!)
);
document.title = currentRoute?.title ?? defaultTitle;
/**
* children
*/
const generateRoutes = (routes: (RouteObject & ExpandRouteProps)[]) =>
routes.map((r) => (
<Route key={r.path} path={r.path} element={r.element}>
{r.children && generateRoutes(r.children)}
</Route>
));
return (
2023-09-12 16:19:42 +08:00
<RouteGuard ignorePaths={baseRoutes.map((r) => r.path!)}>
2023-09-07 16:42:34 +08:00
<Routes>
2023-09-12 16:19:42 +08:00
{generateRoutes([...baseRoutes, ...commonRoutes])}
2023-09-07 16:42:34 +08:00
<Route key="notfound" path="*" element={<span>404</span>} />
</Routes>
</RouteGuard>
);
};