You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@answer.apache.org by ro...@apache.org on 2024/05/31 09:40:32 UTC
(incubator-answer) branch fix/plugin-route created (now c4a05166)
This is an automated email from the ASF dual-hosted git repository.
robin0716 pushed a change to branch fix/plugin-route
in repository https://gitbox.apache.org/repos/asf/incubator-answer.git
at c4a05166 refactor: According to the status of the plug-in, determine whether the routing plug-in is enabled
This branch includes the following new commits:
new 9d1881fe refactor: Remove 'name' property from ActivatedPlugin interface and add 'enabled' property
new c4a05166 refactor: According to the status of the plug-in, determine whether the routing plug-in is enabled
The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
(incubator-answer) 01/02: refactor: Remove 'name' property from ActivatedPlugin interface and add 'enabled' property
Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
robin0716 pushed a commit to branch fix/plugin-route
in repository https://gitbox.apache.org/repos/asf/incubator-answer.git
commit 9d1881fe5ff4b2b00ce6a4536c90feb4462bbb93
Author: robin <ro...@rnode.me>
AuthorDate: Fri May 31 17:35:48 2024 +0800
refactor: Remove 'name' property from ActivatedPlugin interface and add 'enabled' property
---
ui/src/common/interface.ts | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/ui/src/common/interface.ts b/ui/src/common/interface.ts
index 700746b8..83ec1fd7 100644
--- a/ui/src/common/interface.ts
+++ b/ui/src/common/interface.ts
@@ -687,8 +687,8 @@ export interface NotificationConfig {
}
export interface ActivatedPlugin {
- name: string;
slug_name: string;
+ enabled: boolean;
}
export interface UserPluginsConfigRes {
(incubator-answer) 02/02: refactor: According to the status of the plug-in, determine whether the routing plug-in is enabled
Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
robin0716 pushed a commit to branch fix/plugin-route
in repository https://gitbox.apache.org/repos/asf/incubator-answer.git
commit c4a051668e56256adc467ea756ad1569a955195e
Author: robin <ro...@rnode.me>
AuthorDate: Fri May 31 17:40:11 2024 +0800
refactor: According to the status of the plug-in, determine whether the routing plug-in is enabled
---
ui/src/router/RouteGuard.tsx | 8 ++++----
ui/src/router/index.tsx | 18 ++++++++++++-----
ui/src/utils/pluginKit/index.ts | 44 ++++++++++++++++++++++++++++++++++-------
3 files changed, 54 insertions(+), 16 deletions(-)
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,
+ },
+ };
+ },
});
});
}