Repository: twentyhq/twenty
File: packages/twenty-front/src/modules/object-record/record-table/hooks/useCreateNewIndexRecord.ts (TypeScript)

Code before:
            getLabelIdentifierFieldMetadataItem(objectMetadataItem);

          if (isDefined(labelIdentifierFieldMetadataItem)) {
            openRecordTitleCell({
              recordId,
              fieldName: labelIdentifierFieldMetadataItem.name,
              instanceId: getRecordFieldInputInstanceId({
                recordId,
                fieldName: labelIdentifierFieldMetadataItem.name,
                prefix: RecordTitleCellContainerType.PageHeader,
              }),
            });
          }
        } else {
          navigate(AppPath.RecordShowPage, {
            objectNameSingular: objectMetadataItem.nameSingular,
            objectRecordId: recordId,
          });
        }

        if (isDefined(recordIndexGroupFieldMetadataItem)) {
          const recordGroup = recordGroupDefinitions.find(
            findByProperty(
              'value',
              createdRecord[recordIndexGroupFieldMetadataItem.name],
            ),
          );

          if (isDefined(recordGroup)) {
            const currentRecordIds = getSnapshotValue(
              snapshot,
              recordIndexRecordIdsByGroupCallbackState(recordGroup.id),
            );

            if (recordInput?.position === 'first') {
              const newRecordIds = [createdRecord.id, ...currentRecordIds];

              set(
                recordIndexRecordIdsByGroupCallbackState(recordGroup.id),
                newRecordIds,
              );
            } else {
              const newRecordIds = [...currentRecordIds, createdRecord.id];

              set(
                recordIndexRecordIdsByGroupCallbackState(recordGroup.id),
                newRecordIds,
              );
            }
          }
        }

Review comment (suggestion):
This is not the most idiomatic way of doing things. Try to take more inspiration from what exists instead of adding new behavior (e.g. recoil state here). I'll do a quick POC to show you

Code after:
            getLabelIdentifierFieldMetadataItem(objectMetadataItem);

          if (isDefined(labelIdentifierFieldMetadataItem)) {
            openRecordTitleCell({
              recordId,
              fieldName: labelIdentifierFieldMetadataItem.name,
              instanceId: getRecordFieldInputInstanceId({
                recordId,
                fieldName: labelIdentifierFieldMetadataItem.name,
                prefix: RecordTitleCellContainerType.PageHeader,
              }),
            });
          }
        } else {
          const labelIdentifierFieldMetadataItem =
            getLabelIdentifierFieldMetadataItem(objectMetadataItem);

          navigate(
            AppPath.RecordShowPage,
            {
              objectNameSingular: objectMetadataItem.nameSingular,
              objectRecordId: recordId,
            },
            undefined,
            {
              state: {
                isNewRecord: true,
                objectRecordId: recordId,
                labelIdentifierFieldName:
                  labelIdentifierFieldMetadataItem?.name,
              },
            },
          );
        }

        if (isDefined(recordIndexGroupFieldMetadataItem)) {
          const recordGroup = recordGroupDefinitions.find(
            findByProperty(
              'value',
              createdRecord[recordIndexGroupFieldMetadataItem.name],
            ),
          );

          if (isDefined(recordGroup)) {
            const currentRecordIds = getSnapshotValue(
              snapshot,
              recordIndexRecordIdsByGroupCallbackState(recordGroup.id),
            );

            if (recordInput?.position === 'first') {
              const newRecordIds = [createdRecord.id, ...currentRecordIds];