1- import React , { createContext , useCallback , useContext , useEffect , useMemo , useRef , useState } from 'react' ;
1+ import React , { createContext , useCallback , useContext , useEffect , useMemo , useState } from 'react' ;
2+ import { InteractionManager } from 'react-native' ;
23import { useOnyx } from 'react-native-onyx' ;
34import type { OnyxCollection } from 'react-native-onyx' ;
45import usePrevious from '@hooks/usePrevious' ;
6+ import getPlatform from '@libs/getPlatform' ;
57import { createOptionFromReport , createOptionList , processReport } from '@libs/OptionsListUtils' ;
68import type { OptionList , SearchOption } from '@libs/OptionsListUtils' ;
79import { isSelfDM } from '@libs/ReportUtils' ;
10+ import CONST from '@src/CONST' ;
811import ONYXKEYS from '@src/ONYXKEYS' ;
912import type { PersonalDetails , Report } from '@src/types/onyx' ;
1013import { usePersonalDetails } from './OnyxProvider' ;
@@ -42,7 +45,8 @@ const isEqualPersonalDetail = (prevPersonalDetail: PersonalDetails, personalDeta
4245 prevPersonalDetail ?. displayName === personalDetail ?. displayName ;
4346
4447function OptionsListContextProvider ( { children} : OptionsListProviderProps ) {
45- const areOptionsInitialized = useRef ( false ) ;
48+ const [ areOptionsInitialized , setAreOptionsInitialized ] = useState ( false ) ;
49+
4650 const [ options , setOptions ] = useState < OptionList > ( {
4751 reports : [ ] ,
4852 personalDetails : [ ] ,
@@ -67,12 +71,12 @@ function OptionsListContextProvider({children}: OptionsListProviderProps) {
6771 * This effect is responsible for generating the options list when their data is not yet initialized
6872 */
6973 useEffect ( ( ) => {
70- if ( ! areOptionsInitialized . current || ! reports || hasInitialData ) {
74+ if ( ! areOptionsInitialized || ! reports || hasInitialData ) {
7175 return ;
7276 }
7377
7478 loadOptions ( ) ;
75- } , [ reports , personalDetails , hasInitialData , loadOptions ] ) ;
79+ } , [ reports , personalDetails , hasInitialData , loadOptions , areOptionsInitialized ] ) ;
7680
7781 /**
7882 * This effect is responsible for generating the options list when the locale changes
@@ -102,7 +106,7 @@ function OptionsListContextProvider({children}: OptionsListProviderProps) {
102106 * This effect is responsible for updating the options only for changed reports
103107 */
104108 useEffect ( ( ) => {
105- if ( ! changedReportsEntries || ! areOptionsInitialized . current ) {
109+ if ( ! changedReportsEntries || ! areOptionsInitialized ) {
106110 return ;
107111 }
108112
@@ -130,10 +134,10 @@ function OptionsListContextProvider({children}: OptionsListProviderProps) {
130134 reports : Array . from ( updatedReportsMap . values ( ) ) ,
131135 } ;
132136 } ) ;
133- } , [ changedReportsEntries , personalDetails ] ) ;
137+ } , [ areOptionsInitialized , changedReportsEntries , personalDetails ] ) ;
134138
135139 useEffect ( ( ) => {
136- if ( ! changedReportActions || ! areOptionsInitialized . current ) {
140+ if ( ! changedReportActions || ! areOptionsInitialized ) {
137141 return ;
138142 }
139143
@@ -162,14 +166,14 @@ function OptionsListContextProvider({children}: OptionsListProviderProps) {
162166 reports : Array . from ( updatedReportsMap . values ( ) ) ,
163167 } ;
164168 } ) ;
165- } , [ changedReportActions , personalDetails ] ) ;
169+ } , [ areOptionsInitialized , changedReportActions , personalDetails ] ) ;
166170
167171 /**
168172 * This effect is used to update the options list when personal details change.
169173 */
170174 useEffect ( ( ) => {
171175 // there is no need to update the options if the options are not initialized
172- if ( ! areOptionsInitialized . current ) {
176+ if ( ! areOptionsInitialized ) {
173177 return ;
174178 }
175179
@@ -233,24 +237,30 @@ function OptionsListContextProvider({children}: OptionsListProviderProps) {
233237
234238 const initializeOptions = useCallback ( ( ) => {
235239 loadOptions ( ) ;
236- areOptionsInitialized . current = true ;
240+ if ( getPlatform ( ) === CONST . PLATFORM . ANDROID || getPlatform ( ) === CONST . PLATFORM . IOS ) {
241+ InteractionManager . runAfterInteractions ( ( ) => {
242+ setAreOptionsInitialized ( true ) ;
243+ } ) ;
244+ return ;
245+ }
246+ setAreOptionsInitialized ( true ) ;
237247 } , [ loadOptions ] ) ;
238248
239249 const resetOptions = useCallback ( ( ) => {
240- if ( ! areOptionsInitialized . current ) {
250+ if ( ! areOptionsInitialized ) {
241251 return ;
242252 }
243253
244- areOptionsInitialized . current = false ;
254+ setAreOptionsInitialized ( false ) ;
245255 setOptions ( {
246256 reports : [ ] ,
247257 personalDetails : [ ] ,
248258 } ) ;
249- } , [ ] ) ;
259+ } , [ areOptionsInitialized ] ) ;
250260
251261 return (
252262 < OptionsListContext . Provider // eslint-disable-next-line react-compiler/react-compiler
253- value = { useMemo ( ( ) => ( { options, initializeOptions, areOptionsInitialized : areOptionsInitialized . current , resetOptions} ) , [ options , initializeOptions , resetOptions ] ) }
263+ value = { useMemo ( ( ) => ( { options, initializeOptions, areOptionsInitialized, resetOptions} ) , [ options , initializeOptions , areOptionsInitialized , resetOptions ] ) }
254264 >
255265 { children }
256266 </ OptionsListContext . Provider >
@@ -263,15 +273,14 @@ const useOptionsListContext = () => useContext(OptionsListContext);
263273const useOptionsList = ( options ?: { shouldInitialize : boolean } ) => {
264274 const { shouldInitialize = true } = options ?? { } ;
265275 const { initializeOptions, options : optionsList , areOptionsInitialized, resetOptions} = useOptionsListContext ( ) ;
266- const [ isLoadingApp ] = useOnyx ( ONYXKEYS . IS_LOADING_APP , { canBeMissing : false } ) ;
267276
268277 useEffect ( ( ) => {
269- if ( ! shouldInitialize || areOptionsInitialized || isLoadingApp ) {
278+ if ( ! shouldInitialize || areOptionsInitialized ) {
270279 return ;
271280 }
272281
273282 initializeOptions ( ) ;
274- } , [ shouldInitialize , initializeOptions , areOptionsInitialized , isLoadingApp ] ) ;
283+ } , [ shouldInitialize , initializeOptions , areOptionsInitialized ] ) ;
275284
276285 return {
277286 initializeOptions,
0 commit comments