11import deburr from 'lodash/deburr' ;
22import { useCallback , useEffect , useRef , useState } from 'react' ;
3+ import { InteractionManager } from 'react-native' ;
34import Timing from '@libs/actions/Timing' ;
45import FastSearch from '@libs/FastSearch' ;
56import type { Options as OptionsListType , ReportAndPersonalDetailOptions } from '@libs/OptionsListUtils' ;
@@ -9,15 +10,15 @@ import type {OptionData} from '@libs/ReportUtils';
910import StringUtils from '@libs/StringUtils' ;
1011import CONST from '@src/CONST' ;
1112
12- type AllOrSelectiveOptions = ReportAndPersonalDetailOptions | OptionsListType ;
13-
1413type Options = {
1514 includeUserToInvite : boolean ;
1615} ;
1716
1817const emptyResult = {
1918 personalDetails : [ ] ,
2019 recentReports : [ ] ,
20+ userToInvite : null ,
21+ currentUserOption : undefined ,
2122} ;
2223
2324const personalDetailToSearchString = ( option : OptionData ) => {
@@ -48,9 +49,18 @@ const getRecentReportUniqueId = (option: OptionData) => {
4849} ;
4950
5051// You can either use this to search within report and personal details options
51- function useFastSearchFromOptions ( options : ReportAndPersonalDetailOptions , config ?: { includeUserToInvite : false } ) : ( searchInput : string ) => ReportAndPersonalDetailOptions ;
52+ function useFastSearchFromOptions (
53+ options : ReportAndPersonalDetailOptions ,
54+ config ?: { includeUserToInvite : false } ,
55+ ) : { search : ( searchInput : string ) => ReportAndPersonalDetailOptions ; isInitialized : boolean } ;
5256// Or you can use this to include the user invite option. This will require passing all options
53- function useFastSearchFromOptions ( options : OptionsListType , config ?: { includeUserToInvite : true } ) : ( searchInput : string ) => OptionsListType ;
57+ function useFastSearchFromOptions (
58+ options : OptionsListType ,
59+ config ?: { includeUserToInvite : true } ,
60+ ) : {
61+ search : ( searchInput : string ) => OptionsListType ;
62+ isInitialized : boolean ;
63+ } ;
5464
5565/**
5666 * Hook for making options from OptionsListUtils searchable with FastSearch.
@@ -64,43 +74,49 @@ function useFastSearchFromOptions(options: OptionsListType, config?: {includeUse
6474function useFastSearchFromOptions (
6575 options : ReportAndPersonalDetailOptions | OptionsListType ,
6676 { includeUserToInvite} : Options = { includeUserToInvite : false } ,
67- ) : ( searchInput : string ) => AllOrSelectiveOptions {
77+ ) : { search : ( searchInput : string ) => OptionsListType ; isInitialized : boolean } {
6878 const [ fastSearch , setFastSearch ] = useState < ReturnType < typeof FastSearch . createFastSearch < OptionData > > | null > ( null ) ;
79+ const [ isInitialized , setIsInitialized ] = useState ( false ) ;
6980 const prevOptionsRef = useRef < typeof options | null > ( null ) ;
7081 const prevFastSearchRef = useRef < ReturnType < typeof FastSearch . createFastSearch < OptionData > > | null > ( null ) ;
7182
7283 useEffect ( ( ) => {
84+ let newFastSearch : ReturnType < typeof FastSearch . createFastSearch < OptionData > > ;
7385 const prevOptions = prevOptionsRef . current ;
7486 if ( prevOptions && shallowCompareOptions ( prevOptions , options ) ) {
7587 return ;
7688 }
89+ InteractionManager . runAfterInteractions ( ( ) => {
90+ prevOptionsRef . current = options ;
91+ prevFastSearchRef . current ?. dispose ( ) ;
92+ newFastSearch = FastSearch . createFastSearch (
93+ [
94+ {
95+ data : options . personalDetails ,
96+ toSearchableString : personalDetailToSearchString ,
97+ uniqueId : getPersonalDetailUniqueId ,
98+ } ,
99+ {
100+ data : options . recentReports ,
101+ toSearchableString : recentReportToSearchString ,
102+ uniqueId : getRecentReportUniqueId ,
103+ } ,
104+ ] ,
105+
106+ { shouldStoreSearchableStrings : true } ,
107+ ) ;
108+ setFastSearch ( newFastSearch ) ;
109+ prevFastSearchRef . current = newFastSearch ;
110+ setIsInitialized ( true ) ;
111+ } ) ;
77112
78- prevOptionsRef . current = options ;
79- prevFastSearchRef . current ?. dispose ( ) ;
80-
81- const newFastSearch = FastSearch . createFastSearch (
82- [
83- {
84- data : options . personalDetails ,
85- toSearchableString : personalDetailToSearchString ,
86- uniqueId : getPersonalDetailUniqueId ,
87- } ,
88- {
89- data : options . recentReports ,
90- toSearchableString : recentReportToSearchString ,
91- uniqueId : getRecentReportUniqueId ,
92- } ,
93- ] ,
94- { shouldStoreSearchableStrings : true } ,
95- ) ;
96- setFastSearch ( newFastSearch ) ;
97- prevFastSearchRef . current = newFastSearch ;
113+ return ( ) => newFastSearch ?. dispose ( ) ;
98114 } , [ options ] ) ;
99115
100116 useEffect ( ( ) => ( ) => prevFastSearchRef . current ?. dispose ( ) , [ ] ) ;
101117
102118 const findInSearchTree = useCallback (
103- ( searchInput : string ) : AllOrSelectiveOptions => {
119+ ( searchInput : string ) : OptionsListType => {
104120 if ( ! fastSearch ) {
105121 return emptyResult ;
106122 }
@@ -150,12 +166,14 @@ function useFastSearchFromOptions(
150166 return {
151167 personalDetails,
152168 recentReports,
169+ userToInvite : null ,
170+ currentUserOption : undefined ,
153171 } ;
154172 } ,
155173 [ includeUserToInvite , options , fastSearch ] ,
156174 ) ;
157175
158- return findInSearchTree ;
176+ return { search : findInSearchTree , isInitialized } ;
159177}
160178
161179/**
0 commit comments