diff --git a/src/libs/PolicyUtils.ts b/src/libs/PolicyUtils.ts index a2390aae39a6..36ee7b07bacd 100644 --- a/src/libs/PolicyUtils.ts +++ b/src/libs/PolicyUtils.ts @@ -17,6 +17,11 @@ import {getAccountIDsByLogins, getLoginsByAccountIDs, getPersonalDetailByEmail} type MemberEmailsToAccountIDs = Record; +type WorkspaceDetails = { + policyID: string | undefined; + name: string; +}; + let allPolicies: OnyxCollection; Onyx.connect({ @@ -433,6 +438,22 @@ function getXeroBankAccountsWithDefaultSelect(policy: Policy | undefined, select })); } +/** + * Sort the workspaces by their name, while keeping the selected one at the beginning. + * @param workspace1 Details of the first workspace to be compared. + * @param workspace2 Details of the second workspace to be compared. + * @param selectedWorkspaceID ID of the selected workspace which needs to be at the beginning. + */ +const sortWorkspacesBySelected = (workspace1: WorkspaceDetails, workspace2: WorkspaceDetails, selectedWorkspaceID: string | undefined): number => { + if (workspace1.policyID === selectedWorkspaceID) { + return -1; + } + if (workspace2.policyID === selectedWorkspaceID) { + return 1; + } + return workspace1.name?.toLowerCase().localeCompare(workspace2.name?.toLowerCase() ?? '') ?? 0; +}; + export { canEditTaxRate, extractPolicyIDFromPath, @@ -483,6 +504,7 @@ export { findCurrentXeroOrganization, getCurrentXeroOrganizationName, getXeroBankAccountsWithDefaultSelect, + sortWorkspacesBySelected, }; export type {MemberEmailsToAccountIDs}; diff --git a/src/pages/WorkspaceSwitcherPage/index.tsx b/src/pages/WorkspaceSwitcherPage/index.tsx index c8c830e530f8..e09d61664000 100644 --- a/src/pages/WorkspaceSwitcherPage/index.tsx +++ b/src/pages/WorkspaceSwitcherPage/index.tsx @@ -12,6 +12,7 @@ import useLocalize from '@hooks/useLocalize'; import useNetwork from '@hooks/useNetwork'; import Navigation from '@libs/Navigation/Navigation'; import * as PolicyUtils from '@libs/PolicyUtils'; +import {sortWorkspacesBySelected} from '@libs/PolicyUtils'; import * as ReportUtils from '@libs/ReportUtils'; import {getWorkspacesBrickRoads, getWorkspacesUnreadStatuses} from '@libs/WorkspacesSettingsUtils'; import type {BrickRoad} from '@libs/WorkspacesSettingsUtils'; @@ -28,16 +29,6 @@ type WorkspaceListItem = { brickRoadIndicator?: BrickRoad; } & ListItem; -const sortWorkspacesBySelected = (workspace1: WorkspaceListItem, workspace2: WorkspaceListItem, selectedWorkspaceID: string | undefined): number => { - if (workspace1.policyID === selectedWorkspaceID) { - return -1; - } - if (workspace2.policyID === selectedWorkspaceID) { - return 1; - } - return workspace1.text?.toLowerCase().localeCompare(workspace2.text?.toLowerCase() ?? '') ?? 0; -}; - const WorkspaceCardCreateAWorkspaceInstance = ; function WorkspaceSwitcherPage() { @@ -132,7 +123,7 @@ function WorkspaceSwitcherPage() { () => usersWorkspaces .filter((policy) => policy.text?.toLowerCase().includes(debouncedSearchTerm?.toLowerCase() ?? '')) - .sort((policy1, policy2) => sortWorkspacesBySelected(policy1, policy2, activeWorkspaceID)), + .sort((policy1, policy2) => sortWorkspacesBySelected({policyID: policy1.policyID, name: policy1.text}, {policyID: policy2.policyID, name: policy2.text}, activeWorkspaceID)), [debouncedSearchTerm, usersWorkspaces, activeWorkspaceID], ); diff --git a/src/pages/iou/request/step/IOURequestStepSendFrom.tsx b/src/pages/iou/request/step/IOURequestStepSendFrom.tsx index 3bbff502d917..74a1370fb00c 100644 --- a/src/pages/iou/request/step/IOURequestStepSendFrom.tsx +++ b/src/pages/iou/request/step/IOURequestStepSendFrom.tsx @@ -8,6 +8,7 @@ import UserListItem from '@components/SelectionList/UserListItem'; import useLocalize from '@hooks/useLocalize'; import Navigation from '@libs/Navigation/Navigation'; import * as PolicyUtils from '@libs/PolicyUtils'; +import {sortWorkspacesBySelected} from '@libs/PolicyUtils'; import * as ReportUtils from '@libs/ReportUtils'; import * as IOU from '@userActions/IOU'; import CONST from '@src/CONST'; @@ -41,21 +42,24 @@ function IOURequestStepSendFrom({route, transaction, allPolicies}: IOURequestSte const workspaceOptions: WorkspaceListItem[] = useMemo(() => { const activeAdminWorkspaces = PolicyUtils.getActiveAdminWorkspaces(allPolicies); - return activeAdminWorkspaces.map((policy) => ({ - text: policy.name, - value: policy.id, - keyForList: policy.id, - icons: [ - { - id: policy.id, - source: policy?.avatarURL ? policy.avatarURL : ReportUtils.getDefaultWorkspaceAvatar(policy.name), - fallbackIcon: Expensicons.FallbackWorkspaceAvatar, - name: policy.name, - type: CONST.ICON_TYPE_WORKSPACE, - }, - ], - isSelected: selectedWorkspace?.policyID === policy.id, - })); + + return activeAdminWorkspaces + .sort((policy1, policy2) => sortWorkspacesBySelected({policyID: policy1.id, name: policy1.name}, {policyID: policy2.id, name: policy2.name}, selectedWorkspace?.policyID)) + .map((policy) => ({ + text: policy.name, + value: policy.id, + keyForList: policy.id, + icons: [ + { + id: policy.id, + source: policy?.avatarURL ? policy.avatarURL : ReportUtils.getDefaultWorkspaceAvatar(policy.name), + fallbackIcon: Expensicons.FallbackWorkspaceAvatar, + name: policy.name, + type: CONST.ICON_TYPE_WORKSPACE, + }, + ], + isSelected: selectedWorkspace?.policyID === policy.id, + })); }, [allPolicies, selectedWorkspace]); const navigateBack = () => {