Skip to content

Commit

Permalink
Refactor getInitialSortedFields
Browse files Browse the repository at this point in the history
  • Loading branch information
Sakib25800 committed Nov 28, 2024
1 parent 0fc0284 commit 64be82b
Showing 1 changed file with 9 additions and 18 deletions.
27 changes: 9 additions & 18 deletions plugins/hubspot/src/components/FieldMapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}

Expand Down

0 comments on commit 64be82b

Please sign in to comment.