fix: fix Relation field preview (#2605)

Fixes #2586
This commit is contained in:
Thaïs
2023-11-21 00:14:16 +01:00
committed by GitHub
parent 0ace17df82
commit 1e79d4351e
2 changed files with 24 additions and 6 deletions

View File

@ -1,5 +1,7 @@
import { useObjectMainIdentifier } from '@/object-metadata/hooks/useObjectMainIdentifier';
import { useObjectMetadataItemForSettings } from '@/object-metadata/hooks/useObjectMetadataItemForSettings';
import { useFindManyObjectRecords } from '@/object-record/hooks/useFindManyObjectRecords';
import { MainIdentifierMapper } from '@/ui/object/field/types/MainIdentifierMapper';
export const useRelationFieldPreview = ({
relationObjectMetadataId,
@ -19,11 +21,24 @@ export const useRelationFieldPreview = ({
skip: skipDefaultValue || !relationObjectMetadataItem,
});
const { mainIdentifierMapper } = useObjectMainIdentifier(
relationObjectMetadataItem,
);
const recordMapper: MainIdentifierMapper | undefined =
relationObjectMetadataItem && mainIdentifierMapper
? (record: { id: string }) => {
const mappedRecord = mainIdentifierMapper(record);
return {
...mappedRecord,
name: mappedRecord.name || relationObjectMetadataItem.labelSingular,
};
}
: undefined;
return {
defaultValue: relationObjects?.[0],
entityChipDisplayMapper: (fieldValue?: { id: string }) => ({
name: fieldValue?.id || relationObjectMetadataItem?.labelSingular || '',
avatarType: 'squared' as const,
}),
recordMapper,
};
};