From 65fc83a763516e66ba2820723636241c9cfbaa5c Mon Sep 17 00:00:00 2001 From: Lucas Bordeau Date: Thu, 13 Jun 2024 18:28:02 +0200 Subject: [PATCH] Added a fallback default record chip generator (#5860) The record chip generator context was missing a edge were a new field of type relation is created and not yet in the metadata so no chip generator function can be precomputed. For now I added a fallback default chip generator, to prevent any bug, but we might want to add a new chip generator function while creating the new field ? --- .../utils/generateDefaultRecordChipData.ts | 15 +++++++++++++++ .../meta-types/hooks/useChipFieldDisplay.ts | 3 ++- .../meta-types/hooks/useRelationFieldDisplay.ts | 3 ++- 3 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 packages/twenty-front/src/modules/object-metadata/utils/generateDefaultRecordChipData.ts diff --git a/packages/twenty-front/src/modules/object-metadata/utils/generateDefaultRecordChipData.ts b/packages/twenty-front/src/modules/object-metadata/utils/generateDefaultRecordChipData.ts new file mode 100644 index 000000000000..82fe11744317 --- /dev/null +++ b/packages/twenty-front/src/modules/object-metadata/utils/generateDefaultRecordChipData.ts @@ -0,0 +1,15 @@ +import { isFieldFullNameValue } from '@/object-record/record-field/types/guards/isFieldFullNameValue'; +import { ObjectRecord } from '@/object-record/types/ObjectRecord'; + +export const generateDefaultRecordChipData = (record: ObjectRecord) => { + const name = isFieldFullNameValue(record.name) + ? record.name.firstName + ' ' + record.name.lastName + : record.name ?? ''; + + return { + name, + avatarUrl: name, + avatarType: 'rounded', + linkToShowPage: false, + }; +}; diff --git a/packages/twenty-front/src/modules/object-record/record-field/meta-types/hooks/useChipFieldDisplay.ts b/packages/twenty-front/src/modules/object-record/record-field/meta-types/hooks/useChipFieldDisplay.ts index eb7b0ebfabbc..29cf1d3dc651 100644 --- a/packages/twenty-front/src/modules/object-record/record-field/meta-types/hooks/useChipFieldDisplay.ts +++ b/packages/twenty-front/src/modules/object-record/record-field/meta-types/hooks/useChipFieldDisplay.ts @@ -2,6 +2,7 @@ import { useContext } from 'react'; import { isNonEmptyString } from '@sniptt/guards'; import { PreComputedChipGeneratorsContext } from '@/object-metadata/context/PreComputedChipGeneratorsContext'; +import { generateDefaultRecordChipData } from '@/object-metadata/utils/generateDefaultRecordChipData'; import { isFieldFullName } from '@/object-record/record-field/types/guards/isFieldFullName'; import { isFieldNumber } from '@/object-record/record-field/types/guards/isFieldNumber'; import { isFieldText } from '@/object-record/record-field/types/guards/isFieldText'; @@ -37,7 +38,7 @@ export const useChipFieldDisplay = () => { const generateRecordChipData = chipGeneratorPerObjectPerField[ fieldDefinition.metadata.objectMetadataNameSingular - ][fieldDefinition.metadata.fieldName]; + ]?.[fieldDefinition.metadata.fieldName] ?? generateDefaultRecordChipData; return { objectNameSingular, diff --git a/packages/twenty-front/src/modules/object-record/record-field/meta-types/hooks/useRelationFieldDisplay.ts b/packages/twenty-front/src/modules/object-record/record-field/meta-types/hooks/useRelationFieldDisplay.ts index a7af482ed76e..510cd7fbc2f4 100644 --- a/packages/twenty-front/src/modules/object-record/record-field/meta-types/hooks/useRelationFieldDisplay.ts +++ b/packages/twenty-front/src/modules/object-record/record-field/meta-types/hooks/useRelationFieldDisplay.ts @@ -2,6 +2,7 @@ import { useContext } from 'react'; import { isNonEmptyString } from '@sniptt/guards'; import { PreComputedChipGeneratorsContext } from '@/object-metadata/context/PreComputedChipGeneratorsContext'; +import { generateDefaultRecordChipData } from '@/object-metadata/utils/generateDefaultRecordChipData'; import { useRecordFieldValue } from '@/object-record/record-store/contexts/RecordFieldValueSelectorContext'; import { ObjectRecord } from '@/object-record/types/ObjectRecord'; import { FIELD_EDIT_BUTTON_WIDTH } from '@/ui/field/display/constants/FieldEditButtonWidth'; @@ -50,7 +51,7 @@ export const useRelationFieldDisplay = () => { const generateRecordChipData = chipGeneratorPerObjectPerField[ fieldDefinition.metadata.objectMetadataNameSingular - ][fieldDefinition.metadata.fieldName]; + ]?.[fieldDefinition.metadata.fieldName] ?? generateDefaultRecordChipData; return { fieldDefinition,