30 lines
1011 B
TypeScript
30 lines
1011 B
TypeScript
import { useObjectMetadataItemForSettings } from '@/object-metadata/hooks/useObjectMetadataItemForSettings';
|
|
import { useFindManyObjectRecords } from '@/object-record/hooks/useFindManyObjectRecords';
|
|
import { capitalize } from '~/utils/string/capitalize';
|
|
|
|
export const useRelationFieldPreview = ({
|
|
relationObjectMetadataId,
|
|
skipDefaultValue,
|
|
}: {
|
|
relationObjectMetadataId?: string;
|
|
skipDefaultValue: boolean;
|
|
}) => {
|
|
const { findObjectMetadataItemById } = useObjectMetadataItemForSettings();
|
|
|
|
const relationObjectMetadataItem = relationObjectMetadataId
|
|
? findObjectMetadataItemById(relationObjectMetadataId)
|
|
: undefined;
|
|
|
|
const { objects: relationObjects } = useFindManyObjectRecords({
|
|
objectNamePlural: relationObjectMetadataItem?.namePlural,
|
|
skip: skipDefaultValue || !relationObjectMetadataItem,
|
|
});
|
|
|
|
return {
|
|
relationObjectMetadataItem,
|
|
defaultValue: relationObjects?.[0] ?? {
|
|
name: capitalize(relationObjectMetadataItem?.nameSingular ?? ''),
|
|
},
|
|
};
|
|
};
|