refactor: remove deprecated API

This commit is contained in:
Zeke Zhang 2024-11-01 16:55:09 +08:00
parent a5f93b95fb
commit fee1c2999c
3 changed files with 21 additions and 16 deletions

View File

@ -439,15 +439,15 @@ export const updateFilterTargets = (fieldSchema, targets: FilterTarget['targets'
const useDoFilter = () => { const useDoFilter = () => {
const form = useForm(); const form = useForm();
const { getDataBlocks } = useFilterBlock(); const { getDataBlocks } = useFilterBlock();
const { getCollectionJoinField } = useCollectionManager_deprecated(); const cm = useCollectionManager();
const { getOperators } = useOperators(); const { getOperators } = useOperators();
const fieldSchema = useFieldSchema(); const fieldSchema = useFieldSchema();
const { name } = useCollection(); const { name } = useCollection();
const { targets = [], uid } = useMemo(() => findFilterTargets(fieldSchema), [fieldSchema]); const { targets = [], uid } = useMemo(() => findFilterTargets(fieldSchema), [fieldSchema]);
const getFilterFromCurrentForm = useCallback(() => { const getFilterFromCurrentForm = useCallback(() => {
return removeNullCondition(transformToFilter(form.values, getOperators(), getCollectionJoinField, name)); return removeNullCondition(transformToFilter(form.values, getOperators(), cm.getCollectionField.bind(cm), name));
}, [form.values, getCollectionJoinField, getOperators, name]); }, [form.values, cm, getOperators, name]);
const doFilter = useCallback( const doFilter = useCallback(
async ({ doNothingWhenFilterIsEmpty = false } = {}) => { async ({ doNothingWhenFilterIsEmpty = false } = {}) => {

View File

@ -8,7 +8,7 @@
*/ */
import { useField, useFieldSchema } from '@formily/react'; import { useField, useFieldSchema } from '@formily/react';
import _, { uniqBy } from 'lodash'; import _ from 'lodash';
import { CollectionFieldOptions_deprecated } from '../collection-manager'; import { CollectionFieldOptions_deprecated } from '../collection-manager';
import { Collection } from '../data-source/collection/Collection'; import { Collection } from '../data-source/collection/Collection';
import { useCollection } from '../data-source/collection/CollectionProvider'; import { useCollection } from '../data-source/collection/CollectionProvider';
@ -203,8 +203,8 @@ export const useFilterBlock = () => {
Object.assign(existingBlock, block); Object.assign(existingBlock, block);
return; return;
} }
// 由于 setDataBlocks 是异步操作,所以上面的 existingBlock 在判断时有可能用的是旧的 dataBlocks,所以下面还需要根据 uid 进行去重操作
ctx?.setDataBlocks((prev) => uniqBy([...prev, block], 'uid')); ctx?.setDataBlocks((prev) => [...prev, block]);
}, },
[ctx], [ctx],
); );

View File

@ -12,14 +12,10 @@ import { flatten, getValuesByPath } from '@nocobase/utils/client';
import _ from 'lodash'; import _ from 'lodash';
import { useCallback, useEffect, useState } from 'react'; import { useCallback, useEffect, useState } from 'react';
import { FilterTarget, findFilterTargets } from '../block-provider/hooks'; import { FilterTarget, findFilterTargets } from '../block-provider/hooks';
import { import { CollectionFieldOptions_deprecated, FieldOptions, InheritanceCollectionMixin } from '../collection-manager';
CollectionFieldOptions_deprecated,
FieldOptions,
useCollectionManager_deprecated,
useCollection_deprecated,
} from '../collection-manager';
import { Collection } from '../data-source/collection/Collection'; import { Collection } from '../data-source/collection/Collection';
import { useCollection } from '../data-source/collection/CollectionProvider'; import { useCollection } from '../data-source/collection/CollectionProvider';
import { useDataSourceManager } from '../data-source/data-source/DataSourceManagerProvider';
import { removeNullCondition } from '../schema-component'; import { removeNullCondition } from '../schema-component';
import { DataBlock, useFilterBlock } from './FilterProvider'; import { DataBlock, useFilterBlock } from './FilterProvider';
@ -65,10 +61,21 @@ export const getSupportFieldsByForeignKey = (filterBlockCollection: Collection,
* @returns * @returns
*/ */
export const useSupportedBlocks = (filterBlockType: FilterBlockType) => { export const useSupportedBlocks = (filterBlockType: FilterBlockType) => {
const dm = useDataSourceManager();
const { getDataBlocks } = useFilterBlock(); const { getDataBlocks } = useFilterBlock();
const fieldSchema = useFieldSchema(); const fieldSchema = useFieldSchema();
const collection = useCollection(); const collection = useCollection();
const { getAllCollectionsInheritChain } = useCollectionManager_deprecated();
// 获取当前 collection 继承链路上的所有 collection
const getAllCollectionsInheritChain = useCallback(
(collectionName: string, customDataSource?: string) => {
return dm
?.getDataSource(customDataSource)
?.collectionManager?.getCollection<InheritanceCollectionMixin>(collectionName)
?.getAllCollectionsInheritChain();
},
[dm],
);
// Form 和 Collapse 仅支持同表的数据区块 // Form 和 Collapse 仅支持同表的数据区块
if (filterBlockType === FilterBlockType.FORM || filterBlockType === FilterBlockType.COLLAPSE) { if (filterBlockType === FilterBlockType.FORM || filterBlockType === FilterBlockType.COLLAPSE) {
@ -168,9 +175,7 @@ export const transformToFilter = (
}; };
export const useAssociatedFields = () => { export const useAssociatedFields = () => {
const { fields } = useCollection_deprecated(); return useCollection()?.fields.filter((field) => isAssocField(field)) || [];
return fields.filter((field) => isAssocField(field)) || [];
}; };
export const isAssocField = (field?: FieldOptions) => { export const isAssocField = (field?: FieldOptions) => {