Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions src/libs/SuggestionUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {LocaleContextProps} from '@components/LocaleContextProvider';
import CONST from '@src/CONST';
import type {PersonalDetails} from '@src/types/onyx';
import localeCompare from './LocaleCompare';
import {getDisplayNameForParticipant} from './ReportUtils';

/**
Expand Down Expand Up @@ -32,21 +32,19 @@ function getDisplayName(details: PersonalDetails) {
}

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

const displayNameLoginOrder = localeCompare(getDisplayName(first), getDisplayName(second));
if (displayNameLoginOrder !== 0) {
return displayNameLoginOrder;
}
const displayNameLoginOrder = localeCompare(getDisplayName(first), getDisplayName(second));
if (displayNameLoginOrder !== 0) {
return displayNameLoginOrder;
}

return first.accountID - second.accountID;
});
return first.accountID - second.accountID;
}

export {trimLeadingSpace, hasEnoughSpaceForLargeSuggestionMenu, getSortedPersonalDetails};
export {trimLeadingSpace, hasEnoughSpaceForLargeSuggestionMenu, compareUserInList};
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {getDisplayNameOrDefault} from '@libs/PersonalDetailsUtils';
import getPolicyEmployeeAccountIDs from '@libs/PolicyEmployeeListUtils';
import {canReportBeMentionedWithinPolicy, doesReportBelongToWorkspace, isGroupChat, isReportParticipant} from '@libs/ReportUtils';
import StringUtils from '@libs/StringUtils';
import {getSortedPersonalDetails, trimLeadingSpace} from '@libs/SuggestionUtils';
import {compareUserInList, trimLeadingSpace} from '@libs/SuggestionUtils';
import {isValidRoomName} from '@libs/ValidationUtils';
import {searchInServer} from '@userActions/Report';
import CONST from '@src/CONST';
Expand Down Expand Up @@ -62,7 +62,7 @@ function SuggestionMention(
ref: ForwardedRef<SuggestionsRef>,
) {
const personalDetails = usePersonalDetails();
const {translate, formatPhoneNumber, localeCompare} = useLocalize();
const {translate, formatPhoneNumber} = useLocalize();
const [suggestionValues, setSuggestionValues] = useState(defaultSuggestionsValues);
const suggestionValuesRef = useRef(suggestionValues);
// eslint-disable-next-line react-compiler/react-compiler
Expand Down Expand Up @@ -303,7 +303,7 @@ function SuggestionMention(
}) as Array<PersonalDetails & {weight: number}>;

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

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

return suggestions;
},
[translate, formatPhoneNumber, formatLoginPrivateDomain, localeCompare],
[translate, formatPhoneNumber, formatLoginPrivateDomain],
);

const getRoomMentionOptions = useCallback(
Expand Down
32 changes: 0 additions & 32 deletions tests/unit/SuggestionUtils.ts

This file was deleted.

37 changes: 37 additions & 0 deletions tests/unit/compareUserInListTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {compareUserInList} from '@libs/SuggestionUtils';

describe('compareUserInList', () => {
it('Should compare the weight if the weight is different', () => {
const first = {login: 'John Doe', weight: 1, accountID: 1};
const second = {login: 'Jane Doe', weight: 2, accountID: 2};
expect(compareUserInList(first, second)).toBe(-1);
expect(compareUserInList(second, first)).toBe(1);
});

it('Should compare the displayName if the weight is the same', () => {
const first = {login: 'águero', weight: 2, accountID: 3};
const second = {login: 'Bronn', weight: 2, accountID: 4};
const third = {login: 'Carol', weight: 2, accountID: 5};
expect(compareUserInList(first, second)).toBe(-1);
expect(compareUserInList(first, third)).toBe(-1);
expect(compareUserInList(second, third)).toBe(-1);

expect(compareUserInList(second, first)).toBe(1);
expect(compareUserInList(third, first)).toBe(1);
expect(compareUserInList(third, second)).toBe(1);
});

it('Should compare the accountID if both the weight and displayName are the same', () => {
const first = {login: 'aguero', weight: 2, accountID: 6};
const second = {login: 'aguero', weight: 2, accountID: 7};
expect(compareUserInList(first, second)).toBe(-1);
expect(compareUserInList(second, first)).toBe(1);
});

it('Should compare the displayName with different diacritics if the weight is the same', () => {
const first = {login: 'águero', weight: 2, accountID: 8};
const second = {login: 'aguero', weight: 2, accountID: 8};
expect(compareUserInList(first, second)).toBe(1);
expect(compareUserInList(second, first)).toBe(-1);
});
});
Loading