11import type { ForwardedRef , RefObject } from 'react' ;
2- import React , { useContext , useEffect , useMemo , useRef , useState } from 'react' ;
2+ import React , { useEffect , useMemo , useRef , useState } from 'react' ;
33import type { OnyxCollection , OnyxEntry } from 'react-native-onyx' ;
4- import { OptionsListStateContext , useOptionsList } from '@components/OptionListContextProvider' ;
54import OptionsListSkeletonView from '@components/OptionsListSkeletonView' ;
65import type { AnimatedTextInputRef } from '@components/RNTextInput' ;
76import type { ListItem as NewListItem , UserListItemProps } from '@components/SelectionList/ListItem/types' ;
@@ -13,6 +12,7 @@ import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails'
1312import useDebounce from '@hooks/useDebounce' ;
1413import useDebouncedAccessibilityAnnouncement from '@hooks/useDebouncedAccessibilityAnnouncement' ;
1514import useFeedKeysWithAssignedCards from '@hooks/useFeedKeysWithAssignedCards' ;
15+ import useFilteredOptions from '@hooks/useFilteredOptions' ;
1616import { useMemoizedLazyExpensifyIcons } from '@hooks/useLazyAsset' ;
1717import useLocalize from '@hooks/useLocalize' ;
1818import useOnyx from '@hooks/useOnyx' ;
@@ -94,6 +94,11 @@ const defaultListOptions = {
9494 categoryOptions : [ ] ,
9595} ;
9696
97+ const emptyOptionList = {
98+ reports : [ ] ,
99+ personalDetails : [ ] ,
100+ } ;
101+
97102const setPerformanceTimersEnd = ( ) => {
98103 endSpan ( CONST . TELEMETRY . SPAN_OPEN_SEARCH_ROUTER ) ;
99104} ;
@@ -165,7 +170,7 @@ function SearchAutocompleteList({
165170 const expensifyIcons = useMemoizedLazyExpensifyIcons ( [ 'History' , 'MagnifyingGlass' ] ) ;
166171 const taxRates = getAllTaxRates ( policies ) ;
167172
168- const { options, areOptionsInitialized } = useOptionsList ( ) ;
173+ const { options : listOptions , isLoading : isLoadingOptions } = useFilteredOptions ( { enabled : true , isSearching : ! ! autocompleteQueryValue . trim ( ) , betas : betas ?? [ ] } ) ;
169174
170175 const isRecentSearchesDataLoaded = ! isLoadingOnyxValue ( recentSearchesMetadata ) ;
171176
@@ -175,25 +180,24 @@ function SearchAutocompleteList({
175180 } ;
176181 } , [ ] ) ;
177182
178- const { areOptionsInitialized : contextAreOptionsInitialized } = useContext ( OptionsListStateContext ) ;
179183 const coldStartAttributeSet = useRef ( false ) ;
180184 useEffect ( ( ) => {
181185 if ( coldStartAttributeSet . current ) {
182186 return ;
183187 }
184188 const parentSpan = getSpan ( CONST . TELEMETRY . SPAN_OPEN_SEARCH_ROUTER ) ;
185189 if ( parentSpan ) {
186- parentSpan . setAttribute ( CONST . TELEMETRY . ATTRIBUTE_COLD_START , ! contextAreOptionsInitialized ) ;
190+ parentSpan . setAttribute ( CONST . TELEMETRY . ATTRIBUTE_COLD_START , isLoadingOptions ) ;
187191 coldStartAttributeSet . current = true ;
188192 }
189- } , [ contextAreOptionsInitialized ] ) ;
193+ } , [ isLoadingOptions ] ) ;
190194
191195 const searchOptions = ( ( ) => {
192- if ( ! areOptionsInitialized ) {
196+ if ( listOptions === null ) {
193197 return defaultListOptions ;
194198 }
195199 return getSearchOptions ( {
196- options,
200+ options : listOptions ,
197201 draftComments,
198202 nvpDismissedProductTraining,
199203 betas : betas ?? [ ] ,
@@ -277,7 +281,7 @@ function SearchAutocompleteList({
277281 autocompleteQueryValue,
278282 allCards,
279283 allFeeds,
280- options,
284+ options : listOptions ?? emptyOptionList ,
281285 draftComments,
282286 nvpDismissedProductTraining,
283287 betas,
@@ -357,7 +361,7 @@ function SearchAutocompleteList({
357361 const reasonAttributes : SkeletonSpanReasonAttributes = {
358362 context : 'SearchAutocompleteList' ,
359363 isRecentSearchesDataLoaded,
360- areOptionsInitialized ,
364+ isLoadingOptions ,
361365 } ;
362366
363367 /* Sections generation */
@@ -404,7 +408,7 @@ function SearchAutocompleteList({
404408 } as AutocompleteListItem ;
405409 } ) ;
406410
407- if ( areOptionsInitialized ) {
411+ if ( ! isLoadingOptions ) {
408412 pushSection ( {
409413 title : autocompleteQueryValue . trim ( ) === '' ? translate ( 'search.recentChats' ) : undefined ,
410414 data : nextStyledRecentReports ,
@@ -430,7 +434,7 @@ function SearchAutocompleteList({
430434 reasonAttributes = { {
431435 context : 'SearchAutocompleteList' ,
432436 isRecentSearchesDataLoaded,
433- areOptionsInitialized ,
437+ isLoadingOptions ,
434438 } }
435439 />
436440 ) ,
@@ -465,7 +469,7 @@ function SearchAutocompleteList({
465469 searchQueryItem ,
466470 styles ,
467471 translate ,
468- areOptionsInitialized ,
472+ isLoadingOptions ,
469473 isRecentSearchesDataLoaded ,
470474 ] ) ;
471475
@@ -507,13 +511,13 @@ function SearchAutocompleteList({
507511 // because useState(initialFocusedIndex) in useArrowKeyFocusManager only reads the initial value.
508512 // Imperatively focus the first recent report once options become available (desktop only).
509513 useEffect ( ( ) => {
510- if ( shouldUseNarrowLayout || ! areOptionsInitialized || hasSetInitialFocusRef . current || firstRecentReportFlatIndex === - 1 ) {
514+ if ( shouldUseNarrowLayout || isLoadingOptions || hasSetInitialFocusRef . current || firstRecentReportFlatIndex === - 1 ) {
511515 return ;
512516 }
513517 hasSetInitialFocusRef . current = true ;
514518
515519 innerListRef . current ?. updateAndScrollToFocusedIndex ( firstRecentReportFlatIndex , false ) ;
516- } , [ areOptionsInitialized , firstRecentReportFlatIndex , shouldUseNarrowLayout ] ) ;
520+ } , [ isLoadingOptions , firstRecentReportFlatIndex , shouldUseNarrowLayout ] ) ;
517521
518522 useEffect ( ( ) => {
519523 const targetText = autocompleteQueryValue ;
0 commit comments