Repository: twentyhq/twenty
File: packages/twenty-front/src/modules/object-record/record-field/ui/meta-types/input/hooks/useMorphPersistManyToOne.ts (TypeScript)

Code before:

  const { updateOneRecord } = useUpdateOneRecordV2();

  const persistMorphManyToOne = useRecoilCallback(
    ({ snapshot }) =>
      async ({
        recordId,
        fieldDefinition,
        valueToPersist,
        targetObjectMetadataNameSingular,
      }: {
        recordId: string;
        fieldDefinition: FieldDefinition<FieldMetadata>;
        valueToPersist: string | null | undefined;
        targetObjectMetadataNameSingular?: string;
      }) => {
        assertFieldMetadata(
          FieldMetadataType.MORPH_RELATION,
          isFieldMorphRelation,
          fieldDefinition,
        );

        const fieldName = fieldDefinition.metadata.fieldName;

        if (!isDefined(valueToPersist)) {
          const allNullRecordInput: Record<string, null> =
            fieldDefinition.metadata.morphRelations.reduce(
              (acc, morphRelation) => {
                const computedFieldName = computeMorphRelationFieldName({
                  fieldName,
                  relationType: fieldDefinition.metadata.relationType,
                  targetObjectMetadataNameSingular:
                    morphRelation.targetObjectMetadata.nameSingular,
                  targetObjectMetadataNamePlural:
                    morphRelation.targetObjectMetadata.namePlural,
                });
                acc[`${computedFieldName}Id`] = null;
                return acc;
              },
              {} as Record<string, null>,
            );

          updateOneRecord?.({
            objectNameSingular: objectMetadataNameSingular,
            idToUpdate: recordId,
            updateOneRecordInput: allNullRecordInput,
          });

          return;
        }


Review comment (suggestion):
Ok I would add context because I had to read the reducer to understand what it does, something like `recordWithAllMorphObjectIdsToNull` would make more self-explanatory.

Code after:
  const { objectMetadataItems } = useObjectMetadataItems();

  const { updateOneRecord } = useUpdateOneRecordV2();

  const persistMorphManyToOne = useRecoilCallback(
    ({ snapshot }) =>
      async ({
        recordId,
        fieldDefinition,
        valueToPersist,
        targetObjectMetadataNameSingular,
      }: {
        recordId: string;
        fieldDefinition: FieldDefinition<FieldMetadata>;
        valueToPersist: string | null | undefined;
        targetObjectMetadataNameSingular?: string;
      }) => {
        assertFieldMetadata(
          FieldMetadataType.MORPH_RELATION,
          isFieldMorphRelation,
          fieldDefinition,
        );

        const fieldName = fieldDefinition.metadata.fieldName;

        if (!isDefined(valueToPersist)) {
          const recordWithAllMorphObjectIdsToNull =
            buildRecordWithAllMorphObjectIdsToNull({
              morphRelations: fieldDefinition.metadata.morphRelations,
              fieldName,
              relationType: fieldDefinition.metadata.relationType,
            });

          updateOneRecord?.({
            objectNameSingular: objectMetadataNameSingular,
            idToUpdate: recordId,
            updateOneRecordInput: recordWithAllMorphObjectIdsToNull,
          });

          return;
        }

        const targetObjectMetadataItem = objectMetadataItems.find(
          (objectMetadataItem) =>
            objectMetadataItem.nameSingular ===
            targetObjectMetadataNameSingular,
        );

        if (!isDefined(targetObjectMetadataItem)) {
          throw new Error('Object metadata item not found');
        }