refactor: migrate adminSchemaUid & mobileSchemaUid to system settings (#2084)

* refactor: migrate adminSchemaUid & mobileSchemaUid to system settings

* fix: error
This commit is contained in:
chenos 2023-06-21 11:02:49 +08:00 committed by GitHub
parent 56e6d0c3b1
commit 64070b81b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 134 additions and 9 deletions

View File

@ -54,6 +54,12 @@ const useMenuProps = () => {
defaultSelectedUid, defaultSelectedUid,
}; };
}; };
const useAdminSchemaUid = () => {
const ctx = useSystemSettings();
return ctx?.data?.data?.options?.adminSchemaUid;
};
const MenuEditor = (props) => { const MenuEditor = (props) => {
const { setTitle } = useDocumentTitle(); const { setTitle } = useDocumentTitle();
const navigate = useNavigate(); const navigate = useNavigate();
@ -70,12 +76,14 @@ const MenuEditor = (props) => {
navigate(`/admin/${schema['x-uid']}`); navigate(`/admin/${schema['x-uid']}`);
}; };
const adminSchemaUid = useAdminSchemaUid();
const { data, loading } = useRequest( const { data, loading } = useRequest(
{ {
url: `/uiSchemas:getJsonSchema/${route.uiSchemaUid}`, url: `/uiSchemas:getJsonSchema/${adminSchemaUid}`,
}, },
{ {
refreshDeps: [route.uiSchemaUid], refreshDeps: [adminSchemaUid],
onSuccess(data) { onSuccess(data) {
const schema = filterByACL(data?.data, ctx); const schema = filterByACL(data?.data, ctx);
// url 为 `/admin` 的情况 // url 为 `/admin` 的情况

View File

@ -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;
}
}
}
}

View File

@ -87,9 +87,38 @@ export class ClientPlugin extends Plugin {
// //
} }
}); });
this.db.on('systemSettings.beforeCreate', async (instance, { transaction }) => {
const uiSchemas = this.db.getRepository<any>('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() { 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', 'getLang');
this.app.acl.allow('app', 'getInfo'); this.app.acl.allow('app', 'getInfo');
this.app.acl.allow('app', 'getPlugins'); this.app.acl.allow('app', 'getPlugins');

View File

@ -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 React, { useMemo } from 'react';
import { Outlet, useParams } from 'react-router-dom'; 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 { MobileCore } from '../core';
import { useInterfaceContext } from './InterfaceProvider';
const commonCSSVariables = css` const commonCSSVariables = css`
--nb-spacing: 14px; --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 MApplication: React.FC = (props) => {
const route = useRoute(); const route = useRoute();
const mobileSchemaUid = useMobileSchemaUid();
console.log('mobileSchemaUid', mobileSchemaUid);
const params = useParams<{ name: string }>(); const params = useParams<{ name: string }>();
const interfaceContext = useInterfaceContext(); const interfaceContext = useInterfaceContext();
const Provider = useMemo(() => { const Provider = useMemo(() => {
@ -99,7 +113,7 @@ const MApplication: React.FC = (props) => {
{params.name && !params.name.startsWith('tab_') ? ( {params.name && !params.name.startsWith('tab_') ? (
<Outlet /> <Outlet />
) : ( ) : (
<RemoteSchemaComponent key={route.uiSchemaUid} uid={route.uiSchemaUid}> <RemoteSchemaComponent key={mobileSchemaUid} uid={mobileSchemaUid}>
{props.children} {props.children}
</RemoteSchemaComponent> </RemoteSchemaComponent>
)} )}

View File

@ -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;
}
}
}
}

View File

@ -1,10 +1,19 @@
import { InstallOptions, Plugin } from '@nocobase/server'; import { Plugin } from '@nocobase/server';
import { resolve } from 'path';
import { routes } from './routes'; import { routes } from './routes';
export class MobileClientPlugin extends Plugin { export class MobileClientPlugin extends Plugin {
afterAdd() {} afterAdd() {}
async load() {} async load() {
this.db.addMigrations({
namespace: 'client',
directory: resolve(__dirname, './migrations'),
context: {
plugin: this,
},
});
}
async install() { async install() {
const repository = this.app.db.getRepository('uiRoutes'); const repository = this.app.db.getRepository('uiRoutes');
@ -13,6 +22,35 @@ export class MobileClientPlugin extends Plugin {
values, values,
}); });
} }
const uiSchemas = this.db.getRepository<any>('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() {} async afterEnable() {}