refactor: dynamic routes

This commit is contained in:
jialin
2026-05-08 09:33:16 +08:00
committed by jialin
parent 4be5fb4d3d
commit f08d5c904c
8 changed files with 8 additions and 309 deletions
-57
View File
@@ -1,57 +0,0 @@
import { getGPUStackPlugin } from '@/plugins';
import type { RouteConfig } from '@/plugins/types';
type ClientRoute = RouteConfig & {
id?: string;
children?: ClientRoute[];
routes?: ClientRoute[];
};
function getLayoutChildren(routes: ClientRoute[]): ClientRoute[] | null {
const rootRoute = routes.find((route) => route.path === '/');
if (!rootRoute) return null;
const rootChildren = rootRoute.routes || rootRoute.children;
if (!Array.isArray(rootChildren)) return null;
const globalLayout = rootChildren.find((route) =>
route.id?.includes('global-layout')
);
if (!globalLayout) return null;
const layoutChildren = globalLayout.children || globalLayout.routes;
return Array.isArray(layoutChildren) ? layoutChildren : null;
}
function mergeRoute(targetRoutes: ClientRoute[], pluginRoute: ClientRoute) {
const existsIndex = targetRoutes.findIndex(
(route) =>
(pluginRoute.path && route.path === pluginRoute.path) ||
(pluginRoute.key && route.key === pluginRoute.key)
);
if (existsIndex === -1) {
targetRoutes.push(pluginRoute);
return;
}
const currentRoute = targetRoutes[existsIndex];
targetRoutes[existsIndex] = {
...currentRoute,
...pluginRoute,
routes: pluginRoute.routes || currentRoute.routes,
children: pluginRoute.children || currentRoute.children
};
}
export function mergeEnterpriseRoutes(routes: ClientRoute[]) {
const enterprisePlugin = getGPUStackPlugin();
if (!enterprisePlugin?.routes?.length) return;
const layoutChildren = getLayoutChildren(routes);
if (!layoutChildren) return;
enterprisePlugin.routes.forEach((route) =>
mergeRoute(layoutChildren, route as ClientRoute)
);
}