From 37cc172d8266fb5251d7788ae36be063c04dde17 Mon Sep 17 00:00:00 2001 From: "bai.jingfeng" Date: Sun, 7 Apr 2024 18:43:18 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=B8=8A=E6=B8=B8=E8=B7=9F=E8=BF=9B,=20?= =?UTF-8?q?source=20id=E4=B8=BAnull=E7=9A=84=E6=83=85=E5=86=B5,fix-source?= =?UTF-8?q?=20id=20null=20#3917?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/actions/src/utils.ts | 11 +++++-- .../src/block-provider/BlockProvider.tsx | 32 +++++++------------ .../hooks/useDataBlockSourceId.tsx | 12 +++---- packages/core/client/src/index.ts | 11 ++++--- .../useDetailsWithPaginationDecoratorProps.ts | 8 ++--- .../hooks/useDetailsDecoratorProps.ts | 14 ++++---- .../hooks/useCreateFormBlockDecoratorProps.ts | 8 ++--- .../hooks/useEditFormBlockDecoratorProps.ts | 9 +++--- .../hooks/useGridCardBlockDecoratorProps.ts | 8 ++--- .../list/hooks/useListBlockDecoratorProps.ts | 8 ++--- .../hooks/useTableBlockDecoratorProps.ts | 18 ++--------- ...ceIdCommon.ts => useParentRecordCommon.ts} | 6 ++-- .../src/default-actions/utils.ts | 11 +++++-- .../hooks/useCalendarBlockDecoratorProps.ts | 8 ++--- 14 files changed, 73 insertions(+), 91 deletions(-) rename packages/core/client/src/modules/blocks/{useSourceIdCommon.ts => useParentRecordCommon.ts} (63%) diff --git a/packages/core/actions/src/utils.ts b/packages/core/actions/src/utils.ts index 6557c6f0c..c1c1e093e 100644 --- a/packages/core/actions/src/utils.ts +++ b/packages/core/actions/src/utils.ts @@ -15,10 +15,15 @@ export function pageArgsToLimitArgs( } export function getRepositoryFromParams(ctx: Context) { - const { resourceName, resourceOf } = ctx.action; + const { resourceName, sourceId, actionName } = ctx.action; - if (resourceOf) { - return ctx.db.getRepository(resourceName, resourceOf); + if (sourceId === '_' && ['get', 'list'].includes(actionName)) { + const collection = ctx.db.getCollection(resourceName); + return ctx.db.getRepository(collection.name); + } + + if (sourceId) { + return ctx.db.getRepository(resourceName, sourceId); } return ctx.db.getRepository(resourceName); diff --git a/packages/core/client/src/block-provider/BlockProvider.tsx b/packages/core/client/src/block-provider/BlockProvider.tsx index 051131a93..c8e167bf4 100644 --- a/packages/core/client/src/block-provider/BlockProvider.tsx +++ b/packages/core/client/src/block-provider/BlockProvider.tsx @@ -11,13 +11,13 @@ import { TableFieldResource, WithoutTableFieldResource, useCollectionManager, + useCollectionParentRecord, + useCollectionParentRecordData, + useCollectionRecord, useDataBlockProps, useDataBlockRequest, useDataBlockResource, useDesignable, - useCollectionParentRecord, - useCollectionParentRecordData, - useCollectionRecord, useRecord, } from '../'; import { ACLCollectionProvider } from '../acl/ACLProvider'; @@ -29,8 +29,7 @@ import { import { DataBlockCollector } from '../filter-provider/FilterProvider'; import { RecordProvider, useRecordIndex } from '../record-provider'; import { useAssociationNames } from './hooks'; -import { useDataBlockSourceId } from './hooks/useDataBlockSourceId'; -import { useTemplateBlockContext } from './TemplateBlockProvider'; +import { useDataBlockParentRecord } from './hooks/useDataBlockSourceId'; /** * @deprecated @@ -197,16 +196,16 @@ export const useBlockContext = () => { /** * 用于兼容旧版本 Schema */ -const useCompatDataBlockSourceId = (props) => { +const useCompatDataBlockParentRecord = (props) => { const fieldSchema = useFieldSchema(); // 如果存在 x-use-decorator-props,说明是新版 Schema if (fieldSchema['x-use-decorator-props']) { - return props.sourceId; + return props.parentRecord; } else { // 是否存在 x-use-decorator-props 是固定不变的,所以这里可以使用 hooks // eslint-disable-next-line react-hooks/rules-of-hooks - return useDataBlockSourceId(props); + return useDataBlockParentRecord(props); } }; @@ -227,9 +226,8 @@ export const BlockProvider = (props: { /** @deprecated */ useParams?: any; }) => { - const { name, dataSource, association, useParams, parentRecord } = props; - const sourceId = useCompatDataBlockSourceId(props); - const { templateFinshed } = useTemplateBlockContext(); + const { name, dataSource, useParams, parentRecord } = props; + const parentRecordFromHook = useCompatDataBlockParentRecord(props); // 新版(1.0)已弃用 useParams,这里之所以继续保留是为了兼容旧版的 UISchema const paramsFromHook = useParams?.(); @@ -241,20 +239,12 @@ export const BlockProvider = (props: { return { ...props.params, appends, ...paramsFromHook }; } return { ...props.params, ...paramsFromHook }; - }, [appends, paramsFromHook, props.params, templateFinshed]); + }, [appends, paramsFromHook, props.params]); const blockValue = useMemo(() => ({ name }), [name]); return ( - + {props.children} diff --git a/packages/core/client/src/block-provider/hooks/useDataBlockSourceId.tsx b/packages/core/client/src/block-provider/hooks/useDataBlockSourceId.tsx index 3b2ff81af..ff9a527c1 100644 --- a/packages/core/client/src/block-provider/hooks/useDataBlockSourceId.tsx +++ b/packages/core/client/src/block-provider/hooks/useDataBlockSourceId.tsx @@ -5,14 +5,13 @@ import { useCollectionParentRecordData, useCollectionRecordData, } from '../..'; -import { useSourceKey } from '../../modules/blocks/useSourceKey'; /** * @internal * @deprecated - * 已弃用(该方法现在只是用来兼容旧版 Schema 的),请通过各个区块的 x-use-decorator-props 中获取 sourceId + * 已弃用(该方法现在只是用来兼容旧版 Schema 的),请通过各个区块的 x-use-decorator-props 中获取 parentRecord * - * 注意:这里有一个需要更改 schema 才能解决的问题,就是在获取 sourceId 的时候无法确定(在关系字段和当前表同表时) + * 注意:这里有一个需要更改 schema 才能解决的问题,就是在获取 parentRecord 的时候无法确定(在关系字段和当前表同表时) * 是需要从 recordData 还是 parentRecordData 中获取;解决方法是通过更改 schema,在不同类型的关系区块中 * (`通过点击关系字段按钮打开的弹窗中创建的非关系字段区块`和`关系字段区块`)使用不同的 hook。 * @@ -21,12 +20,11 @@ import { useSourceKey } from '../../modules/blocks/useSourceKey'; * @param param0 * @returns */ -export const useDataBlockSourceId = ({ association }: { association: string }) => { +export const useDataBlockParentRecord = ({ association }: { association: string }) => { const recordData = useCollectionRecordData(); const parentRecordData = useCollectionParentRecordData(); const cm = useCollectionManager(); const currentRecordCollection = useCollection(); - const sourceKey = useSourceKey(association); if (!association) return; @@ -36,8 +34,8 @@ export const useDataBlockSourceId = ({ association }: { association: string }) = currentRecordCollection.name === sourceCollection.name || currentRecordCollection.getParentCollectionsName?.().includes(sourceCollection.name) ) { - return recordData?.[sourceKey]; + return recordData; } - return parentRecordData?.[sourceKey]; + return parentRecordData; }; diff --git a/packages/core/client/src/index.ts b/packages/core/client/src/index.ts index b8586d3f2..b30595767 100644 --- a/packages/core/client/src/index.ts +++ b/packages/core/client/src/index.ts @@ -25,6 +25,7 @@ export * from './collection-manager'; export * from './common'; export * from './css-variable'; +export * from './data-source'; export * from './document-title'; export * from './filter-provider'; export * from './flag-provider'; @@ -52,16 +53,16 @@ export * from './system-settings'; export * from './testUtils'; export * from './user'; export * from './variables'; -export * from './data-source'; export { withDynamicSchemaProps } from './application/hoc/withDynamicSchemaProps'; export * from './modules/blocks/BlockSchemaToolbar'; -export * from './modules/blocks/data-blocks/table'; -export * from './modules/blocks/data-blocks/form'; -export * from './modules/blocks/data-blocks/table-selector'; export * from './modules/blocks/data-blocks/details-multi/setDataLoadingModeSettingsItem'; -export * from './modules/blocks/useSourceIdCommon'; +export * from './modules/blocks/data-blocks/form'; +export * from './modules/blocks/data-blocks/table'; +export * from './modules/blocks/data-blocks/table-selector'; + +export * from './modules/blocks/useParentRecordCommon'; export * as __UNSAFE__ from './unsafe'; export type { diff --git a/packages/core/client/src/modules/blocks/data-blocks/details-multi/hooks/useDetailsWithPaginationDecoratorProps.ts b/packages/core/client/src/modules/blocks/data-blocks/details-multi/hooks/useDetailsWithPaginationDecoratorProps.ts index 68f9750dc..0061a0057 100644 --- a/packages/core/client/src/modules/blocks/data-blocks/details-multi/hooks/useDetailsWithPaginationDecoratorProps.ts +++ b/packages/core/client/src/modules/blocks/data-blocks/details-multi/hooks/useDetailsWithPaginationDecoratorProps.ts @@ -1,15 +1,15 @@ -import { useSourceIdCommon } from '../../../useSourceIdCommon'; +import { useParentRecordCommon } from '../../../useParentRecordCommon'; export function useDetailsWithPaginationDecoratorProps(props) { - let sourceId; + let parentRecord; // association 的值是固定不变的,所以可以在条件中使用 hooks if (props.association) { // eslint-disable-next-line react-hooks/rules-of-hooks - sourceId = useSourceIdCommon(props.association); + parentRecord = useParentRecordCommon(props.association); } return { - sourceId, + parentRecord, }; } diff --git a/packages/core/client/src/modules/blocks/data-blocks/details-single/hooks/useDetailsDecoratorProps.ts b/packages/core/client/src/modules/blocks/data-blocks/details-single/hooks/useDetailsDecoratorProps.ts index 1090629d6..dcb8e6321 100644 --- a/packages/core/client/src/modules/blocks/data-blocks/details-single/hooks/useDetailsDecoratorProps.ts +++ b/packages/core/client/src/modules/blocks/data-blocks/details-single/hooks/useDetailsDecoratorProps.ts @@ -4,7 +4,6 @@ import { useCollectionParentRecordData, useCollectionRecordData, } from '../../../../../data-source/collection-record/CollectionRecordProvider'; -import { useSourceKey } from '../../../useSourceKey'; /** * 应用在通过 Current record 选项创建的区块中(弹窗中的 Add block 菜单) @@ -13,31 +12,30 @@ import { useSourceKey } from '../../../useSourceKey'; */ export function useDetailsDecoratorProps(props) { const params = useParamsFromRecord(); - let sourceId; + let parentRecord; // association 的值是固定不变的,所以可以在条件中使用 hooks if (props.association) { // eslint-disable-next-line react-hooks/rules-of-hooks - sourceId = useDetailsSourceId(props.association); + parentRecord = useDetailsParentRecord(props.association); } return { params, - sourceId, + parentRecord, }; } -export function useDetailsSourceId(association: string) { +export function useDetailsParentRecord(association: string) { const fieldSchema = useFieldSchema(); const recordData = useCollectionRecordData(); const parentRecordData = useCollectionParentRecordData(); - const sourceKey = useSourceKey(association); if (!association) return; if (fieldSchema['x-is-current']) { - return parentRecordData?.[sourceKey]; + return parentRecordData; } - return recordData?.[sourceKey]; + return recordData; } diff --git a/packages/core/client/src/modules/blocks/data-blocks/form/hooks/useCreateFormBlockDecoratorProps.ts b/packages/core/client/src/modules/blocks/data-blocks/form/hooks/useCreateFormBlockDecoratorProps.ts index 87fdb9c64..d08ec5fb0 100644 --- a/packages/core/client/src/modules/blocks/data-blocks/form/hooks/useCreateFormBlockDecoratorProps.ts +++ b/packages/core/client/src/modules/blocks/data-blocks/form/hooks/useCreateFormBlockDecoratorProps.ts @@ -1,15 +1,15 @@ -import { useSourceIdCommon } from '../../../useSourceIdCommon'; +import { useParentRecordCommon } from '../../../useParentRecordCommon'; export function useCreateFormBlockDecoratorProps(props) { - let sourceId; + let parentRecord; // association 的值是固定不变的,所以这里可以使用 hooks if (props.association) { // eslint-disable-next-line react-hooks/rules-of-hooks - sourceId = useSourceIdCommon(props.association); + parentRecord = useParentRecordCommon(props.association); } return { - sourceId, + parentRecord, }; } diff --git a/packages/core/client/src/modules/blocks/data-blocks/form/hooks/useEditFormBlockDecoratorProps.ts b/packages/core/client/src/modules/blocks/data-blocks/form/hooks/useEditFormBlockDecoratorProps.ts index 47df14192..bd007cd21 100644 --- a/packages/core/client/src/modules/blocks/data-blocks/form/hooks/useEditFormBlockDecoratorProps.ts +++ b/packages/core/client/src/modules/blocks/data-blocks/form/hooks/useEditFormBlockDecoratorProps.ts @@ -1,20 +1,19 @@ import { useParamsFromRecord } from '../../../../../block-provider/BlockProvider'; -import { useDetailsSourceId } from '../../details-single/hooks/useDetailsDecoratorProps'; - +import { useDetailsParentRecord } from '../../details-single/hooks/useDetailsDecoratorProps'; export function useEditFormBlockDecoratorProps(props) { const params = useFormBlockParams(); - let sourceId; + let parentRecord; // association 的值是固定不变的,所以这里可以使用 hooks if (props.association) { // 复用详情区块的 sourceId 获取逻辑 // eslint-disable-next-line react-hooks/rules-of-hooks - sourceId = useDetailsSourceId(props.association); + parentRecord = useDetailsParentRecord(props.association); } return { params, - sourceId, + parentRecord, }; } diff --git a/packages/core/client/src/modules/blocks/data-blocks/grid-card/hooks/useGridCardBlockDecoratorProps.ts b/packages/core/client/src/modules/blocks/data-blocks/grid-card/hooks/useGridCardBlockDecoratorProps.ts index 4fd4c36ef..41b217753 100644 --- a/packages/core/client/src/modules/blocks/data-blocks/grid-card/hooks/useGridCardBlockDecoratorProps.ts +++ b/packages/core/client/src/modules/blocks/data-blocks/grid-card/hooks/useGridCardBlockDecoratorProps.ts @@ -1,18 +1,18 @@ import { useGridCardBlockParams } from './useGridCardBlockParams'; -import { useSourceIdCommon } from '../../../useSourceIdCommon'; +import { useParentRecordCommon } from '../../../useParentRecordCommon'; export function useGridCardBlockDecoratorProps(props) { const params = useGridCardBlockParams(props); - let sourceId; + let parentRecord; // 因为 association 是固定的,所以可以在条件中使用 hooks if (props.association) { // eslint-disable-next-line react-hooks/rules-of-hooks - sourceId = useSourceIdCommon(props.association); + parentRecord = useParentRecordCommon(props.association); } return { params, - sourceId, + parentRecord, }; } diff --git a/packages/core/client/src/modules/blocks/data-blocks/list/hooks/useListBlockDecoratorProps.ts b/packages/core/client/src/modules/blocks/data-blocks/list/hooks/useListBlockDecoratorProps.ts index 98825fc06..0d4a13758 100644 --- a/packages/core/client/src/modules/blocks/data-blocks/list/hooks/useListBlockDecoratorProps.ts +++ b/packages/core/client/src/modules/blocks/data-blocks/list/hooks/useListBlockDecoratorProps.ts @@ -1,15 +1,15 @@ -import { useSourceIdCommon } from '../../../useSourceIdCommon'; +import { useParentRecordCommon } from '../../../useParentRecordCommon'; export function useListBlockDecoratorProps(props) { - let sourceId; + let parentRecord; // 因为 association 的值是固定的,所以这里可以使用 hooks if (props.association) { // eslint-disable-next-line react-hooks/rules-of-hooks - sourceId = useSourceIdCommon(props.association); + parentRecord = useParentRecordCommon(props.association); } return { - sourceId, + parentRecord, }; } diff --git a/packages/core/client/src/modules/blocks/data-blocks/table/hooks/useTableBlockDecoratorProps.ts b/packages/core/client/src/modules/blocks/data-blocks/table/hooks/useTableBlockDecoratorProps.ts index e728076a8..24be82811 100644 --- a/packages/core/client/src/modules/blocks/data-blocks/table/hooks/useTableBlockDecoratorProps.ts +++ b/packages/core/client/src/modules/blocks/data-blocks/table/hooks/useTableBlockDecoratorProps.ts @@ -1,15 +1,15 @@ import { useFieldSchema } from '@nocobase/schema'; import { useParsedFilter } from '../../../../../block-provider/hooks/useParsedFilter'; import { useMemo } from 'react'; -import { useSourceIdCommon } from '../../../useSourceIdCommon'; +import { useParentRecordCommon } from '../../../useParentRecordCommon'; export const useTableBlockDecoratorProps = (props) => { const params = useTableBlockParams(props); - const sourceId = useTableBlockSourceId(props); + const parentRecord = useParentRecordCommon(props.association); return { params, - sourceId, + parentRecord, }; }; @@ -37,15 +37,3 @@ export function useTableBlockParams(props) { return paramsWithFilter; }, [fieldSchema, parsedFilter, props.dragSort, props.params]); } - -function useTableBlockSourceId(props) { - let sourceId: string | undefined; - - // 因为 association 是固定不变的,所以在条件中使用 hooks 是安全的 - if (props.association) { - // eslint-disable-next-line react-hooks/rules-of-hooks - sourceId = useSourceIdCommon(props.association); - } - - return sourceId; -} diff --git a/packages/core/client/src/modules/blocks/useSourceIdCommon.ts b/packages/core/client/src/modules/blocks/useParentRecordCommon.ts similarity index 63% rename from packages/core/client/src/modules/blocks/useSourceIdCommon.ts rename to packages/core/client/src/modules/blocks/useParentRecordCommon.ts index f3bfa7156..1cc103a6f 100644 --- a/packages/core/client/src/modules/blocks/useSourceIdCommon.ts +++ b/packages/core/client/src/modules/blocks/useParentRecordCommon.ts @@ -1,5 +1,4 @@ import { useCollectionRecordData } from '../../data-source/collection-record/CollectionRecordProvider'; -import { useSourceKey } from './useSourceKey'; /** * @internal @@ -7,11 +6,10 @@ import { useSourceKey } from './useSourceKey'; * @param association * @returns */ -export const useSourceIdCommon = (association: string) => { +export const useParentRecordCommon = (association: string) => { const recordData = useCollectionRecordData(); - const sourceKey = useSourceKey(association); if (!association) return; - return recordData?.[sourceKey]; + return recordData; }; diff --git a/packages/core/data-source-manager/src/default-actions/utils.ts b/packages/core/data-source-manager/src/default-actions/utils.ts index 9adbdf29a..84783065d 100644 --- a/packages/core/data-source-manager/src/default-actions/utils.ts +++ b/packages/core/data-source-manager/src/default-actions/utils.ts @@ -15,12 +15,17 @@ export function pageArgsToLimitArgs( } export function getRepositoryFromParams(ctx: Context): IRepository { - const { resourceName, resourceOf } = ctx.action; + const { resourceName, sourceId, actionName } = ctx.action; const dataSource: DataSource = ctx.dataSource; - if (resourceOf) { - return dataSource.collectionManager.getRepository(resourceName, resourceOf); + if (sourceId === '_' && ['get', 'list'].includes(actionName)) { + const collection = dataSource.collectionManager.getCollection(resourceName); + return dataSource.collectionManager.getRepository(collection.name); + } + + if (sourceId) { + return dataSource.collectionManager.getRepository(resourceName, sourceId); } return dataSource.collectionManager.getRepository(resourceName); diff --git a/packages/plugins/@nocobase/plugin-calendar/src/client/hooks/useCalendarBlockDecoratorProps.ts b/packages/plugins/@nocobase/plugin-calendar/src/client/hooks/useCalendarBlockDecoratorProps.ts index 85baa46be..7e29575ec 100644 --- a/packages/plugins/@nocobase/plugin-calendar/src/client/hooks/useCalendarBlockDecoratorProps.ts +++ b/packages/plugins/@nocobase/plugin-calendar/src/client/hooks/useCalendarBlockDecoratorProps.ts @@ -1,18 +1,18 @@ -import { useSourceIdCommon } from '@nocobase/client'; +import { useParentRecordCommon } from '@nocobase/client'; import { useCalendarBlockParams } from './useCalendarBlockParams'; export function useCalendarBlockDecoratorProps(props) { const params = useCalendarBlockParams(props); - let sourceId: string; + let parentRecord; // 因为 association 是一个固定的值,所以可以在 hooks 中直接使用 if (props.association) { // eslint-disable-next-line react-hooks/rules-of-hooks - sourceId = useSourceIdCommon(props.association); + parentRecord = useParentRecordCommon(props.association); } return { params, - sourceId, + parentRecord, }; }