Skip to content

Commit

Permalink
Merge branch 'dev' into test
Browse files Browse the repository at this point in the history
  • Loading branch information
LinkinStars committed Oct 22, 2024
2 parents 5fd09d8 + 895c1c4 commit 0c5eb64
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 49 deletions.
7 changes: 6 additions & 1 deletion ui/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@ import { RouterProvider, createBrowserRouter } from 'react-router-dom';
import './i18n/init';

import '@/utils/pluginKit';
import routes from '@/router';
import { useMergeRoutes } from '@/router';
import InitialLoadingPlaceholder from '@/components/InitialLoadingPlaceholder';

function App() {
const routes = useMergeRoutes();
if (routes.length === 0) {
return <InitialLoadingPlaceholder />;
}
const router = createBrowserRouter(routes, {
basename: process.env.REACT_APP_BASE_URL,
});
Expand Down
35 changes: 35 additions & 0 deletions ui/src/components/InitialLoadingPlaceholder/index.scss
Original file line number Diff line number Diff line change
@@ -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;
}
}
15 changes: 15 additions & 0 deletions ui/src/components/InitialLoadingPlaceholder/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Same as spin in `public/index.html`

import './index.scss';

function InitialLoadingPlaceholder() {
return (
<div className="InitialLoadingPlaceholder">
<div className="InitialLoadingPlaceholder-spinnerContainer">
<div className="InitialLoadingPlaceholder-spinner" />
</div>
</div>
);
}

export default InitialLoadingPlaceholder;
34 changes: 19 additions & 15 deletions ui/src/components/TagSelector/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
*/

/* eslint-disable no-nested-ternary */
import { FC, useState, useEffect, useRef } from 'react';
import { FC, useState, useEffect, useRef, useCallback } from 'react';
import { Dropdown, Button, Form } from 'react-bootstrap';
import { useTranslation } from 'react-i18next';

import debounce from 'lodash/debounce';
import { marked } from 'marked';
import classNames from 'classnames';

Expand Down Expand Up @@ -69,7 +70,7 @@ const TagSelector: FC<IProps> = ({
const [currentIndex, setCurrentIndex] = useState<number>(0);
const [repeatIndex, setRepeatIndex] = useState(-1);
const [searchValue, setSearchValue] = useState<string>('');
const [tags, setTags] = useState<Type.Tag[] | null>(null);
const [tags, setTags] = useState<Type.Tag[] | null>([]);
const [requiredTags, setRequiredTags] = useState<Type.Tag[] | null>(null);
const { t } = useTranslation('translation', { keyPrefix: 'tag_selector' });
const { data: userPermission } = useUserPermission('tag.add');
Expand Down Expand Up @@ -146,20 +147,23 @@ const TagSelector: FC<IProps> = ({
handleMenuShow(false);
};

const fetchTags = (str) => {
if (!showRequiredTag && !str) {
setTags([]);
return;
}
queryTags(str).then((res) => {
const tagArray: Type.Tag[] = filterTags(res || []);
if (str === '') {
setRequiredTags(res);
const fetchTags = useCallback(
debounce((str) => {
if (!showRequiredTag && !str) {
setTags([]);
return;
}
handleMenuShow(tagArray.length > 0);
setTags(tagArray);
});
};
queryTags(str).then((res) => {
const tagArray: Type.Tag[] = filterTags(res || []);
if (str === '') {
setRequiredTags(res);
}
handleMenuShow(tagArray.length > 0);
setTags(tagArray);
});
}, 400),
[],
);

const resetSearch = () => {
setCurrentIndex(0);
Expand Down
24 changes: 18 additions & 6 deletions ui/src/router/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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,22 @@ const routeWrapper = (routeNodes: RouteNode[], root: RouteNode[]) => {
}
});
};
const mergedRoutes = mergeRoutePlugins(baseRoutes);

routeWrapper(mergedRoutes, routes);
function useMergeRoutes() {
const [routesState, setRoutes] = useState<RouteObject[]>([]);

const init = async () => {
const routes = [];
const mergedRoutes = await mergeRoutePlugins(baseRoutes).catch(() => []);
routeWrapper(mergedRoutes, routes);
setRoutes(routes);
};

useEffect(() => {
init();
}, []);

return routesState;
}

export default routes as RouteObject[];
export { useMergeRoutes };
41 changes: 14 additions & 27 deletions ui/src/utils/pluginKit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,18 @@ class Plugins {

registeredPlugins: Type.ActivatedPlugin[] = [];

initialization: Promise<void>;

constructor() {
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() {
Expand Down Expand Up @@ -101,12 +102,9 @@ 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));
return true;
});
}

Expand All @@ -122,18 +120,6 @@ class Plugins {
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);
}
Expand All @@ -150,7 +136,8 @@ class Plugins {

const plugins = new Plugins();

const getRoutePlugins = () => {
const getRoutePlugins = async () => {
await plugins.initialization;
return plugins
.getPlugins()
.filter((plugin) => plugin.info.type === PluginType.Route);
Expand Down Expand Up @@ -180,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;
}
Expand Down

0 comments on commit 0c5eb64

Please sign in to comment.