Skip to content

Commit 9df24a1

Browse files
authored
Merge pull request Expensify#67068 from shubham1206agra/refactor-onyx-7
Refactored localeCompare in SuggestionUtils
2 parents 9cdb9ce + 64678d1 commit 9df24a1

File tree

4 files changed

+50
-53
lines changed

4 files changed

+50
-53
lines changed

src/libs/SuggestionUtils.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import type {LocaleContextProps} from '@components/LocaleContextProvider';
12
import CONST from '@src/CONST';
23
import type {PersonalDetails} from '@src/types/onyx';
3-
import localeCompare from './LocaleCompare';
44
import {getDisplayNameForParticipant} from './ReportUtils';
55

66
/**
@@ -32,19 +32,21 @@ function getDisplayName(details: PersonalDetails) {
3232
}
3333

3434
/**
35-
* Comparison function to sort users. It compares weights, display names, and accountIDs in that order
35+
* Function to sort users. It compares weights, display names, and accountIDs in that order
3636
*/
37-
function compareUserInList(first: PersonalDetails & {weight: number}, second: PersonalDetails & {weight: number}) {
38-
if (first.weight !== second.weight) {
39-
return first.weight - second.weight;
40-
}
37+
function getSortedPersonalDetails(personalDetails: Array<PersonalDetails & {weight: number}>, localeCompare: LocaleContextProps['localeCompare']) {
38+
return personalDetails.sort((first, second) => {
39+
if (first.weight !== second.weight) {
40+
return first.weight - second.weight;
41+
}
4142

42-
const displayNameLoginOrder = localeCompare(getDisplayName(first), getDisplayName(second));
43-
if (displayNameLoginOrder !== 0) {
44-
return displayNameLoginOrder;
45-
}
43+
const displayNameLoginOrder = localeCompare(getDisplayName(first), getDisplayName(second));
44+
if (displayNameLoginOrder !== 0) {
45+
return displayNameLoginOrder;
46+
}
4647

47-
return first.accountID - second.accountID;
48+
return first.accountID - second.accountID;
49+
});
4850
}
4951

50-
export {trimLeadingSpace, hasEnoughSpaceForLargeSuggestionMenu, compareUserInList};
52+
export {trimLeadingSpace, hasEnoughSpaceForLargeSuggestionMenu, getSortedPersonalDetails};

src/pages/home/report/ReportActionCompose/SuggestionMention.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {getDisplayNameOrDefault} from '@libs/PersonalDetailsUtils';
1919
import getPolicyEmployeeAccountIDs from '@libs/PolicyEmployeeListUtils';
2020
import {canReportBeMentionedWithinPolicy, doesReportBelongToWorkspace, isGroupChat, isReportParticipant} from '@libs/ReportUtils';
2121
import StringUtils from '@libs/StringUtils';
22-
import {compareUserInList, trimLeadingSpace} from '@libs/SuggestionUtils';
22+
import {getSortedPersonalDetails, trimLeadingSpace} from '@libs/SuggestionUtils';
2323
import {isValidRoomName} from '@libs/ValidationUtils';
2424
import {searchInServer} from '@userActions/Report';
2525
import CONST from '@src/CONST';
@@ -62,7 +62,7 @@ function SuggestionMention(
6262
ref: ForwardedRef<SuggestionsRef>,
6363
) {
6464
const personalDetails = usePersonalDetails();
65-
const {translate, formatPhoneNumber} = useLocalize();
65+
const {translate, formatPhoneNumber, localeCompare} = useLocalize();
6666
const [suggestionValues, setSuggestionValues] = useState(defaultSuggestionsValues);
6767
const suggestionValuesRef = useRef(suggestionValues);
6868
// eslint-disable-next-line react-compiler/react-compiler
@@ -303,7 +303,7 @@ function SuggestionMention(
303303
}) as Array<PersonalDetails & {weight: number}>;
304304

305305
// At this point we are sure that the details are not null, since empty user details have been filtered in the previous step
306-
const sortedPersonalDetails = filteredPersonalDetails.sort(compareUserInList);
306+
const sortedPersonalDetails = getSortedPersonalDetails(filteredPersonalDetails, localeCompare);
307307

308308
sortedPersonalDetails.slice(0, CONST.AUTO_COMPLETE_SUGGESTER.MAX_AMOUNT_OF_SUGGESTIONS - suggestions.length).forEach((detail) => {
309309
suggestions.push({
@@ -324,7 +324,7 @@ function SuggestionMention(
324324

325325
return suggestions;
326326
},
327-
[translate, formatPhoneNumber, formatLoginPrivateDomain],
327+
[translate, formatPhoneNumber, formatLoginPrivateDomain, localeCompare],
328328
);
329329

330330
const getRoomMentionOptions = useCallback(

tests/unit/SuggestionUtils.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import {getSortedPersonalDetails} from '@libs/SuggestionUtils';
2+
import {localeCompare} from '../utils/TestHelper';
3+
4+
describe('SuggestionUtils', () => {
5+
describe('getSortedPersonalDetails', () => {
6+
it('Should sort using the weight if the weight is different', () => {
7+
const first = {login: 'John Doe', weight: 1, accountID: 1};
8+
const second = {login: 'Jane Doe', weight: 2, accountID: 2};
9+
expect(getSortedPersonalDetails([second, first], localeCompare)).toEqual([first, second]);
10+
});
11+
12+
it('Should sort using the displayName if the weight is the same', () => {
13+
const first = {login: 'águero', weight: 2, accountID: 3};
14+
const second = {login: 'Bronn', weight: 2, accountID: 4};
15+
const third = {login: 'Carol', weight: 2, accountID: 5};
16+
expect(getSortedPersonalDetails([second, first, third], localeCompare)).toEqual([first, second, third]);
17+
expect(getSortedPersonalDetails([third, second, first], localeCompare)).toEqual([first, second, third]);
18+
});
19+
20+
it('Should sort using the accountID if both the weight and displayName are the same', () => {
21+
const first = {login: 'aguero', weight: 2, accountID: 6};
22+
const second = {login: 'aguero', weight: 2, accountID: 7};
23+
expect(getSortedPersonalDetails([second, first], localeCompare)).toEqual([first, second]);
24+
});
25+
26+
it('Should sort using the displayName with different diacritics if the weight is the same', () => {
27+
const first = {login: 'águero', weight: 2, accountID: 8};
28+
const second = {login: 'aguero', weight: 2, accountID: 8};
29+
expect(getSortedPersonalDetails([second, first], localeCompare)).toEqual([second, first]);
30+
});
31+
});
32+
});

tests/unit/compareUserInListTest.ts

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)