Skip to content

Commit

Permalink
Make sortAlphabetically more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
blazejkustra committed Aug 26, 2024
1 parent 5c4b98c commit f7a701a
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 11 deletions.
8 changes: 1 addition & 7 deletions src/libs/OptionsListUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,6 @@ type FilterOptionsConfig = Pick<GetOptionsConfig, 'sortByReportTypeInSearch' | '
preferPolicyExpenseChat?: boolean;
};

type HasText = {
text?: string;
};

/**
* OptionsListUtils is used to build a list options passed to the OptionsList component. Several different UI views can
* be configured to display different results based on the options passed to the private getOptions() method. Public
Expand Down Expand Up @@ -2479,9 +2475,7 @@ function filterOptions(options: Options, searchInputValue: string, config?: Filt
};
}

function sortAlphabetically<T extends HasText>(items: T[]): T[];
function sortAlphabetically<T extends Record<TKey, string | undefined>, TKey extends keyof T>(items: T[], key: TKey): T[];
function sortAlphabetically<T extends Record<TKey, string | undefined>, TKey extends keyof T>(items: T[], key: TKey = 'text' as TKey): T[] {
function sortAlphabetically<T extends Partial<Record<TKey, string | undefined>>, TKey extends keyof T>(items: T[], key: TKey): T[] {
return items.sort((a, b) => (a[key] ?? '').toLowerCase().localeCompare((b[key] ?? '').toLowerCase()));
}

Expand Down
2 changes: 1 addition & 1 deletion src/pages/workspace/WorkspaceMembersPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ function WorkspaceMembersPage({personalDetails, route, policy, currentUserPerson
invitedSecondaryLogin: details?.login ? invitedPrimaryToSecondaryLogins[details.login] ?? '' : '',
});
});
result = OptionsListUtils.sortAlphabetically(result);
result = OptionsListUtils.sortAlphabetically(result, 'text');
return result;
}, [
isOffline,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function AssigneeStep({policy}: AssigneeStepProps) {
});
});

membersList = OptionsListUtils.sortAlphabetically(membersList);
membersList = OptionsListUtils.sortAlphabetically(membersList, 'text');

return membersList;
}, [isOffline, policy?.employeeList]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ function WorkspaceWorkflowsApprovalsApproverPageBeta({policy, personalDetails, i
return [
{
title: undefined,
data: OptionsListUtils.sortAlphabetically(filteredApprovers),
data: OptionsListUtils.sortAlphabetically(filteredApprovers, 'text'),
shouldShow: true,
},
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ function WorkspaceWorkflowsApprovalsExpensesFromPage({policy, isLoadingReportDat
return [
{
title: undefined,
data: OptionsListUtils.sortAlphabetically(filteredMembers),
data: OptionsListUtils.sortAlphabetically(filteredMembers, 'text'),
shouldShow: true,
},
];
Expand Down

0 comments on commit f7a701a

Please sign in to comment.