diff --git a/src/libs/Navigation/AppNavigator/AuthScreens.tsx b/src/libs/Navigation/AppNavigator/AuthScreens.tsx index 0fee45ceb335..89dc6f5b5ee2 100644 --- a/src/libs/Navigation/AppNavigator/AuthScreens.tsx +++ b/src/libs/Navigation/AppNavigator/AuthScreens.tsx @@ -1,9 +1,7 @@ import type {RouteProp} from '@react-navigation/native'; import {useNavigation} from '@react-navigation/native'; import type {StackCardInterpolationProps} from '@react-navigation/stack'; -import React, {memo, useContext, useEffect, useMemo, useState} from 'react'; -import type {OnyxEntry} from 'react-native-onyx'; -import Onyx from 'react-native-onyx'; +import React, {memo, useContext, useEffect, useMemo, useRef, useState} from 'react'; import ComposeProviders from '@components/ComposeProviders'; import DelegateNoAccessModalProvider from '@components/DelegateNoAccessModalProvider'; import FullScreenLoadingIndicator from '@components/FullscreenLoadingIndicator'; @@ -106,31 +104,6 @@ function initializePusher() { User.subscribeToUserEvents(); }); } -let lastUpdateIDAppliedToClient: OnyxEntry; -let isLoadingApp = false; - -Onyx.connect({ - key: ONYXKEYS.ONYX_UPDATES_LAST_UPDATE_ID_APPLIED_TO_CLIENT, - callback: (value) => { - lastUpdateIDAppliedToClient = value; - }, -}); - -Onyx.connect({ - key: ONYXKEYS.IS_LOADING_APP, - callback: (value) => { - isLoadingApp = !!value; - }, -}); - -function handleNetworkReconnect() { - if (isLoadingApp) { - App.openApp(); - } else { - Log.info('[handleNetworkReconnect] Sending ReconnectApp'); - App.reconnectApp(lastUpdateIDAppliedToClient); - } -} const RootStack = createRootStackNavigator(); @@ -199,6 +172,24 @@ function AuthScreens() { const [initialLastUpdateIDAppliedToClient] = useOnyx(ONYXKEYS.ONYX_UPDATES_LAST_UPDATE_ID_APPLIED_TO_CLIENT, {canBeMissing: true}); const [modal] = useOnyx(ONYXKEYS.MODAL, {canBeMissing: true}); + const [lastUpdateIDAppliedToClient] = useOnyx(ONYXKEYS.ONYX_UPDATES_LAST_UPDATE_ID_APPLIED_TO_CLIENT, {canBeMissing: true}); + const [isLoadingApp] = useOnyx(ONYXKEYS.IS_LOADING_APP, {canBeMissing: true}); + const lastUpdateIDAppliedToClientRef = useRef(lastUpdateIDAppliedToClient); + const isLoadingAppRef = useRef(isLoadingApp); + // eslint-disable-next-line react-compiler/react-compiler + lastUpdateIDAppliedToClientRef.current = lastUpdateIDAppliedToClient; + // eslint-disable-next-line react-compiler/react-compiler + isLoadingAppRef.current = isLoadingApp; + + const handleNetworkReconnect = () => { + if (isLoadingAppRef.current) { + App.openApp(); + } else { + Log.info('[handleNetworkReconnect] Sending ReconnectApp'); + App.reconnectApp(lastUpdateIDAppliedToClientRef.current); + } + }; + // On HybridApp we need to prevent flickering during transition to OldDot const shouldRenderOnboardingExclusivelyOnHybridApp = useMemo(() => { return CONFIG.IS_HYBRID_APP && Navigation.getActiveRoute().includes(ROUTES.ONBOARDING_INTERESTED_FEATURES.route) && isOnboardingCompleted === true;