@@ -3,8 +3,8 @@ import lodashMapValues from 'lodash/mapValues';
3
3
import lodashSortBy from 'lodash/sortBy' ;
4
4
import type { ForwardedRef } from 'react' ;
5
5
import React , { forwardRef , useCallback , useEffect , useImperativeHandle , useMemo , useRef , useState } from 'react' ;
6
- import { useOnyx } from 'react-native-onyx' ;
7
6
import type { OnyxCollection } from 'react-native-onyx' ;
7
+ import { useOnyx } from 'react-native-onyx' ;
8
8
import * as Expensicons from '@components/Icon/Expensicons' ;
9
9
import type { Mention } from '@components/MentionSuggestions' ;
10
10
import MentionSuggestions from '@components/MentionSuggestions' ;
@@ -14,6 +14,7 @@ import useCurrentReportID from '@hooks/useCurrentReportID';
14
14
import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails' ;
15
15
import useDebounce from '@hooks/useDebounce' ;
16
16
import useLocalize from '@hooks/useLocalize' ;
17
+ import localeCompare from '@libs/LocaleCompare' ;
17
18
import * as LoginUtils from '@libs/LoginUtils' ;
18
19
import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils' ;
19
20
import getPolicyEmployeeAccountIDs from '@libs/PolicyEmployeeListUtils' ;
@@ -56,6 +57,30 @@ type SuggestionPersonalDetailsList = Record<
56
57
| null
57
58
> ;
58
59
60
+ function getDisplayName ( details : PersonalDetails ) {
61
+ const displayNameFromAccountID = ReportUtils . getDisplayNameForParticipant ( details . accountID ) ;
62
+ if ( ! displayNameFromAccountID ) {
63
+ return details . login ?. length ? details . login : '' ;
64
+ }
65
+ return displayNameFromAccountID ;
66
+ }
67
+
68
+ /**
69
+ * Comparison function to sort users. It compares weights, display names, and accountIDs in that order
70
+ */
71
+ function compareUserInList ( first : PersonalDetails & { weight : number } , second : PersonalDetails & { weight : number } ) {
72
+ if ( first . weight !== second . weight ) {
73
+ return first . weight - second . weight ;
74
+ }
75
+
76
+ const displayNameLoginOrder = localeCompare ( getDisplayName ( first ) , getDisplayName ( second ) ) ;
77
+ if ( displayNameLoginOrder !== 0 ) {
78
+ return displayNameLoginOrder ;
79
+ }
80
+
81
+ return first . accountID - second . accountID ;
82
+ }
83
+
59
84
function SuggestionMention (
60
85
{ value, selection, setSelection, updateComment, isAutoSuggestionPickerLarge, measureParentContainerAndReportCursor, isComposerFocused, isGroupPolicyReport, policyID} : SuggestionProps ,
61
86
ref : ForwardedRef < SuggestionsRef > ,
@@ -270,9 +295,11 @@ function SuggestionMention(
270
295
}
271
296
272
297
return true ;
273
- } ) ;
298
+ } ) as Array < PersonalDetails & { weight : number } > ;
299
+
300
+ // At this point we are sure that the details are not null, since empty user details have been filtered in the previous step
301
+ const sortedPersonalDetails = filteredPersonalDetails . sort ( compareUserInList ) ;
274
302
275
- const sortedPersonalDetails = lodashSortBy ( filteredPersonalDetails , [ 'weight' , 'displayName' , 'login' ] ) ;
276
303
sortedPersonalDetails . slice ( 0 , CONST . AUTO_COMPLETE_SUGGESTER . MAX_AMOUNT_OF_SUGGESTIONS - suggestions . length ) . forEach ( ( detail ) => {
277
304
suggestions . push ( {
278
305
text : formatLoginPrivateDomain ( PersonalDetailsUtils . getDisplayNameOrDefault ( detail ) , detail ?. login ) ,
@@ -443,3 +470,5 @@ function SuggestionMention(
443
470
SuggestionMention . displayName = 'SuggestionMention' ;
444
471
445
472
export default forwardRef ( SuggestionMention ) ;
473
+
474
+ export { compareUserInList } ;
0 commit comments