Skip to content

Commit

Permalink
fix(ui): route plugins register failed
Browse files Browse the repository at this point in the history
  • Loading branch information
ourai committed Oct 19, 2024
1 parent 916dba3 commit 2488f58
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 14 deletions.
29 changes: 26 additions & 3 deletions ui/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,37 @@
* under the License.
*/

import { RouterProvider, createBrowserRouter } from 'react-router-dom';
import {
type RouteObject,
RouterProvider,
createBrowserRouter,
} from 'react-router-dom';
import { useState, useEffect } from 'react';

import './i18n/init';

import '@/utils/pluginKit';
import routes from '@/router';
import { subscribe, unsubscribe } from '@/utils/pluginKit';
import resolveRoutes from '@/router';

function App() {
const [routes, setRoutes] = useState<RouteObject[]>([]);

useEffect(() => {
const callback = () => {
setRoutes(resolveRoutes());
};

subscribe('registered', callback);

return () => {
unsubscribe('registered', callback);
};
}, []);

if (routes.length === 0) {
return <div>initializing</div>;
}

const router = createBrowserRouter(routes, {
basename: process.env.REACT_APP_BASE_URL,
});
Expand Down
14 changes: 9 additions & 5 deletions ui/src/router/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ import baseRoutes, { RouteNode } from './routes';
import RouteGuard from './RouteGuard';
import RouteErrorBoundary from './RouteErrorBoundary';

const routes: RouteNode[] = [];

const routeWrapper = (routeNodes: RouteNode[], root: RouteNode[]) => {
routeNodes.forEach((rn) => {
if (rn.page === 'pages/Layout') {
Expand Down Expand Up @@ -76,8 +74,14 @@ const routeWrapper = (routeNodes: RouteNode[], root: RouteNode[]) => {
}
});
};
const mergedRoutes = mergeRoutePlugins(baseRoutes);

routeWrapper(mergedRoutes, routes);
function resolveRoutes(): RouteObject[] {
const routes: RouteNode[] = [];
const mergedRoutes = mergeRoutePlugins(baseRoutes);

routeWrapper(mergedRoutes, routes);

return routes as RouteObject[];
}

export default routes as RouteObject[];
export default resolveRoutes;
46 changes: 40 additions & 6 deletions ui/src/utils/pluginKit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,46 @@ import { Plugin, PluginInfo, PluginType } from './interface';
* @field description: Plugin description, optionally configurable. Usually read from the `i18n` file
*/

type EventName = string;
type EventHandler = () => void;

class Plugins {
plugins: Plugin[] = [];

registeredPlugins: Type.ActivatedPlugin[] = [];

events: Record<EventName, EventHandler[]> = {};

constructor() {
this.init();
}

on(name: EventName, handler: EventHandler) {
if (!this.events[name]) {
this.events[name] = [];
}

this.events[name].push(handler);
}

off(name: EventName, handler?: EventHandler) {
const handlers = this.events[name];

if (!handlers || handlers.length === 0) {
return;
}

if (handler) {
this.events[name] = handlers.filter((func) => func !== handler);
} else {
delete this.events[name];
}
}

trigger(name: EventName) {
(this.events[name] || []).forEach((handler) => handler());
}

init() {
this.registerBuiltin();

Expand Down Expand Up @@ -101,12 +132,10 @@ class Plugins {
return func;
})
.filter((p) => p);
return new Promise((resolve) => {
plugins.forEach(async (p) => {
const plugin = await p();
this.register(plugin);
});
resolve(true);
return Promise.all(plugins.map((p) => p())).then((resolvedPlugins) => {
resolvedPlugins.forEach((plugin) => this.register(plugin));
this.trigger('registered');
return true;
});
}

Expand Down Expand Up @@ -150,6 +179,9 @@ class Plugins {

const plugins = new Plugins();

const subscribe = plugins.on.bind(plugins);
const unsubscribe = plugins.off.bind(plugins);

const getRoutePlugins = () => {
return plugins
.getPlugins()
Expand Down Expand Up @@ -287,5 +319,7 @@ export {
useCaptchaPlugin,
useRenderPlugin,
PluginType,
subscribe,
unsubscribe,
};
export default plugins;

0 comments on commit 2488f58

Please sign in to comment.