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/10/22 06:22:23 UTC
(incubator-answer) branch dev updated (5f28f106 -> 9fa6ef68)
This is an automated email from the ASF dual-hosted git repository.
robin0716 pushed a change to branch dev
in repository https://gitbox.apache.org/repos/asf/incubator-answer.git
from 5f28f106 refactor(router): comment out UI index file check for dynamic routing
new c0a1f4e5 fix(ui): route plugins register failed
new 49033820 refactor(ui): extract event logic of plugin kit out
new 6e51cc75 refactor(ui): optimize initial loading
new 8753716c refactor(ui): optimize initial loading and extract event logic of plugin kit out
new 9fa6ef68 chore(ui): copy style from `public/index.html` to keep consistency
The 5 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.
Summary of changes:
ui/src/App.tsx | 7 +++-
.../InitialLoadingPlaceholder/index.scss | 35 ++++++++++++++++++
.../components/InitialLoadingPlaceholder/index.tsx | 15 ++++++++
ui/src/router/index.tsx | 24 +++++++++----
ui/src/utils/pluginKit/index.ts | 41 ++++++++--------------
5 files changed, 88 insertions(+), 34 deletions(-)
create mode 100644 ui/src/components/InitialLoadingPlaceholder/index.scss
create mode 100644 ui/src/components/InitialLoadingPlaceholder/index.tsx
(incubator-answer) 05/05: chore(ui): copy style from `public/index.html` to keep consistency
Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
robin0716 pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/incubator-answer.git
commit 9fa6ef6851895a52f18535aafd006d0192aafe5d
Author: Ourai Lin <ou...@gmail.com>
AuthorDate: Tue Oct 22 11:32:52 2024 +0800
chore(ui): copy style from `public/index.html` to keep consistency
---
.../InitialLoadingPlaceholder/index.scss | 35 ++++++++++++++++++++++
.../components/InitialLoadingPlaceholder/index.tsx | 17 +++++------
2 files changed, 42 insertions(+), 10 deletions(-)
diff --git a/ui/src/components/InitialLoadingPlaceholder/index.scss b/ui/src/components/InitialLoadingPlaceholder/index.scss
new file mode 100644
index 00000000..2b73f163
--- /dev/null
+++ b/ui/src/components/InitialLoadingPlaceholder/index.scss
@@ -0,0 +1,35 @@
+// Same as spin in `public/index.html`
+
+@keyframes _initial-loading-spin {
+ to { transform: rotate(360deg) }
+}
+
+.InitialLoadingPlaceholder {
+ position: fixed;
+ top: 0;
+ right: 0;
+ bottom: 0;
+ left: 0;
+ background-color: white;
+ z-index: 9999;
+
+ &-spinnerContainer {
+ position: absolute;
+ top: 50%;
+ left: 50%;
+ transform: translate(-50%, -50%);
+ }
+
+ &-spinner {
+ box-sizing: border-box;
+ display: inline-block;
+ width: 2rem;
+ height: 2rem;
+ vertical-align: -.125em;
+ border: .25rem solid currentColor;
+ border-right-color: transparent;
+ color: rgba(108, 117, 125, .75);
+ border-radius: 50%;
+ animation: 0.75s linear infinite _initial-loading-spin;
+ }
+}
diff --git a/ui/src/components/InitialLoadingPlaceholder/index.tsx b/ui/src/components/InitialLoadingPlaceholder/index.tsx
index 28ba1cfd..41e81d1a 100644
--- a/ui/src/components/InitialLoadingPlaceholder/index.tsx
+++ b/ui/src/components/InitialLoadingPlaceholder/index.tsx
@@ -1,16 +1,13 @@
-import { Spinner } from 'react-bootstrap';
+// Same as spin in `public/index.html`
+
+import './index.scss';
function InitialLoadingPlaceholder() {
return (
- <div
- style={{
- flexGrow: 1,
- display: 'flex',
- alignItems: 'center',
- justifyContent: 'center',
- }}>
- <Spinner />
- <span style={{ marginLeft: 8 }}>Initializing</span>
+ <div className="InitialLoadingPlaceholder">
+ <div className="InitialLoadingPlaceholder-spinnerContainer">
+ <div className="InitialLoadingPlaceholder-spinner" />
+ </div>
</div>
);
}
(incubator-answer) 01/05: fix(ui): route plugins register failed
Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
robin0716 pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/incubator-answer.git
commit c0a1f4e5cd816d739f1eb98ec9ca0294008165b3
Author: Ourai Lin <ou...@gmail.com>
AuthorDate: Sat Oct 19 20:25:10 2024 +0800
fix(ui): route plugins register failed
---
ui/src/App.tsx | 29 +++++++++++++++++++++++---
ui/src/router/index.tsx | 14 ++++++++-----
ui/src/utils/pluginKit/index.ts | 46 +++++++++++++++++++++++++++++++++++------
3 files changed, 75 insertions(+), 14 deletions(-)
diff --git a/ui/src/App.tsx b/ui/src/App.tsx
index affac7a9..1b7d1b82 100644
--- a/ui/src/App.tsx
+++ b/ui/src/App.tsx
@@ -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,
});
diff --git a/ui/src/router/index.tsx b/ui/src/router/index.tsx
index 432cb581..8c111e0f 100644
--- a/ui/src/router/index.tsx
+++ b/ui/src/router/index.tsx
@@ -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') {
@@ -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;
diff --git a/ui/src/utils/pluginKit/index.ts b/ui/src/utils/pluginKit/index.ts
index 66d5c376..43b0d691 100644
--- a/ui/src/utils/pluginKit/index.ts
+++ b/ui/src/utils/pluginKit/index.ts
@@ -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();
@@ -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;
});
}
@@ -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()
@@ -287,5 +319,7 @@ export {
useCaptchaPlugin,
useRenderPlugin,
PluginType,
+ subscribe,
+ unsubscribe,
};
export default plugins;
(incubator-answer) 03/05: refactor(ui): optimize initial loading
Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
robin0716 pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/incubator-answer.git
commit 6e51cc756df7313732ad65400a0a533e7f4582e5
Author: Ourai Lin <ou...@gmail.com>
AuthorDate: Sun Oct 20 12:13:23 2024 +0800
refactor(ui): optimize initial loading
---
ui/src/App.tsx | 11 +++++++++--
ui/src/components/InitialLoadingPlaceholder/index.tsx | 18 ++++++++++++++++++
2 files changed, 27 insertions(+), 2 deletions(-)
diff --git a/ui/src/App.tsx b/ui/src/App.tsx
index 1b7d1b82..f799042a 100644
--- a/ui/src/App.tsx
+++ b/ui/src/App.tsx
@@ -28,8 +28,9 @@ import './i18n/init';
import { subscribe, unsubscribe } from '@/utils/pluginKit';
import resolveRoutes from '@/router';
+import InitialLoadingPlaceholder from '@/components/InitialLoadingPlaceholder';
-function App() {
+function useResolvedRoutes() {
const [routes, setRoutes] = useState<RouteObject[]>([]);
useEffect(() => {
@@ -44,8 +45,14 @@ function App() {
};
}, []);
+ return routes;
+}
+
+function App() {
+ const routes = useResolvedRoutes();
+
if (routes.length === 0) {
- return <div>initializing</div>;
+ return <InitialLoadingPlaceholder />;
}
const router = createBrowserRouter(routes, {
diff --git a/ui/src/components/InitialLoadingPlaceholder/index.tsx b/ui/src/components/InitialLoadingPlaceholder/index.tsx
new file mode 100644
index 00000000..28ba1cfd
--- /dev/null
+++ b/ui/src/components/InitialLoadingPlaceholder/index.tsx
@@ -0,0 +1,18 @@
+import { Spinner } from 'react-bootstrap';
+
+function InitialLoadingPlaceholder() {
+ return (
+ <div
+ style={{
+ flexGrow: 1,
+ display: 'flex',
+ alignItems: 'center',
+ justifyContent: 'center',
+ }}>
+ <Spinner />
+ <span style={{ marginLeft: 8 }}>Initializing</span>
+ </div>
+ );
+}
+
+export default InitialLoadingPlaceholder;
(incubator-answer) 04/05: refactor(ui): optimize initial loading and extract event logic of plugin kit out
Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
robin0716 pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/incubator-answer.git
commit 8753716ceb789d59effc5c3613d0215775c225ec
Author: robin <ro...@rnode.me>
AuthorDate: Mon Oct 21 16:19:32 2024 +0800
refactor(ui): optimize initial loading and extract event logic of plugin kit out
---
ui/src/App.tsx | 33 ++++--------------------------
ui/src/router/index.tsx | 22 +++++++++++++-------
ui/src/utils/pluginKit/emitter.ts | 34 -------------------------------
ui/src/utils/pluginKit/index.ts | 42 +++++++++++----------------------------
4 files changed, 31 insertions(+), 100 deletions(-)
diff --git a/ui/src/App.tsx b/ui/src/App.tsx
index f799042a..8c425f29 100644
--- a/ui/src/App.tsx
+++ b/ui/src/App.tsx
@@ -17,44 +17,19 @@
* under the License.
*/
-import {
- type RouteObject,
- RouterProvider,
- createBrowserRouter,
-} from 'react-router-dom';
-import { useState, useEffect } from 'react';
+import { RouterProvider, createBrowserRouter } from 'react-router-dom';
import './i18n/init';
-import { subscribe, unsubscribe } from '@/utils/pluginKit';
-import resolveRoutes from '@/router';
+import '@/utils/pluginKit';
+import { useMergeRoutes } from '@/router';
import InitialLoadingPlaceholder from '@/components/InitialLoadingPlaceholder';
-function useResolvedRoutes() {
- const [routes, setRoutes] = useState<RouteObject[]>([]);
-
- useEffect(() => {
- const callback = () => {
- setRoutes(resolveRoutes());
- };
-
- subscribe('registered', callback);
-
- return () => {
- unsubscribe('registered', callback);
- };
- }, []);
-
- return routes;
-}
-
function App() {
- const routes = useResolvedRoutes();
-
+ const routes = useMergeRoutes();
if (routes.length === 0) {
return <InitialLoadingPlaceholder />;
}
-
const router = createBrowserRouter(routes, {
basename: process.env.REACT_APP_BASE_URL,
});
diff --git a/ui/src/router/index.tsx b/ui/src/router/index.tsx
index 8c111e0f..ea2fdf3c 100644
--- a/ui/src/router/index.tsx
+++ b/ui/src/router/index.tsx
@@ -17,7 +17,7 @@
* under the License.
*/
-import { Suspense, lazy } from 'react';
+import { Suspense, lazy, useEffect, useState } from 'react';
import { RouteObject } from 'react-router-dom';
import Layout from '@/pages/Layout';
@@ -75,13 +75,21 @@ const routeWrapper = (routeNodes: RouteNode[], root: RouteNode[]) => {
});
};
-function resolveRoutes(): RouteObject[] {
- const routes: RouteNode[] = [];
- const mergedRoutes = mergeRoutePlugins(baseRoutes);
+function useMergeRoutes() {
+ const [routesState, setRoutes] = useState<RouteObject[]>([]);
- routeWrapper(mergedRoutes, routes);
+ const init = async () => {
+ const routes = [];
+ const mergedRoutes = await mergeRoutePlugins(baseRoutes).catch(() => []);
+ routeWrapper(mergedRoutes, routes);
+ setRoutes(routes);
+ };
- return routes as RouteObject[];
+ useEffect(() => {
+ init();
+ }, []);
+
+ return routesState;
}
-export default resolveRoutes;
+export { useMergeRoutes };
diff --git a/ui/src/utils/pluginKit/emitter.ts b/ui/src/utils/pluginKit/emitter.ts
deleted file mode 100644
index eee427b6..00000000
--- a/ui/src/utils/pluginKit/emitter.ts
+++ /dev/null
@@ -1,34 +0,0 @@
-type EventName = string;
-type EventHandler = () => void;
-
-class SimpleEventEmitter {
- events: Record<EventName, EventHandler[]> = {};
-
- 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];
- }
- }
-
- emit(name: EventName) {
- (this.events[name] || []).forEach((handler) => handler());
- }
-}
-
-export default SimpleEventEmitter;
diff --git a/ui/src/utils/pluginKit/index.ts b/ui/src/utils/pluginKit/index.ts
index 39d1edb5..346c1456 100644
--- a/ui/src/utils/pluginKit/index.ts
+++ b/ui/src/utils/pluginKit/index.ts
@@ -29,7 +29,6 @@ import request from '@/utils/request';
import { initI18nResource } from './utils';
import { Plugin, PluginInfo, PluginType } from './interface';
-import SimpleEventEmitter from './emitter';
/**
* This information is to be defined for all components.
@@ -43,23 +42,23 @@ import SimpleEventEmitter from './emitter';
* @field description: Plugin description, optionally configurable. Usually read from the `i18n` file
*/
-class Plugins extends SimpleEventEmitter {
+class Plugins {
plugins: Plugin[] = [];
registeredPlugins: Type.ActivatedPlugin[] = [];
+ initialization: Promise<void>;
+
constructor() {
- super();
- this.init();
+ this.initialization = this.init();
}
- init() {
+ async init() {
this.registerBuiltin();
- getPluginsStatus().then((plugins) => {
- this.registeredPlugins = plugins.filter((p) => p.enabled);
- this.registerPlugins();
- });
+ const plugins = await getPluginsStatus().catch(() => []);
+ this.registeredPlugins = plugins.filter((p) => p.enabled);
+ await this.registerPlugins();
}
refresh() {
@@ -105,7 +104,6 @@ class Plugins extends SimpleEventEmitter {
.filter((p) => p);
return Promise.all(plugins.map((p) => p())).then((resolvedPlugins) => {
resolvedPlugins.forEach((plugin) => this.register(plugin));
- this.emit('registered');
return true;
});
}
@@ -122,18 +120,6 @@ class Plugins extends SimpleEventEmitter {
this.plugins.push(plugin);
}
- activatePlugins(activatedPlugins: Type.ActivatedPlugin[]) {
- this.plugins.forEach((plugin: any) => {
- const { slug_name } = plugin.info;
- const activatedPlugin: any = activatedPlugins?.find(
- (p) => p.slug_name === slug_name,
- );
- if (activatedPlugin) {
- plugin.activated = activatedPlugin?.enabled;
- }
- });
- }
-
getPlugin(slug_name: string) {
return this.plugins.find((p) => p.info.slug_name === slug_name);
}
@@ -150,10 +136,8 @@ class Plugins extends SimpleEventEmitter {
const plugins = new Plugins();
-const subscribe = plugins.on.bind(plugins);
-const unsubscribe = plugins.off.bind(plugins);
-
-const getRoutePlugins = () => {
+const getRoutePlugins = async () => {
+ await plugins.initialization;
return plugins
.getPlugins()
.filter((plugin) => plugin.info.type === PluginType.Route);
@@ -183,8 +167,8 @@ const validateRoutePlugin = async (slugName) => {
return Boolean(registeredPlugin?.enabled);
};
-const mergeRoutePlugins = (routes) => {
- const routePlugins = getRoutePlugins();
+const mergeRoutePlugins = async (routes) => {
+ const routePlugins = await getRoutePlugins();
if (routePlugins.length === 0) {
return routes;
}
@@ -290,7 +274,5 @@ export {
useCaptchaPlugin,
useRenderPlugin,
PluginType,
- subscribe,
- unsubscribe,
};
export default plugins;
(incubator-answer) 02/05: refactor(ui): extract event logic of plugin kit out
Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
robin0716 pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/incubator-answer.git
commit 49033820568b81fdb1d860fa631417f451b0ff4c
Author: Ourai Lin <ou...@gmail.com>
AuthorDate: Sun Oct 20 11:40:47 2024 +0800
refactor(ui): extract event logic of plugin kit out
---
ui/src/utils/pluginKit/emitter.ts | 34 ++++++++++++++++++++++++++++++++++
ui/src/utils/pluginKit/index.ts | 37 ++++---------------------------------
2 files changed, 38 insertions(+), 33 deletions(-)
diff --git a/ui/src/utils/pluginKit/emitter.ts b/ui/src/utils/pluginKit/emitter.ts
new file mode 100644
index 00000000..eee427b6
--- /dev/null
+++ b/ui/src/utils/pluginKit/emitter.ts
@@ -0,0 +1,34 @@
+type EventName = string;
+type EventHandler = () => void;
+
+class SimpleEventEmitter {
+ events: Record<EventName, EventHandler[]> = {};
+
+ 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];
+ }
+ }
+
+ emit(name: EventName) {
+ (this.events[name] || []).forEach((handler) => handler());
+ }
+}
+
+export default SimpleEventEmitter;
diff --git a/ui/src/utils/pluginKit/index.ts b/ui/src/utils/pluginKit/index.ts
index 43b0d691..39d1edb5 100644
--- a/ui/src/utils/pluginKit/index.ts
+++ b/ui/src/utils/pluginKit/index.ts
@@ -29,6 +29,7 @@ import request from '@/utils/request';
import { initI18nResource } from './utils';
import { Plugin, PluginInfo, PluginType } from './interface';
+import SimpleEventEmitter from './emitter';
/**
* This information is to be defined for all components.
@@ -42,46 +43,16 @@ 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 {
+class Plugins extends SimpleEventEmitter {
plugins: Plugin[] = [];
registeredPlugins: Type.ActivatedPlugin[] = [];
- events: Record<EventName, EventHandler[]> = {};
-
constructor() {
+ super();
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();
@@ -134,7 +105,7 @@ class Plugins {
.filter((p) => p);
return Promise.all(plugins.map((p) => p())).then((resolvedPlugins) => {
resolvedPlugins.forEach((plugin) => this.register(plugin));
- this.trigger('registered');
+ this.emit('registered');
return true;
});
}