From 64be82b0ed4244455a90d019b5ea065b3cd3a165 Mon Sep 17 00:00:00 2001 From: Sakibul Islam Date: Thu, 28 Nov 2024 20:49:48 +0000 Subject: [PATCH] Refactor getInitialSortedFields --- .../hubspot/src/components/FieldMapper.tsx | 27 +++++++------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/plugins/hubspot/src/components/FieldMapper.tsx b/plugins/hubspot/src/components/FieldMapper.tsx index feade3d0..5f561fe6 100644 --- a/plugins/hubspot/src/components/FieldMapper.tsx +++ b/plugins/hubspot/src/components/FieldMapper.tsx @@ -28,26 +28,17 @@ const getInitialSortedFields = ( isFieldSelected: (fieldId: string) => boolean ): ManagedCollectionFieldConfig[] => { return [...fields].sort((a, b) => { - const aIsSelected = a.field && isFieldSelected(a.field.id) - const bIsSelected = b.field && isFieldSelected(b.field.id) + const aSelected = a.field?.id && isFieldSelected(a.field.id) + const bSelected = b.field?.id && isFieldSelected(b.field.id) + if (aSelected !== bSelected) return bSelected ? 1 : -1 - // Sort based on whether the fields are selected - if (aIsSelected && !bIsSelected) return -1 - if (!aIsSelected && bIsSelected) return 1 + const getFieldValue = (field: typeof a.field) => { + if (field === undefined) return 0 // unsupported fields last + if (field === null) return 1 // missing references next + return 2 // valid fields first + } - // Sort by whether they are supported fields - if (a.field !== null && a.field !== undefined && (b.field === null || b.field === undefined)) return -1 - if ((a.field === null || a.field === undefined) && b.field !== null && b.field !== undefined) return 1 - - // Sort by whether they are null (missing reference) - if (a.field === null && b.field !== null) return -1 - if (a.field !== null && b.field === null) return 1 - - // Sort by whether they are undefined (unsupported fields) - if (a.field === undefined && b.field !== undefined) return 1 - if (a.field !== undefined && b.field === undefined) return -1 - - return 0 + return getFieldValue(b.field) - getFieldValue(a.field) }) }