Skip to content

Commit

Permalink
Added a fallback default record chip generator (twentyhq#5860)
Browse files Browse the repository at this point in the history
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 ?
  • Loading branch information
lucasbordeau authored Jun 13, 2024
1 parent 582653f commit 65fc83a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -37,7 +38,7 @@ export const useChipFieldDisplay = () => {
const generateRecordChipData =
chipGeneratorPerObjectPerField[
fieldDefinition.metadata.objectMetadataNameSingular
][fieldDefinition.metadata.fieldName];
]?.[fieldDefinition.metadata.fieldName] ?? generateDefaultRecordChipData;

return {
objectNameSingular,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -50,7 +51,7 @@ export const useRelationFieldDisplay = () => {
const generateRecordChipData =
chipGeneratorPerObjectPerField[
fieldDefinition.metadata.objectMetadataNameSingular
][fieldDefinition.metadata.fieldName];
]?.[fieldDefinition.metadata.fieldName] ?? generateDefaultRecordChipData;

return {
fieldDefinition,
Expand Down

0 comments on commit 65fc83a

Please sign in to comment.