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 b14321b14..d0c2e6118 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 @@ -1,6 +1,6 @@ import { css } from '@emotion/css'; import { Layout, Spin } from 'antd'; -import React, { useRef, useState } from 'react'; +import React, { createContext, useContext, useMemo, useRef } from 'react'; import { useHistory, useRouteMatch } from 'react-router-dom'; import { ACLAllowConfigure, @@ -11,10 +11,11 @@ import { findMenuItem, PluginManager, RemoteCollectionManagerProvider, - RemoteSchemaComponent, RemoteSchemaTemplateManagerProvider, + SchemaComponent, useACLRoleContext, useDocumentTitle, + useRequest, useRoute, useSystemSettings } from '../../../'; @@ -27,6 +28,9 @@ const filterByACL = (schema, options) => { return schema; } const filterSchema = (s) => { + if (!s) { + return; + } for (const key in s.properties) { if (Object.prototype.hasOwnProperty.call(s.properties, key)) { const element = s.properties[key]; @@ -40,23 +44,74 @@ const filterByACL = (schema, options) => { return schema; }; +const SchemaIdContext = createContext(null); +const useMenuProps = () => { + const defaultSelectedUid = useContext(SchemaIdContext); + return { + selectedUid: defaultSelectedUid, + defaultSelectedUid, + }; +}; +const MenuEditor = (props) => { + const { setTitle } = useDocumentTitle(); + const history = useHistory(); + const match = useRouteMatch(); + const defaultSelectedUid = match.params.name; + const { sideMenuRef } = props; + const ctx = useACLRoleContext(); + const route = useRoute(); + const onSelect = ({ item }) => { + const schema = item.props.schema; + setTitle(schema.title); + history.push(`/admin/${schema['x-uid']}`); + }; + const { data, loading } = useRequest( + { + url: `/uiSchemas:getJsonSchema/${route.uiSchemaUid}`, + }, + { + refreshDeps: [route.uiSchemaUid], + onSuccess(data) { + const schema = filterByACL(data?.data, ctx); + if (defaultSelectedUid) { + const s = findByUid(schema, defaultSelectedUid); + if (s) { + setTitle(s.title); + } + } else { + const s = findMenuItem(schema); + if (s) { + history.push(`/admin/${s['x-uid']}`); + setTitle(s.title); + } + } + }, + }, + ); + const schema = useMemo(() => { + const s = filterByACL(data?.data, ctx); + if (s?.['x-component-props']) { + s['x-component-props']['useProps'] = useMenuProps; + } + return s; + }, [data?.data]); + if (loading) { + return ; + } + return ( + + + + ); +}; + const InternalAdminLayout = (props: any) => { const route = useRoute(); const history = useHistory(); const match = useRouteMatch(); const { setTitle } = useDocumentTitle(); const sideMenuRef = useRef(); - const defaultSelectedUid = match.params.name; - const [schema, setSchema] = useState({}); - const ctx = useACLRoleContext(); - const onSelect = ({ item }) => { - const schema = item.props.schema; - console.log('onSelect', schema); - setSchema(schema); - setTitle(schema.title); - history.push(`/admin/${schema['x-uid']}`); - }; - const [hidden, setHidden] = useState(false); + const result = useSystemSettings(); const { service } = useCollectionManager(); return ( @@ -91,40 +146,7 @@ const InternalAdminLayout = (props: any) => { width: 'calc(100% - 590px)', }} > -