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) }) }