Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use localeCompare to sort in mention list #45324

Merged
merged 8 commits into from
Jul 31, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import useCurrentReportID from '@hooks/useCurrentReportID';
import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails';
import useDebounce from '@hooks/useDebounce';
import useLocalize from '@hooks/useLocalize';
import localeCompare from '@libs/LocaleCompare';
import * as LoginUtils from '@libs/LoginUtils';
import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils';
import getPolicyEmployeeAccountIDs from '@libs/PolicyEmployeeListUtils';
Expand Down Expand Up @@ -270,9 +271,29 @@ function SuggestionMention(
}

return true;
}) as Array<PersonalDetails & {weight: number}>; // at this point we are sure that the details are not null

const sortedPersonalDetails = filteredPersonalDetails.sort((first, second) => {
// first, we should sort by weight
if (first.weight !== second.weight) {
return first.weight - second.weight;
}

// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
const firstDisplayName = ReportUtils.getDisplayNameForParticipant(first.accountID) || first?.login || '';

// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
const secondDisplayName = ReportUtils.getDisplayNameForParticipant(second.accountID) || second?.login || '';

const displayNameLoginOrder = localeCompare(firstDisplayName, secondDisplayName);
if (displayNameLoginOrder !== 0) {
return displayNameLoginOrder;
}

// Then fallback on accountID as the final sorting criteria.
return first.accountID - second.accountID;
});

const sortedPersonalDetails = lodashSortBy(filteredPersonalDetails, ['weight', 'displayName', 'login']);
sortedPersonalDetails.slice(0, CONST.AUTO_COMPLETE_SUGGESTER.MAX_AMOUNT_OF_SUGGESTIONS - suggestions.length).forEach((detail) => {
suggestions.push({
text: formatLoginPrivateDomain(PersonalDetailsUtils.getDisplayNameOrDefault(detail), detail?.login),
Expand Down
Loading