From f185b116164f2096880cb90ecec146d36c05a2f6 Mon Sep 17 00:00:00 2001 From: dream2023 <1098626505@qq.com> Date: Thu, 18 Jan 2024 11:02:54 +0800 Subject: [PATCH 1/2] fix: cache bug --- .../Configuration/ConfigurationTable.tsx | 24 ++++++++++--------- .../hooks/useCollectionManager.ts | 14 +++-------- 2 files changed, 16 insertions(+), 22 deletions(-) diff --git a/packages/core/client/src/collection-manager/Configuration/ConfigurationTable.tsx b/packages/core/client/src/collection-manager/Configuration/ConfigurationTable.tsx index 88318a4939..96b3bfc859 100644 --- a/packages/core/client/src/collection-manager/Configuration/ConfigurationTable.tsx +++ b/packages/core/client/src/collection-manager/Configuration/ConfigurationTable.tsx @@ -27,12 +27,14 @@ import { collectionSchema } from './schemas/collections'; const useAsyncDataSource = (service: any, exclude?: string[]) => { return (field: any, options?: any) => { field.loading = true; - service(field, options, exclude).then( - action.bound((data: any) => { - field.dataSource = data; - field.loading = false; - }), - ); + service(field, options, exclude) + .then( + action.bound((data: any) => { + field.dataSource = data; + field.loading = false; + }), + ) + .catch(console.error); }; }; @@ -66,13 +68,15 @@ const useBulkDestroySubField = () => { // 获取当前字段列表 const useCurrentFields = () => { const record = useRecord(); + const { getCollectionFields } = useCollectionManager(); + // 仅当当前字段为子表单时,从DataSourceContext中获取已配置的字段列表 if (record.__parent && record.__parent.interface === 'subTable') { + // eslint-disable-next-line react-hooks/rules-of-hooks const ctx = useContext(DataSourceContext); return ctx.dataSource; } - const { getCollectionFields } = useCollectionManager(); const fields = getCollectionFields(record.collectionName || record.name) as any[]; return fields; }; @@ -83,7 +87,7 @@ const useNewId = (prefix) => { export const ConfigurationTable = () => { const { t } = useTranslation(); - const { collections = [], interfaces } = useCollectionManager(); + const { interfaces, getCollections } = useCollectionManager(); const { data: { database }, } = useCurrentAppInfo(); @@ -91,8 +95,6 @@ export const ConfigurationTable = () => { const data = useContext(CollectionCategroriesContext); const api = useAPIClient(); const resource = api.resource('dbViews'); - const collectonsRef: any = useRef(); - collectonsRef.current = collections; const compile = useCompile(); /** @@ -105,7 +107,7 @@ export const ConfigurationTable = () => { const loadCollections = async (field, options, exclude?: string[]) => { const { targetScope } = options; const isFieldInherits = field.props?.name === 'inherits'; - const filteredItems = collectonsRef.current.filter((item) => { + const filteredItems = getCollections().filter((item) => { if (exclude?.includes(item.template)) { return false; } diff --git a/packages/core/client/src/collection-manager/hooks/useCollectionManager.ts b/packages/core/client/src/collection-manager/hooks/useCollectionManager.ts index 55772834d2..750bfcbb7c 100644 --- a/packages/core/client/src/collection-manager/hooks/useCollectionManager.ts +++ b/packages/core/client/src/collection-manager/hooks/useCollectionManager.ts @@ -1,7 +1,7 @@ import { CascaderProps } from 'antd'; import _ from 'lodash'; import { useCallback, useMemo, useState } from 'react'; -import { useCompile, useSchemaComponentContext } from '../../schema-component'; +import { useCompile } from '../../schema-component'; import { CollectionFieldOptions, CollectionOptions } from '../types'; import { useCollectionManagerV2 } from '../../application'; import { InheritanceCollectionMixin } from '../mixins/InheritanceCollectionMixin'; @@ -19,17 +19,9 @@ export const useCollectionManager = () => { .map((item) => item.getOptions()); }, [cm]); const collections = useMemo(() => getCollections(), [cm, random]); - const { refresh } = useSchemaComponentContext(); - const service = useCallback(() => cm?.reload(refresh), [cm]); + const service = useCallback(() => cm?.reload(() => setRandom(uid())), [cm]); const updateCollection = cm?.setCollections.bind(cm); - const refreshCM = useCallback( - () => - cm?.reload(() => { - refresh(); - setRandom(uid()); - }), - [cm], - ); + const refreshCM = useCallback(() => cm?.reload(() => setRandom(uid())), [cm]); const compile = useCompile(); const getInheritedFields = useCallback( From 87d8f767fad231ba37568120952fda157ee017a3 Mon Sep 17 00:00:00 2001 From: dream2023 <1098626505@qq.com> Date: Thu, 18 Jan 2024 11:51:01 +0800 Subject: [PATCH 2/2] fix: workflow menual --- .../src/block-provider/BlockProvider.tsx | 10 ++-- .../collection-manager/CollectionProvider.tsx | 12 +++-- .../hooks/useCollectionManager.ts | 4 +- .../items/DataBlockInitializer.tsx | 2 +- .../items/FormBlockInitializer.tsx | 1 - .../instruction/FormBlockInitializer.tsx | 2 +- .../client/instruction/FormBlockProvider.tsx | 48 ++++++++++++------- .../src/client/instruction/forms/create.tsx | 5 +- .../src/client/instruction/forms/update.tsx | 1 + 9 files changed, 49 insertions(+), 36 deletions(-) diff --git a/packages/core/client/src/block-provider/BlockProvider.tsx b/packages/core/client/src/block-provider/BlockProvider.tsx index 028186b2f1..76079ecc64 100644 --- a/packages/core/client/src/block-provider/BlockProvider.tsx +++ b/packages/core/client/src/block-provider/BlockProvider.tsx @@ -303,9 +303,9 @@ export const useBlockContext = () => { return useContext(BlockContext); }; -export const BlockNamespaceContext = createContext(undefined); -export const useBlockNamespace = () => { - return useContext(BlockNamespaceContext); +export const CollectionNamespace = createContext(undefined); +export const useCollectionNamespace = () => { + return useContext(CollectionNamespace); }; export const BlockProvider = (props: { @@ -331,7 +331,7 @@ export const BlockProvider = (props: { return ( - + @@ -345,7 +345,7 @@ export const BlockProvider = (props: { - + ); }; diff --git a/packages/core/client/src/collection-manager/CollectionProvider.tsx b/packages/core/client/src/collection-manager/CollectionProvider.tsx index fd961de74b..2dbb839eaf 100644 --- a/packages/core/client/src/collection-manager/CollectionProvider.tsx +++ b/packages/core/client/src/collection-manager/CollectionProvider.tsx @@ -4,7 +4,7 @@ import { CollectionOptions } from './types'; import React from 'react'; import { DeletedPlaceholder } from '../application/collection/DeletedPlaceholder'; import { CollectionExtendsProvider } from './CollectionManagerProvider'; -import { useBlockNamespace } from '../block-provider'; +import { useCollectionNamespace } from '../block-provider'; function getCollectionName(name?: string | CollectionOptions): string { if (!name) return undefined; @@ -17,14 +17,16 @@ export const CollectionProvider: FC<{ collection?: CollectionOptions | string; allowNull?: boolean; children?: ReactNode; -}> = ({ children, allowNull, name, collection }) => { + namespace?: string; +}> = ({ children, allowNull, name, namespace, collection }) => { const collectionName = getCollectionName(name || collection); - const namespace = useBlockNamespace(); + const collectionNamespace = useCollectionNamespace(); + const namespaceValue = namespace || collectionNamespace || undefined; const cm = useCollectionManagerV2(); - const hasCollection = cm.getCollection(collectionName, { namespace }); + const hasCollection = cm.getCollection(collectionName, { namespace: namespaceValue }); if (hasCollection || (allowNull && !collection)) return ( - + {children} ); diff --git a/packages/core/client/src/collection-manager/hooks/useCollectionManager.ts b/packages/core/client/src/collection-manager/hooks/useCollectionManager.ts index ab195f9d49..df7891b10d 100644 --- a/packages/core/client/src/collection-manager/hooks/useCollectionManager.ts +++ b/packages/core/client/src/collection-manager/hooks/useCollectionManager.ts @@ -6,11 +6,11 @@ import { CollectionFieldOptions, CollectionOptions } from '../types'; import { useCollectionManagerV2 } from '../../application'; import { InheritanceCollectionMixin } from '../mixins/InheritanceCollectionMixin'; import { uid } from '@formily/shared'; -import { useBlockNamespace } from '../../block-provider'; +import { useCollectionNamespace } from '../../block-provider'; export const useCollectionManager = (namespace?: string) => { const cm = useCollectionManagerV2(); - const blockNamespaceValue = useBlockNamespace(); + const blockNamespaceValue = useCollectionNamespace(); const namespaceValue = namespace || blockNamespaceValue || undefined; const [random, setRandom] = useState(uid()); const interfaces = useMemo(() => cm?.getFieldInterfaces(), [cm, random]); diff --git a/packages/core/client/src/schema-initializer/items/DataBlockInitializer.tsx b/packages/core/client/src/schema-initializer/items/DataBlockInitializer.tsx index aa7becabd8..10aefd8264 100644 --- a/packages/core/client/src/schema-initializer/items/DataBlockInitializer.tsx +++ b/packages/core/client/src/schema-initializer/items/DataBlockInitializer.tsx @@ -121,7 +121,7 @@ export function useMenuSearch(data: any, openKey: string, showType?: boolean) { const currentItems = useMemo(() => { if (!openKey) return []; - return data.find((item) => item.key === openKey)?.children || []; + return data.find((item) => (item.key || item.name) === openKey)?.children || []; }, [data, openKey]); // 根据搜索的值进行处理 diff --git a/packages/core/client/src/schema-initializer/items/FormBlockInitializer.tsx b/packages/core/client/src/schema-initializer/items/FormBlockInitializer.tsx index 9704f90b7a..7533a33b3f 100644 --- a/packages/core/client/src/schema-initializer/items/FormBlockInitializer.tsx +++ b/packages/core/client/src/schema-initializer/items/FormBlockInitializer.tsx @@ -13,7 +13,6 @@ export const FormBlockInitializer = () => { icon={} componentType={'FormItem'} templateWrap={(templateSchema, { item }) => { - console.log('item', item); const s = createFormBlockSchema({ isCusomeizeCreate, namespace: item.namespace, diff --git a/packages/plugins/@nocobase/plugin-workflow-manual/src/client/instruction/FormBlockInitializer.tsx b/packages/plugins/@nocobase/plugin-workflow-manual/src/client/instruction/FormBlockInitializer.tsx index 2d8b2546c0..b568eb1732 100644 --- a/packages/plugins/@nocobase/plugin-workflow-manual/src/client/instruction/FormBlockInitializer.tsx +++ b/packages/plugins/@nocobase/plugin-workflow-manual/src/client/instruction/FormBlockInitializer.tsx @@ -65,7 +65,7 @@ function InternalFormBlockInitializer({ schema, ...others }) { export function FormBlockInitializer() { const itemConfig = useSchemaInitializerItem(); return ( - + ); diff --git a/packages/plugins/@nocobase/plugin-workflow-manual/src/client/instruction/FormBlockProvider.tsx b/packages/plugins/@nocobase/plugin-workflow-manual/src/client/instruction/FormBlockProvider.tsx index f7bc931e3d..2ed625f75f 100644 --- a/packages/plugins/@nocobase/plugin-workflow-manual/src/client/instruction/FormBlockProvider.tsx +++ b/packages/plugins/@nocobase/plugin-workflow-manual/src/client/instruction/FormBlockProvider.tsx @@ -2,7 +2,9 @@ import { createForm } from '@formily/core'; import { RecursionField, useField, useFieldSchema } from '@formily/react'; import { BlockRequestContext, + CollectionNamespace, CollectionProvider, + DEFAULT_COLLECTION_NAMESPACE_NAME, FormActiveFieldsProvider, FormBlockContext, FormV2, @@ -19,7 +21,9 @@ export function FormBlockProvider(props) { const fieldSchema = useFieldSchema(); const field = useField(); const formBlockRef = useRef(null); - const { getAssociationAppends } = useAssociationNames(); + const namespace = props.namespace || DEFAULT_COLLECTION_NAMESPACE_NAME; + + const { getAssociationAppends } = useAssociationNames(namespace); const { appends, updateAssociationValues } = getAssociationAppends(); const [formKey] = Object.keys(fieldSchema.toJSON().properties ?? {}); const values = userJob?.result?.[formKey]; @@ -50,7 +54,13 @@ export function FormBlockProvider(props) { }; }, [values]); const api = useAPIClient(); - const resource = api.resource(props.collection); + const headers = useMemo(() => { + if (namespace !== DEFAULT_COLLECTION_NAMESPACE_NAME) { + return { 'x-connection': namespace }; + } + }, [namespace]); + + const resource = api.resource(props.collection, undefined, headers); const __parent = useContext(BlockRequestContext); const formBlockValue = useMemo(() => { @@ -65,21 +75,23 @@ export function FormBlockProvider(props) { }, [field, form, params, service, updateAssociationValues]); return !userJob.status || values ? ( - - - - - - - -
- -
-
-
-
-
-
-
+ + + + + + + + +
+ +
+
+
+
+
+
+
+
) : null; } diff --git a/packages/plugins/@nocobase/plugin-workflow-manual/src/client/instruction/forms/create.tsx b/packages/plugins/@nocobase/plugin-workflow-manual/src/client/instruction/forms/create.tsx index 0e0cdf1237..e096b15040 100644 --- a/packages/plugins/@nocobase/plugin-workflow-manual/src/client/instruction/forms/create.tsx +++ b/packages/plugins/@nocobase/plugin-workflow-manual/src/client/instruction/forms/create.tsx @@ -41,9 +41,10 @@ export default { title: `{{t("Create record form", { ns: "${NAMESPACE}" })}}`, config: { useInitializer({ allCollections }) { - const items = useMemo( + const childItems = useMemo( () => allCollections.map(({ nsName, nsTitle, collections }) => ({ + key: nsName, name: nsName, label: nsTitle, type: 'subMenu', @@ -63,8 +64,6 @@ export default { })), [allCollections], ); - const getMenuItems = useGetSchemaInitializerMenuItems(); - const childItems = useMemo(() => getMenuItems(items, 'createRecordForm'), [items]); const [openMenuKey, setOpenMenuKey] = useState(''); const searchedChildren = useMenuSearch(childItems, openMenuKey); diff --git a/packages/plugins/@nocobase/plugin-workflow-manual/src/client/instruction/forms/update.tsx b/packages/plugins/@nocobase/plugin-workflow-manual/src/client/instruction/forms/update.tsx index a5bdfa75ea..181992353d 100644 --- a/packages/plugins/@nocobase/plugin-workflow-manual/src/client/instruction/forms/update.tsx +++ b/packages/plugins/@nocobase/plugin-workflow-manual/src/client/instruction/forms/update.tsx @@ -81,6 +81,7 @@ export default { const childItems = useMemo( () => allCollections.map(({ nsName, nsTitle, collections }) => ({ + key: nsName, name: nsName, label: nsTitle, type: 'subMenu',