From 64070b81b9a388fe1bcc3adb585cdaa59495f244 Mon Sep 17 00:00:00 2001 From: chenos Date: Wed, 21 Jun 2023 11:02:49 +0800 Subject: [PATCH] refactor: migrate adminSchemaUid & mobileSchemaUid to system settings (#2084) * refactor: migrate adminSchemaUid & mobileSchemaUid to system settings * fix: error --- .../route-switch/antd/admin-layout/index.tsx | 12 +++++- .../20230620203213-admin-ui-schema-uid.ts | 18 ++++++++ packages/plugins/client/src/server.ts | 29 +++++++++++++ .../src/client/router/Application.tsx | 24 ++++++++--- .../20230620203215-mobile-ui-schema-uid.ts | 18 ++++++++ .../mobile-client/src/server/plugin.ts | 42 ++++++++++++++++++- 6 files changed, 134 insertions(+), 9 deletions(-) create mode 100644 packages/plugins/client/src/migrations/20230620203213-admin-ui-schema-uid.ts create mode 100644 packages/plugins/mobile-client/src/server/migrations/20230620203215-mobile-ui-schema-uid.ts diff --git a/packages/core/client/src/route-switch/antd/admin-layout/index.tsx b/packages/core/client/src/route-switch/antd/admin-layout/index.tsx index c43a56b98..8dee2a056 100644 --- a/packages/core/client/src/route-switch/antd/admin-layout/index.tsx +++ b/packages/core/client/src/route-switch/antd/admin-layout/index.tsx @@ -54,6 +54,12 @@ const useMenuProps = () => { defaultSelectedUid, }; }; + +const useAdminSchemaUid = () => { + const ctx = useSystemSettings(); + return ctx?.data?.data?.options?.adminSchemaUid; +}; + const MenuEditor = (props) => { const { setTitle } = useDocumentTitle(); const navigate = useNavigate(); @@ -70,12 +76,14 @@ const MenuEditor = (props) => { navigate(`/admin/${schema['x-uid']}`); }; + const adminSchemaUid = useAdminSchemaUid(); + const { data, loading } = useRequest( { - url: `/uiSchemas:getJsonSchema/${route.uiSchemaUid}`, + url: `/uiSchemas:getJsonSchema/${adminSchemaUid}`, }, { - refreshDeps: [route.uiSchemaUid], + refreshDeps: [adminSchemaUid], onSuccess(data) { const schema = filterByACL(data?.data, ctx); // url 为 `/admin` 的情况 diff --git a/packages/plugins/client/src/migrations/20230620203213-admin-ui-schema-uid.ts b/packages/plugins/client/src/migrations/20230620203213-admin-ui-schema-uid.ts new file mode 100644 index 000000000..a956d793e --- /dev/null +++ b/packages/plugins/client/src/migrations/20230620203213-admin-ui-schema-uid.ts @@ -0,0 +1,18 @@ +import { Migration } from '@nocobase/server'; + +export default class extends Migration { + async up() { + const systemSettings = this.db.getRepository('systemSettings'); + const instance = await systemSettings.findOne(); + const uiRoutes = this.db.getRepository('uiRoutes'); + const routes = await uiRoutes.find(); + for (const route of routes) { + if (route.uiSchemaUid && route?.options?.component === 'AdminLayout') { + instance.set('options.adminSchemaUid', route.uiSchemaUid); + console.log('options.adminSchemaUid', route.uiSchemaUid); + await instance.save(); + return; + } + } + } +} diff --git a/packages/plugins/client/src/server.ts b/packages/plugins/client/src/server.ts index b407bb787..8370c6655 100644 --- a/packages/plugins/client/src/server.ts +++ b/packages/plugins/client/src/server.ts @@ -87,9 +87,38 @@ export class ClientPlugin extends Plugin { // } }); + + this.db.on('systemSettings.beforeCreate', async (instance, { transaction }) => { + const uiSchemas = this.db.getRepository('uiSchemas'); + const schema = await uiSchemas.insert( + { + type: 'void', + 'x-component': 'Menu', + 'x-designer': 'Menu.Designer', + 'x-initializer': 'MenuItemInitializers', + 'x-component-props': { + mode: 'mix', + theme: 'dark', + // defaultSelectedUid: 'u8', + onSelect: '{{ onSelect }}', + sideMenuRefScopeKey: 'sideMenuRef', + }, + properties: {}, + }, + { transaction }, + ); + instance.set('options.adminSchemaUid', schema['x-uid']); + }); } async load() { + this.db.addMigrations({ + namespace: 'client', + directory: resolve(__dirname, './migrations'), + context: { + plugin: this, + }, + }); this.app.acl.allow('app', 'getLang'); this.app.acl.allow('app', 'getInfo'); this.app.acl.allow('app', 'getPlugins'); diff --git a/packages/plugins/mobile-client/src/client/router/Application.tsx b/packages/plugins/mobile-client/src/client/router/Application.tsx index b4bc48d0d..cb622fcc5 100644 --- a/packages/plugins/mobile-client/src/client/router/Application.tsx +++ b/packages/plugins/mobile-client/src/client/router/Application.tsx @@ -1,10 +1,17 @@ +import { css, cx } from '@emotion/css'; +import { + ActionContextProvider, + AdminProvider, + RemoteSchemaComponent, + useRoute, + useSystemSettings, + useViewport, +} from '@nocobase/client'; +import { DrawerProps, ModalProps } from 'antd'; import React, { useMemo } from 'react'; import { Outlet, useParams } from 'react-router-dom'; -import { ActionContextProvider, AdminProvider, RemoteSchemaComponent, useRoute, useViewport } from '@nocobase/client'; -import { css, cx } from '@emotion/css'; -import { useInterfaceContext } from './InterfaceProvider'; -import { DrawerProps, ModalProps } from 'antd'; import { MobileCore } from '../core'; +import { useInterfaceContext } from './InterfaceProvider'; const commonCSSVariables = css` --nb-spacing: 14px; @@ -66,8 +73,15 @@ const modalProps = { `, }; +const useMobileSchemaUid = () => { + const ctx = useSystemSettings(); + return ctx?.data?.data?.options?.mobileSchemaUid; +}; + const MApplication: React.FC = (props) => { const route = useRoute(); + const mobileSchemaUid = useMobileSchemaUid(); + console.log('mobileSchemaUid', mobileSchemaUid); const params = useParams<{ name: string }>(); const interfaceContext = useInterfaceContext(); const Provider = useMemo(() => { @@ -99,7 +113,7 @@ const MApplication: React.FC = (props) => { {params.name && !params.name.startsWith('tab_') ? ( ) : ( - + {props.children} )} diff --git a/packages/plugins/mobile-client/src/server/migrations/20230620203215-mobile-ui-schema-uid.ts b/packages/plugins/mobile-client/src/server/migrations/20230620203215-mobile-ui-schema-uid.ts new file mode 100644 index 000000000..a86662d8e --- /dev/null +++ b/packages/plugins/mobile-client/src/server/migrations/20230620203215-mobile-ui-schema-uid.ts @@ -0,0 +1,18 @@ +import { Migration } from '@nocobase/server'; + +export default class extends Migration { + async up() { + const systemSettings = this.db.getRepository('systemSettings'); + const instance = await systemSettings.findOne(); + const uiRoutes = this.db.getRepository('uiRoutes'); + const routes = await uiRoutes.find(); + for (const route of routes) { + if (route.uiSchemaUid && route?.options?.component === 'MApplication') { + instance.set('options.mobileSchemaUid', route.uiSchemaUid); + console.log('options.mobileSchemaUid', route.uiSchemaUid); + await instance.save(); + return; + } + } + } +} diff --git a/packages/plugins/mobile-client/src/server/plugin.ts b/packages/plugins/mobile-client/src/server/plugin.ts index b6e93e2d1..30bd5a114 100644 --- a/packages/plugins/mobile-client/src/server/plugin.ts +++ b/packages/plugins/mobile-client/src/server/plugin.ts @@ -1,10 +1,19 @@ -import { InstallOptions, Plugin } from '@nocobase/server'; +import { Plugin } from '@nocobase/server'; +import { resolve } from 'path'; import { routes } from './routes'; export class MobileClientPlugin extends Plugin { afterAdd() {} - async load() {} + async load() { + this.db.addMigrations({ + namespace: 'client', + directory: resolve(__dirname, './migrations'), + context: { + plugin: this, + }, + }); + } async install() { const repository = this.app.db.getRepository('uiRoutes'); @@ -13,6 +22,35 @@ export class MobileClientPlugin extends Plugin { values, }); } + const uiSchemas = this.db.getRepository('uiSchemas'); + const systemSettings = this.db.getRepository('systemSettings'); + const schema = await uiSchemas.insert({ + type: 'void', + 'x-component': 'MContainer', + 'x-designer': 'MContainer.Designer', + 'x-component-props': {}, + properties: { + page: { + type: 'void', + 'x-component': 'MPage', + 'x-designer': 'MPage.Designer', + 'x-component-props': {}, + properties: { + grid: { + type: 'void', + 'x-component': 'Grid', + 'x-initializer': 'MBlockInitializers', + 'x-component-props': { + showDivider: false, + }, + }, + }, + }, + }, + }); + const instance = await systemSettings.findOne(); + instance.set('options.mobileSchemaUid', schema['x-uid']); + await instance.save(); } async afterEnable() {}