Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

refactor: According to the status of the plug-in, determine whether the routing plug-in is enabled

robind9d5a787fd3f
diff --git a/ui/src/router/RouteGuard.tsx b/ui/src/router/RouteGuard.tsx
index 26c00d4c..8654726e 100644
--- a/ui/src/router/RouteGuard.tsx
+++ b/ui/src/router/RouteGuard.tsx
@@ -50,11 +50,11 @@ const RouteGuard: FC<{
setKeeper(gr);
if (
- gk.ok === false &&
- gk.error?.code &&
- /403|404|50X/i.test(gk.error.code.toString())
+ gr.ok === false &&
+ gr.error?.code &&
+ /403|404|50X/i.test(gr.error.code.toString())
) {
- setGkError(gk.error);
+ setGkError(gr.error);
return;
}
setGkError(undefined);
diff --git a/ui/src/router/index.tsx b/ui/src/router/index.tsx
index 4cb222a9..432cb581 100644
--- a/ui/src/router/index.tsx
+++ b/ui/src/router/index.tsx
@@ -45,8 +45,16 @@ const routeWrapper = (routeNodes: RouteNode[], root: RouteNode[]) => {
* cannot use a fully dynamic import statement
* ref: https://webpack.js.org/api/module-methods/#import-1
*/
- const pagePath = rn.page.replace('pages/', '');
- const Ctrl = lazy(() => import(`@/pages/${pagePath}`));
+
+ let Ctrl;
+
+ if (typeof rn.page === 'string') {
+ const pagePath = rn.page.replace('pages/', '');
+ Ctrl = lazy(() => import(`@/pages/${pagePath}`));
+ } else {
+ Ctrl = rn.page;
+ }
+
rn.element = (
<Suspense>
{rn.guard ? (
@@ -68,8 +76,8 @@ const routeWrapper = (routeNodes: RouteNode[], root: RouteNode[]) => {
}
});
};
+const mergedRoutes = mergeRoutePlugins(baseRoutes);
-routeWrapper(baseRoutes, routes);
-const mergedRoutes = mergeRoutePlugins(routes);
+routeWrapper(mergedRoutes, routes);
-export default mergedRoutes as RouteObject[];
+export default routes as RouteObject[];
diff --git a/ui/src/utils/pluginKit/index.ts b/ui/src/utils/pluginKit/index.ts
index 87851d80..95f14780 100644
--- a/ui/src/utils/pluginKit/index.ts
+++ b/ui/src/utils/pluginKit/index.ts
@@ -17,8 +17,6 @@
* under the License.
*/
-import React from 'react';
-
import builtin from '@/plugins/builtin';
import * as allPlugins from '@/plugins';
import type * as Type from '@/common/interface';
@@ -43,11 +41,14 @@ import { initI18nResource, Plugin, PluginInfo, PluginType } from './utils';
class Plugins {
plugins: Plugin[] = [];
+ registeredPlugins: Type.ActivatedPlugin[] = [];
+
constructor() {
this.registerBuiltin();
this.registerPlugins();
getPluginsStatus().then((plugins) => {
+ this.registeredPlugins = plugins;
this.activatePlugins(plugins);
});
}
@@ -158,6 +159,20 @@ const defaultProps = () => {
};
};
+const validateRoutePlugin = async (slugName) => {
+ let registeredPlugin;
+ if (plugins.registeredPlugins.length === 0) {
+ const pluginsStatus = await getPluginsStatus();
+ registeredPlugin = pluginsStatus.find((p) => p.slug_name === slugName);
+ } else {
+ registeredPlugin = plugins.registeredPlugins.find(
+ (p) => p.slug_name === slugName,
+ );
+ }
+
+ return Boolean(registeredPlugin?.enabled);
+};
+
const mergeRoutePlugins = (routes) => {
const routePlugins = getRoutePlugins();
if (routePlugins.length === 0) {
@@ -168,13 +183,28 @@ const mergeRoutePlugins = (routes) => {
route.children?.forEach((child) => {
if (child.page === 'pages/SideNavLayout') {
routePlugins.forEach((plugin) => {
- const { slug_name, route: path } = plugin.info;
- const Component = plugin.component;
-
+ const { route: path, slug_name } = plugin.info;
child.children.push({
- page: `plugin/${slug_name}`,
+ page: plugin.component,
path,
- element: React.createElement(Component, defaultProps(), null),
+ loader: async () => {
+ const bool = await validateRoutePlugin(slug_name);
+ return bool;
+ },
+ guard: (params) => {
+ if (params.loaderData) {
+ return {
+ ok: true,
+ };
+ }
+
+ return {
+ ok: false,
+ error: {
+ code: 404,
+ },
+ };
+ },
});
});
}