@@ -11,6 +11,7 @@ import useScreenWrapperTransitionStatus from '@hooks/useScreenWrapperTransitionS
1111import useSearchSelector from '@hooks/useSearchSelector' ;
1212import { canUseTouchScreen } from '@libs/DeviceCapabilities' ;
1313import { formatSectionsFromSearchTerm , getFilteredRecentAttendees , getParticipantsOption } from '@libs/OptionsListUtils' ;
14+ import { doesPersonalDetailMatchSearchTerm } from '@libs/OptionsListUtils/searchMatchUtils' ;
1415import type { OptionData } from '@libs/ReportUtils' ;
1516import { getDisplayNameForParticipant } from '@libs/ReportUtils' ;
1617import Navigation from '@navigation/Navigation' ;
@@ -76,18 +77,19 @@ function SearchFiltersParticipantsSelector({initialAccountIDs, onFiltersUpdate,
7677 [ personalDetails , recentAttendees , currentUserEmail , currentUserAccountID , shouldAllowNameOnlyOptions ] ,
7778 ) ;
7879
79- const { searchTerm, setSearchTerm, availableOptions, selectedOptions, setSelectedOptions, toggleSelection, areOptionsInitialized, onListEndReached} = useSearchSelector ( {
80- selectionMode : CONST . SEARCH_SELECTOR . SELECTION_MODE_MULTI ,
81- searchContext : CONST . SEARCH_SELECTOR . SEARCH_CONTEXT_GENERAL ,
82- maxRecentReportsToShow : CONST . IOU . MAX_RECENT_REPORTS_TO_SHOW ,
83- includeUserToInvite : true ,
84- excludeLogins : CONST . EXPENSIFY_EMAILS_OBJECT ,
85- includeRecentReports : true ,
86- shouldInitialize : didScreenTransitionEnd ,
87- includeCurrentUser : true ,
88- recentAttendees : recentAttendeeLists ,
89- shouldAllowNameOnlyOptions,
90- } ) ;
80+ const { searchTerm, debouncedSearchTerm, setSearchTerm, availableOptions, selectedOptions, setSelectedOptions, toggleSelection, areOptionsInitialized, onListEndReached} =
81+ useSearchSelector ( {
82+ selectionMode : CONST . SEARCH_SELECTOR . SELECTION_MODE_MULTI ,
83+ searchContext : CONST . SEARCH_SELECTOR . SEARCH_CONTEXT_GENERAL ,
84+ maxRecentReportsToShow : CONST . IOU . MAX_RECENT_REPORTS_TO_SHOW ,
85+ includeUserToInvite : true ,
86+ excludeLogins : CONST . EXPENSIFY_EMAILS_OBJECT ,
87+ includeRecentReports : true ,
88+ shouldInitialize : didScreenTransitionEnd ,
89+ includeCurrentUser : true ,
90+ recentAttendees : recentAttendeeLists ,
91+ shouldAllowNameOnlyOptions,
92+ } ) ;
9193
9294 const { sections, headerMessage} = useMemo ( ( ) => {
9395 const newSections = [ ] ;
@@ -104,7 +106,7 @@ function SearchFiltersParticipantsSelector({initialAccountIDs, onFiltersUpdate,
104106 }
105107
106108 const formattedResults = formatSectionsFromSearchTerm (
107- searchTerm . trim ( ) . toLowerCase ( ) ,
109+ debouncedSearchTerm . trim ( ) . toLowerCase ( ) ,
108110 selectedOptions ,
109111 chatOptions . recentReports ,
110112 chatOptions . personalDetails ,
@@ -116,7 +118,7 @@ function SearchFiltersParticipantsSelector({initialAccountIDs, onFiltersUpdate,
116118 undefined ,
117119 reportAttributesDerived ,
118120 ) ;
119- const selectedCurrentUser = formattedResults . section . data . find ( ( option ) => option . accountID === chatOptions . currentUserOption ?. accountID ) ;
121+ const selectedCurrentUser = formattedResults . section . data . find ( ( option ) => option . accountID === currentUserAccountID ) ;
120122
121123 // If the current user is already selected, remove them from the recent reports and personal details
122124 if ( selectedCurrentUser ) {
@@ -125,20 +127,33 @@ function SearchFiltersParticipantsSelector({initialAccountIDs, onFiltersUpdate,
125127 }
126128
127129 // If the current user is not selected, add them to the top of the list
128- if ( ! selectedCurrentUser && chatOptions . currentUserOption ) {
129- const formattedName = getDisplayNameForParticipant ( {
130- accountID : chatOptions . currentUserOption . accountID ,
131- shouldAddCurrentUserPostfix : true ,
132- personalDetailsData : personalDetails ,
133- formatPhoneNumber,
134- } ) ;
135- chatOptions . currentUserOption . text = formattedName ;
136-
137- newSections . push ( {
138- title : '' ,
139- data : [ chatOptions . currentUserOption ] ,
140- sectionIndex : 0 ,
141- } ) ;
130+ // Falls back to creating from personal details to handle pagination edge cases
131+ if ( ! selectedCurrentUser ) {
132+ let currentUserOptionToShow = chatOptions . currentUserOption ;
133+ const currentUserDetails = currentUserAccountID ? personalDetails ?. [ currentUserAccountID ] : undefined ;
134+ if ( ! currentUserOptionToShow && currentUserAccountID && currentUserDetails ) {
135+ const candidateOption = getParticipantsOption ( currentUserDetails , personalDetails ) as OptionData ;
136+ const trimmedSearchTerm = debouncedSearchTerm . trim ( ) . toLowerCase ( ) ;
137+ if ( ! trimmedSearchTerm || doesPersonalDetailMatchSearchTerm ( candidateOption , currentUserAccountID , trimmedSearchTerm ) ) {
138+ currentUserOptionToShow = candidateOption ;
139+ }
140+ }
141+
142+ if ( currentUserOptionToShow ) {
143+ const formattedName = getDisplayNameForParticipant ( {
144+ accountID : currentUserOptionToShow . accountID ,
145+ shouldAddCurrentUserPostfix : true ,
146+ personalDetailsData : personalDetails ,
147+ formatPhoneNumber,
148+ } ) ;
149+ currentUserOptionToShow . text = formattedName ;
150+
151+ newSections . push ( {
152+ title : '' ,
153+ data : [ currentUserOptionToShow ] ,
154+ sectionIndex : 0 ,
155+ } ) ;
156+ }
142157 }
143158
144159 newSections . push ( {
@@ -175,7 +190,7 @@ function SearchFiltersParticipantsSelector({initialAccountIDs, onFiltersUpdate,
175190 } , [
176191 areOptionsInitialized ,
177192 availableOptions ,
178- searchTerm ,
193+ debouncedSearchTerm ,
179194 selectedOptions ,
180195 privateIsArchivedMap ,
181196 currentUserAccountID ,
0 commit comments