11import React , { useCallback , useEffect , useMemo , useState } from 'react' ;
22import { usePersonalDetails } from '@components/OnyxListItemProvider' ;
3+ import { useOptionsList } from '@components/OptionListContextProvider' ;
34import SelectionList from '@components/SelectionList' ;
45import UserSelectionListItem from '@components/SelectionList/Search/UserSelectionListItem' ;
56import useLocalize from '@hooks/useLocalize' ;
67import useOnyx from '@hooks/useOnyx' ;
78import useScreenWrapperTransitionStatus from '@hooks/useScreenWrapperTransitionStatus' ;
8- import useSearchParticipantsOptions from '@hooks/useSearchParticipantsOptions' ;
99import { canUseTouchScreen } from '@libs/DeviceCapabilities' ;
10- import type { Option } from '@libs/OptionsListUtils' ;
10+ import { filterAndOrderOptions , formatSectionsFromSearchTerm , getValidOptions } from '@libs/OptionsListUtils' ;
11+ import type { Option , Section } from '@libs/OptionsListUtils' ;
1112import type { OptionData } from '@libs/ReportUtils' ;
13+ import { getDisplayNameForParticipant } from '@libs/ReportUtils' ;
1214import Navigation from '@navigation/Navigation' ;
15+ import CONST from '@src/CONST' ;
1316import ONYXKEYS from '@src/ONYXKEYS' ;
1417import ROUTES from '@src/ROUTES' ;
1518import SearchFilterPageFooterButtons from './SearchFilterPageFooterButtons' ;
1619
20+ const defaultListOptions = {
21+ userToInvite : null ,
22+ recentReports : [ ] ,
23+ personalDetails : [ ] ,
24+ currentUserOption : null ,
25+ headerMessage : '' ,
26+ } ;
27+
1728function getSelectedOptionData ( option : Option ) : OptionData {
1829 // eslint-disable-next-line rulesdir/no-default-id-values
1930 return { ...option , selected : true , reportID : option . reportID ?? '-1' } ;
@@ -28,17 +39,87 @@ function SearchFiltersParticipantsSelector({initialAccountIDs, onFiltersUpdate}:
2839 const { translate} = useLocalize ( ) ;
2940 const personalDetails = usePersonalDetails ( ) ;
3041 const { didScreenTransitionEnd} = useScreenWrapperTransitionStatus ( ) ;
42+ const { options, areOptionsInitialized} = useOptionsList ( {
43+ shouldInitialize : didScreenTransitionEnd ,
44+ } ) ;
3145
3246 const [ isSearchingForReports ] = useOnyx ( ONYXKEYS . IS_SEARCHING_FOR_REPORTS , { canBeMissing : false , initWithStoredValues : false } ) ;
3347 const [ selectedOptions , setSelectedOptions ] = useState < OptionData [ ] > ( [ ] ) ;
3448 const [ searchTerm , setSearchTerm ] = useState ( '' ) ;
3549 const cleanSearchTerm = useMemo ( ( ) => searchTerm . trim ( ) . toLowerCase ( ) , [ searchTerm ] ) ;
3650
37- const { sections, headerMessage, areOptionsInitialized} = useSearchParticipantsOptions ( {
38- selectedOptions,
39- cleanSearchTerm,
40- shouldInitialize : didScreenTransitionEnd ,
41- } ) ;
51+ const defaultOptions = useMemo ( ( ) => {
52+ if ( ! areOptionsInitialized ) {
53+ return defaultListOptions ;
54+ }
55+
56+ return getValidOptions (
57+ {
58+ reports : options . reports ,
59+ personalDetails : options . personalDetails ,
60+ } ,
61+ {
62+ selectedOptions,
63+ excludeLogins : CONST . EXPENSIFY_EMAILS_OBJECT ,
64+ } ,
65+ ) ;
66+ } , [ areOptionsInitialized , options . personalDetails , options . reports , selectedOptions ] ) ;
67+
68+ const chatOptions = useMemo ( ( ) => {
69+ return filterAndOrderOptions ( defaultOptions , cleanSearchTerm , {
70+ selectedOptions,
71+ excludeLogins : CONST . EXPENSIFY_EMAILS_OBJECT ,
72+ maxRecentReportsToShow : CONST . IOU . MAX_RECENT_REPORTS_TO_SHOW ,
73+ canInviteUser : false ,
74+ } ) ;
75+ } , [ defaultOptions , cleanSearchTerm , selectedOptions ] ) ;
76+
77+ const { sections, headerMessage} = useMemo ( ( ) => {
78+ const newSections : Section [ ] = [ ] ;
79+ if ( ! areOptionsInitialized ) {
80+ return { sections : [ ] , headerMessage : undefined } ;
81+ }
82+
83+ const formattedResults = formatSectionsFromSearchTerm ( cleanSearchTerm , selectedOptions , chatOptions . recentReports , chatOptions . personalDetails , personalDetails , true ) ;
84+
85+ const selectedCurrentUser = formattedResults . section . data . find ( ( option ) => option . accountID === chatOptions . currentUserOption ?. accountID ) ;
86+
87+ if ( chatOptions . currentUserOption ) {
88+ const formattedName = getDisplayNameForParticipant ( {
89+ accountID : chatOptions . currentUserOption . accountID ,
90+ shouldAddCurrentUserPostfix : true ,
91+ personalDetailsData : personalDetails ,
92+ } ) ;
93+ if ( selectedCurrentUser ) {
94+ selectedCurrentUser . text = formattedName ;
95+ } else {
96+ chatOptions . currentUserOption . text = formattedName ;
97+ chatOptions . recentReports = [ chatOptions . currentUserOption , ...chatOptions . recentReports ] ;
98+ }
99+ }
100+
101+ newSections . push ( formattedResults . section ) ;
102+
103+ newSections . push ( {
104+ title : '' ,
105+ data : chatOptions . recentReports ,
106+ shouldShow : chatOptions . recentReports . length > 0 ,
107+ } ) ;
108+
109+ newSections . push ( {
110+ title : '' ,
111+ data : chatOptions . personalDetails ,
112+ shouldShow : chatOptions . personalDetails . length > 0 ,
113+ } ) ;
114+
115+ const noResultsFound = chatOptions . personalDetails . length === 0 && chatOptions . recentReports . length === 0 && ! chatOptions . currentUserOption ;
116+ const message = noResultsFound ? translate ( 'common.noResultsFound' ) : undefined ;
117+
118+ return {
119+ sections : newSections ,
120+ headerMessage : message ,
121+ } ;
122+ } , [ areOptionsInitialized , cleanSearchTerm , selectedOptions , chatOptions , personalDetails , translate ] ) ;
42123
43124 const resetChanges = useCallback ( ( ) => {
44125 setSelectedOptions ( [ ] ) ;
0 commit comments