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.

feat: Initial design of front-end plug-in structure completed

haitaood680820785bc
diff --git a/ui/src/common/interface.ts b/ui/src/common/interface.ts
index ac35596f..151a8c39 100644
--- a/ui/src/common/interface.ts
+++ b/ui/src/common/interface.ts
@@ -560,13 +560,10 @@ export interface OauthBindEmailReq {
must: boolean;
}
-export interface OauthConnectorItem {
+export interface UserOauthConnectorItem {
icon: string;
name: string;
link: string;
-}
-
-export interface UserOauthConnectorItem extends OauthConnectorItem {
binding: boolean;
external_id: string;
}
diff --git a/ui/src/components/PluginRender/index.tsx b/ui/src/components/PluginRender/index.tsx
new file mode 100644
index 00000000..ff68f567
--- /dev/null
+++ b/ui/src/components/PluginRender/index.tsx
@@ -0,0 +1,45 @@
+import { FC, ReactNode } from 'react';
+
+import builtin from '@/plugins/builtin';
+import * as plugins from '@/plugins';
+import { Plugin } from '@/utils/pluginKit';
+
+interface Props {
+ slug_name: string;
+ children?: ReactNode;
+ [prop: string]: any;
+}
+
+const findPluginBySlugName: (l, n) => Plugin | null = (source, slug_name) => {
+ let ret: Plugin | null = null;
+ if (source) {
+ Object.keys(source).forEach((k) => {
+ const p = source[k];
+ if (p && p.info && p.info.slug_name === slug_name && p.component) {
+ ret = p;
+ }
+ });
+ }
+
+ return ret;
+};
+
+const Index: FC<Props> = ({ slug_name, children, ...props }) => {
+ const bp = findPluginBySlugName(builtin, slug_name);
+ const vp = findPluginBySlugName(plugins, slug_name);
+ const plugin = bp || vp;
+
+ if (!plugin) {
+ return null;
+ }
+ /**
+ * TODO: Rendering control for non-builtin plug-ins
+ * ps: Logic such as version compatibility determination can be placed here
+ */
+
+ const PluginComponent = plugin.component;
+ // @ts-ignore
+ return <PluginComponent {...props}>{children}</PluginComponent>;
+};
+
+export default Index;
diff --git a/ui/src/components/index.ts b/ui/src/components/index.ts
index dfe906c5..8639d4d6 100644
--- a/ui/src/components/index.ts
+++ b/ui/src/components/index.ts
@@ -41,6 +41,7 @@ import HttpErrorContent from './HttpErrorContent';
import CustomSidebar from './CustomSidebar';
import ImgViewer from './ImgViewer';
import SideNav from './SideNav';
+import PluginRender from './PluginRender';
export {
Avatar,
@@ -88,5 +89,6 @@ export {
CustomSidebar,
ImgViewer,
SideNav,
+ PluginRender,
};
export type { EditorRef, JSONSchema, UISchema };
diff --git a/ui/src/i18n/init.ts b/ui/src/i18n/init.ts
index ad88c161..7f95398b 100644
--- a/ui/src/i18n/init.ts
+++ b/ui/src/i18n/init.ts
@@ -4,22 +4,42 @@ import i18next from 'i18next';
import en_US from '@i18n/en_US.yaml';
import zh_CN from '@i18n/zh_CN.yaml';
-import { DEFAULT_LANG } from '@/common/constants';
+import { DEFAULT_LANG, LANG_RESOURCE_STORAGE_KEY } from '@/common/constants';
+import Storage from '@/utils/storage';
+
+/**
+ * Prevent i18n from re-initialising when the page is refreshed and switching to `fallbackLng`.
+ */
+const initLng = i18next.resolvedLanguage || DEFAULT_LANG;
+const initResources = {
+ en_US: {
+ translation: en_US.ui,
+ },
+ zh_CN: {
+ translation: zh_CN.ui,
+ },
+};
+
+const storageLang = Storage.get(LANG_RESOURCE_STORAGE_KEY);
+if (
+ storageLang &&
+ storageLang.resources &&
+ storageLang.lng &&
+ storageLang.lng !== 'en_US' &&
+ storageLang.lng !== 'zh_CN'
+) {
+ initResources[storageLang.lng] = {
+ translation: storageLang.resources,
+ };
+}
i18next
// pass the i18n instance to react-i18next.
.use(initReactI18next)
.init({
- resources: {
- en_US: {
- translation: en_US.ui,
- },
- zh_CN: {
- translation: zh_CN.ui,
- },
- },
- // debug: process.env.NODE_ENV === 'development',
- fallbackLng: process.env.REACT_APP_LANG || DEFAULT_LANG,
+ resources: initResources,
+ lng: initLng,
+ fallbackLng: DEFAULT_LANG,
interpolation: {
escapeValue: false,
},
diff --git a/ui/src/pages/Users/Login/index.tsx b/ui/src/pages/Users/Login/index.tsx
index 84379676..060f1c14 100644
--- a/ui/src/pages/Users/Login/index.tsx
+++ b/ui/src/pages/Users/Login/index.tsx
@@ -9,8 +9,7 @@ import type {
ImgCodeRes,
FormDataType,
} from '@/common/interface';
-import { Unactivate, WelcomeTitle } from '@/components';
-import { PluginOauth, PluginUcLogin } from '@/plugins';
+import { Unactivate, WelcomeTitle, PluginRender } from '@/components';
import {
loggedUserInfoStore,
loginSettingStore,
@@ -172,15 +171,16 @@ const Index: React.FC = () => {
usePageTags({
title: t('login', { keyPrefix: 'page_title' }),
});
+
return (
<Container style={{ paddingTop: '4rem', paddingBottom: '5rem' }}>
<WelcomeTitle />
{step === 1 ? (
<Col className="mx-auto" md={6} lg={4} xl={3}>
{ucAgentInfo ? (
- <PluginUcLogin className="mb-5" />
+ <PluginRender slug_name="uc_login" className="mb-5" />
) : (
- <PluginOauth className="mb-5" />
+ <PluginRender slug_name="connector" className="mb-5" />
)}
{canOriginalLogin ? (
<>
diff --git a/ui/src/pages/Users/Register/index.tsx b/ui/src/pages/Users/Register/index.tsx
index d102d71a..712485ef 100644
--- a/ui/src/pages/Users/Register/index.tsx
+++ b/ui/src/pages/Users/Register/index.tsx
@@ -3,8 +3,7 @@ import { Container, Col } from 'react-bootstrap';
import { useTranslation } from 'react-i18next';
import { usePageTags } from '@/hooks';
-import { Unactivate, WelcomeTitle } from '@/components';
-import { PluginOauth } from '@/plugins';
+import { Unactivate, WelcomeTitle, PluginRender } from '@/components';
import { guard } from '@/utils';
import SignUpForm from './components/SignUpForm';
@@ -27,7 +26,7 @@ const Index: React.FC = () => {
{showForm ? (
<Col className="mx-auto" md={6} lg={4} xl={3}>
- <PluginOauth className="mb-5" />
+ <PluginRender slug_name="connector" className="mb-5" />
<SignUpForm callback={onStep} />
</Col>
) : (
diff --git a/ui/src/plugins/builtin/Connector/i18n/en_US.yaml b/ui/src/plugins/builtin/Connector/i18n/en_US.yaml
new file mode 100644
index 00000000..fe679444
--- /dev/null
+++ b/ui/src/plugins/builtin/Connector/i18n/en_US.yaml
@@ -0,0 +1,5 @@
+plugin:
+ connector:
+ ui:
+ connect: Connect with {{ auth_name }}
+ remove: Remove {{ auth_name }}
diff --git a/ui/src/plugins/builtin/Connector/i18n/index.ts b/ui/src/plugins/builtin/Connector/i18n/index.ts
new file mode 100644
index 00000000..b693440a
--- /dev/null
+++ b/ui/src/plugins/builtin/Connector/i18n/index.ts
@@ -0,0 +1,9 @@
+import pluginKit from '@/utils/pluginKit';
+
+import en_US from './en_US.yaml';
+import zh_CN from './zh_CN.yaml';
+
+pluginKit.initI18nResource({
+ en_US,
+ zh_CN,
+});
diff --git a/ui/src/plugins/builtin/Connector/i18n/zh_CN.yaml b/ui/src/plugins/builtin/Connector/i18n/zh_CN.yaml
new file mode 100644
index 00000000..aede9204
--- /dev/null
+++ b/ui/src/plugins/builtin/Connector/i18n/zh_CN.yaml
@@ -0,0 +1,6 @@
+plugin:
+ connector:
+ ui:
+ connect: 连接到 {{ auth_name }}
+ remove: 解绑 {{ auth_name }}
+
diff --git a/ui/src/plugins/PluginOauth/index.tsx b/ui/src/plugins/builtin/Connector/index.tsx
similarity index 64%
rename from ui/src/plugins/PluginOauth/index.tsx
rename to ui/src/plugins/builtin/Connector/index.tsx
index 40a28785..d07624e8 100644
--- a/ui/src/plugins/PluginOauth/index.tsx
+++ b/ui/src/plugins/builtin/Connector/index.tsx
@@ -4,14 +4,24 @@ import { useTranslation } from 'react-i18next';
import classnames from 'classnames';
-import { useGetStartUseOauthConnector } from '@/services';
+import pluginKit, { PluginInfo } from '@/utils/pluginKit';
import { SvgIcon } from '@/components';
+import info from './info.yaml';
+import { useGetStartUseOauthConnector } from './services';
+import './i18n';
+
+const pluginInfo: PluginInfo = {
+ slug_name: info.slug_name,
+};
interface Props {
className?: string;
}
const Index: FC<Props> = ({ className }) => {
- const { t } = useTranslation('translation', { keyPrefix: 'plugins.oauth' });
+ const { t } = useTranslation(pluginKit.getTransNs(), {
+ keyPrefix: pluginKit.getTransKeyPrefix(pluginInfo),
+ });
+
const { data } = useGetStartUseOauthConnector();
if (!data?.length) return null;
@@ -29,4 +39,7 @@ const Index: FC<Props> = ({ className }) => {
);
};
-export default memo(Index);
+export default {
+ info: pluginInfo,
+ component: memo(Index),
+};
diff --git a/ui/src/plugins/builtin/Connector/info.yaml b/ui/src/plugins/builtin/Connector/info.yaml
new file mode 100644
index 00000000..ba65c13e
--- /dev/null
+++ b/ui/src/plugins/builtin/Connector/info.yaml
@@ -0,0 +1,5 @@
+slug_name: connector
+version: 0.0.1
+link: https://github.com/answerdev/plugins/tree/main/connector/
+author: Answer.dev
+
diff --git a/ui/src/plugins/builtin/Connector/services.ts b/ui/src/plugins/builtin/Connector/services.ts
new file mode 100644
index 00000000..a1ca232d
--- /dev/null
+++ b/ui/src/plugins/builtin/Connector/services.ts
@@ -0,0 +1,21 @@
+import useSWR from 'swr';
+
+import request from '@/utils/request';
+
+export interface OauthConnectorItem {
+ icon: string;
+ name: string;
+ link: string;
+}
+
+export const useGetStartUseOauthConnector = () => {
+ const { data, error } = useSWR<OauthConnectorItem[]>(
+ '/answer/api/v1/connector/info',
+ request.instance.get,
+ );
+
+ return {
+ data,
+ error,
+ };
+};
diff --git a/ui/src/plugins/builtin/UcLogin/i18n/en_US.yaml b/ui/src/plugins/builtin/UcLogin/i18n/en_US.yaml
new file mode 100644
index 00000000..b61e3d64
--- /dev/null
+++ b/ui/src/plugins/builtin/UcLogin/i18n/en_US.yaml
@@ -0,0 +1,6 @@
+plugin:
+ uc_login:
+ ui:
+ login: Login
+ qrcode_login_tip: Please use {{ agentName }} to scan the QR code and log in.
+ login_failed_email_tip: Login failed, please allow this app to access your email information before try again.
diff --git a/ui/src/plugins/builtin/UcLogin/i18n/index.ts b/ui/src/plugins/builtin/UcLogin/i18n/index.ts
new file mode 100644
index 00000000..b693440a
--- /dev/null
+++ b/ui/src/plugins/builtin/UcLogin/i18n/index.ts
@@ -0,0 +1,9 @@
+import pluginKit from '@/utils/pluginKit';
+
+import en_US from './en_US.yaml';
+import zh_CN from './zh_CN.yaml';
+
+pluginKit.initI18nResource({
+ en_US,
+ zh_CN,
+});
diff --git a/ui/src/plugins/builtin/UcLogin/i18n/zh_CN.yaml b/ui/src/plugins/builtin/UcLogin/i18n/zh_CN.yaml
new file mode 100644
index 00000000..d8956ef3
--- /dev/null
+++ b/ui/src/plugins/builtin/UcLogin/i18n/zh_CN.yaml
@@ -0,0 +1,6 @@
+plugin:
+ uc_login:
+ ui:
+ login: 登录
+ qrcode_login_tip: 请使用 {{ agentName }} 扫描二维码登录
+ login_failed_email_tip: 登录失败, 请允许该应用程序访问您的电子邮件信息,然后再试一次。
diff --git a/ui/src/plugins/PluginUcLogin/index.tsx b/ui/src/plugins/builtin/UcLogin/index.tsx
similarity index 71%
rename from ui/src/plugins/PluginUcLogin/index.tsx
rename to ui/src/plugins/builtin/UcLogin/index.tsx
index b6dceb24..4e2f8c66 100644
--- a/ui/src/plugins/PluginUcLogin/index.tsx
+++ b/ui/src/plugins/builtin/UcLogin/index.tsx
@@ -4,14 +4,25 @@ import { useTranslation } from 'react-i18next';
import classnames from 'classnames';
+import pluginKit, { PluginInfo } from '@/utils/pluginKit';
import { SvgIcon } from '@/components';
import { userCenterStore } from '@/stores';
+import './i18n';
+
+import info from './info.yaml';
interface Props {
className?: classnames.Argument;
}
+
+const pluginInfo: PluginInfo = {
+ slug_name: info.slug_name,
+};
+
const Index: FC<Props> = ({ className }) => {
- const { t } = useTranslation('translation', { keyPrefix: 'plugins.oauth' });
+ const { t } = useTranslation(pluginKit.getTransNs(), {
+ keyPrefix: pluginKit.getTransKeyPrefix(pluginInfo),
+ });
const ucAgent = userCenterStore().agent;
const ucLoginRedirect =
ucAgent?.enabled && ucAgent?.agent_info?.login_redirect_url;
@@ -31,5 +42,7 @@ const Index: FC<Props> = ({ className }) => {
}
return null;
};
-
-export default memo(Index);
+export default {
+ info: pluginInfo,
+ component: memo(Index),
+};
diff --git a/ui/src/plugins/builtin/UcLogin/info.yaml b/ui/src/plugins/builtin/UcLogin/info.yaml
new file mode 100644
index 00000000..d181d870
--- /dev/null
+++ b/ui/src/plugins/builtin/UcLogin/info.yaml
@@ -0,0 +1,4 @@
+slug_name: uc_login
+version: 0.0.1
+author: Answer.dev
+
diff --git a/ui/src/plugins/builtin/index.ts b/ui/src/plugins/builtin/index.ts
new file mode 100644
index 00000000..95c102e1
--- /dev/null
+++ b/ui/src/plugins/builtin/index.ts
@@ -0,0 +1,7 @@
+import Connector from './Connector';
+import UcLogin from './UcLogin';
+
+export default {
+ Connector,
+ UcLogin,
+};
diff --git a/ui/src/plugins/index.ts b/ui/src/plugins/index.ts
index 09d25445..7646bbd1 100644
--- a/ui/src/plugins/index.ts
+++ b/ui/src/plugins/index.ts
@@ -1,4 +1 @@
-import PluginOauth from './PluginOauth';
-import PluginUcLogin from './PluginUcLogin';
-
-export { PluginOauth, PluginUcLogin };
+export default null;
diff --git a/ui/src/plugins/types.ts b/ui/src/plugins/types.ts
deleted file mode 100644
index faca6ad0..00000000
--- a/ui/src/plugins/types.ts
+++ /dev/null
@@ -1,23 +0,0 @@
-import { UIOptions, UIWidget } from '@/components/SchemaForm';
-
-export interface PluginOption {
- label: string;
- value: string;
-}
-
-export interface PluginItem {
- name: string;
- type: UIWidget;
- title: string;
- description: string;
- ui_options?: UIOptions;
- options?: PluginOption[];
- value?: string;
- required?: boolean;
-}
-
-export interface PluginConfig {
- name: string;
- slug_name: string;
- config_fields: PluginItem[];
-}
diff --git a/ui/src/services/admin/plugins.ts b/ui/src/services/admin/plugins.ts
index e58acb02..178fd1af 100644
--- a/ui/src/services/admin/plugins.ts
+++ b/ui/src/services/admin/plugins.ts
@@ -2,7 +2,29 @@ import qs from 'qs';
import useSWR from 'swr';
import request from '@/utils/request';
-import type { PluginConfig } from '@/plugins/types';
+import { UIOptions, UIWidget } from '@/components/SchemaForm';
+
+export interface PluginOption {
+ label: string;
+ value: string;
+}
+
+export interface PluginItem {
+ name: string;
+ type: UIWidget;
+ title: string;
+ description: string;
+ ui_options?: UIOptions;
+ options?: PluginOption[];
+ value?: string;
+ required?: boolean;
+}
+
+export interface PluginConfig {
+ name: string;
+ slug_name: string;
+ config_fields: PluginItem[];
+}
export const useQueryPlugins = (params) => {
const apiUrl = `/answer/admin/api/plugins?${qs.stringify(params)}`;
diff --git a/ui/src/services/client/Oauth.ts b/ui/src/services/client/Oauth.ts
index b02e88c8..7474757f 100644
--- a/ui/src/services/client/Oauth.ts
+++ b/ui/src/services/client/Oauth.ts
@@ -23,14 +23,3 @@ export const useOauthConnectorInfoByUser = () => {
export const userOauthUnbind = (data: { external_id: string }) => {
return request.delete('/answer/api/v1/connector/user/unbinding', data);
};
-
-export const useGetStartUseOauthConnector = () => {
- const { data, error } = useSWR<Type.OauthConnectorItem[]>(
- '/answer/api/v1/connector/info',
- request.instance.get,
- );
- return {
- data,
- error,
- };
-};
diff --git a/ui/src/utils/localize.ts b/ui/src/utils/localize.ts
index 775896e8..ae5fe479 100644
--- a/ui/src/utils/localize.ts
+++ b/ui/src/utils/localize.ts
@@ -17,6 +17,9 @@ import {
import Storage from './storage';
+/**
+ * localize kit for i18n
+ */
export const loadLanguageOptions = async (forAdmin = false) => {
const languageOptions = forAdmin
? await getAdminLanguageOptions()
@@ -68,13 +71,6 @@ const addI18nResource = async (langName) => {
}
};
-dayjs.extend(utc);
-dayjs.extend(timezone);
-const localeDayjs = (langName) => {
- langName = langName.replace('_', '-').toLowerCase();
- dayjs.locale(langName);
-};
-
export const getCurrentLang = () => {
const loggedUser = loggedUserInfoStore.getState().user;
const adminInterface = interfaceStore.getState().interface;
@@ -88,6 +84,16 @@ export const getCurrentLang = () => {
return currentLang;
};
+/**
+ * localize for Day.js
+ */
+dayjs.extend(utc);
+dayjs.extend(timezone);
+const localeDayjs = (langName) => {
+ langName = langName.replace('_', '-').toLowerCase();
+ dayjs.locale(langName);
+};
+
export const setupAppLanguage = async () => {
const lang = getCurrentLang();
if (!i18next.getDataByLanguage(lang)) {
diff --git a/ui/src/utils/pluginKit.ts b/ui/src/utils/pluginKit.ts
new file mode 100644
index 00000000..f65d3bf1
--- /dev/null
+++ b/ui/src/utils/pluginKit.ts
@@ -0,0 +1,73 @@
+import { NamedExoticComponent, FC } from 'react';
+
+import i18next from 'i18next';
+
+/**
+ * This information is to be defined for all components.
+ * It may be used for feature upgrades or version compatibility processing.
+ *
+ * @field slug_name: Unique identity string for the plugin, usually configured in `info.yaml`
+ * @field name: Plugin name, optionally configurable. Usually read from the `i18n` file
+ * @field description: Plugin description, optionally configurable. Usually read from the `i18n` file
+ */
+
+const I18N_NS = 'plugin';
+
+export interface PluginInfo {
+ slug_name: string;
+ name?: string;
+ description?: string;
+}
+
+export interface Plugin {
+ info: PluginInfo;
+ component: NamedExoticComponent | FC;
+}
+
+interface I18nResource {
+ [lng: string]: {
+ plugin: {
+ [slug_name: string]: {
+ ui: any;
+ };
+ };
+ };
+}
+
+const addResourceBundle = (resource: I18nResource) => {
+ if (resource) {
+ Object.keys(resource).forEach((lng) => {
+ const r = resource[lng];
+
+ i18next.addResourceBundle(lng, I18N_NS, r.plugin, true, true);
+ });
+ }
+};
+
+const initI18nResource = (resource: I18nResource) => {
+ addResourceBundle(resource);
+ /**
+ * Note: In development mode,
+ * when the base i18n file is changed, `i18next` will reinitialise the updated resource file,
+ * which will cause the resource package added in the plugin to be lost
+ * and will need to be automatically re-added by listening for events
+ */
+ i18next.on('initialized', () => {
+ addResourceBundle(resource);
+ });
+};
+
+const getTransNs = () => {
+ return I18N_NS;
+};
+
+const getTransKeyPrefix = (info: PluginInfo) => {
+ const kp = `${info.slug_name}.ui`;
+ return kp;
+};
+
+export default {
+ initI18nResource,
+ getTransNs,
+ getTransKeyPrefix,
+};