@@ -2,7 +2,7 @@ import type {RouteProp} from '@react-navigation/native';
22import { useNavigation } from '@react-navigation/native' ;
33import React , { memo , useContext , useEffect , useMemo , useRef , useState } from 'react' ;
44import type { OnyxEntry } from 'react-native-onyx' ;
5- import Onyx , { withOnyx } from 'react-native-onyx' ;
5+ import { withOnyx } from 'react-native-onyx' ;
66import ComposeProviders from '@components/ComposeProviders' ;
77import DelegateNoAccessModalProvider from '@components/DelegateNoAccessModalProvider' ;
88import FullScreenLoadingIndicator from '@components/FullscreenLoadingIndicator' ;
@@ -62,7 +62,7 @@ import ONYXKEYS from '@src/ONYXKEYS';
6262import ROUTES from '@src/ROUTES' ;
6363import SCREENS from '@src/SCREENS' ;
6464import type * as OnyxTypes from '@src/types/onyx' ;
65- import type { SelectedTimezone , Timezone } from '@src/types/onyx/PersonalDetails' ;
65+ import type { SelectedTimezone } from '@src/types/onyx/PersonalDetails' ;
6666import { isEmptyObject } from '@src/types/utils/EmptyObject' ;
6767import type ReactComponentModule from '@src/types/utils/ReactComponentModule' ;
6868import attachmentModalScreenOptions from './attachmentModalScreenOptions' ;
@@ -119,66 +119,7 @@ function initializePusher() {
119119 } ) ;
120120}
121121
122- let timezone : Timezone | null ;
123- let currentAccountID = - 1 ;
124- let isLoadingApp = false ;
125- let lastUpdateIDAppliedToClient : OnyxEntry < number > ;
126-
127- Onyx . connect ( {
128- key : ONYXKEYS . SESSION ,
129- callback : ( value ) => {
130- // When signed out, val hasn't accountID
131- if ( ! ( value && 'accountID' in value ) ) {
132- currentAccountID = - 1 ;
133- timezone = null ;
134- return ;
135- }
136-
137- currentAccountID = value . accountID ?? CONST . DEFAULT_NUMBER_ID ;
138-
139- if ( Navigation . isActiveRoute ( ROUTES . SIGN_IN_MODAL ) ) {
140- // This means sign in in RHP was successful, so we can subscribe to user events
141- initializePusher ( ) ;
142- }
143- } ,
144- } ) ;
145-
146- Onyx . connect ( {
147- key : ONYXKEYS . PERSONAL_DETAILS_LIST ,
148- callback : ( value ) => {
149- if ( ! value || ! isEmptyObject ( timezone ) ) {
150- return ;
151- }
152-
153- timezone = value ?. [ currentAccountID ] ?. timezone ?? { } ;
154- const currentTimezone = Intl . DateTimeFormat ( ) . resolvedOptions ( ) . timeZone as SelectedTimezone ;
155-
156- // If the current timezone is different than the user's timezone, and their timezone is set to automatic
157- // then update their timezone.
158- if ( ! isEmptyObject ( currentTimezone ) && timezone ?. automatic && timezone ?. selected !== currentTimezone ) {
159- PersonalDetails . updateAutomaticTimezone ( {
160- automatic : true ,
161- selected : currentTimezone ,
162- } ) ;
163- }
164- } ,
165- } ) ;
166-
167- Onyx . connect ( {
168- key : ONYXKEYS . IS_LOADING_APP ,
169- callback : ( value ) => {
170- isLoadingApp = ! ! value ;
171- } ,
172- } ) ;
173-
174- Onyx . connect ( {
175- key : ONYXKEYS . ONYX_UPDATES_LAST_UPDATE_ID_APPLIED_TO_CLIENT ,
176- callback : ( value ) => {
177- lastUpdateIDAppliedToClient = value ;
178- } ,
179- } ) ;
180-
181- function handleNetworkReconnect ( ) {
122+ function handleNetworkReconnect ( isLoadingApp : boolean , lastUpdateIDAppliedToClient : number | undefined ) {
182123 if ( isLoadingApp ) {
183124 App . openApp ( ) ;
184125 } else {
@@ -238,6 +179,35 @@ function AuthScreens({session, lastOpenedPublicRoomID, initialLastUpdateIDApplie
238179 canBeMissing : true ,
239180 } ) ;
240181 const [ onboardingCompanySize ] = useOnyx ( ONYXKEYS . ONBOARDING_COMPANY_SIZE , { canBeMissing : true } ) ;
182+ const [ isLoadingApp ] = useOnyx ( ONYXKEYS . IS_LOADING_APP , { canBeMissing : true } ) ;
183+ const [ lastUpdateIDAppliedToClient ] = useOnyx ( ONYXKEYS . ONYX_UPDATES_LAST_UPDATE_ID_APPLIED_TO_CLIENT , { canBeMissing : true } ) ;
184+
185+ const timezone = useMemo ( ( ) => {
186+ const timezoneObject = currentUserPersonalDetails ?. timezone ?? { } ;
187+ if ( ! currentUserPersonalDetails || ! isEmptyObject ( timezoneObject ) ) {
188+ return ;
189+ }
190+ return timezoneObject ;
191+ } , [ currentUserPersonalDetails ] ) ;
192+
193+ useEffect ( ( ) => {
194+ if ( Navigation . isActiveRoute ( ROUTES . SIGN_IN_MODAL ) ) {
195+ // This means sign in in RHP was successful, so we can subscribe to user events
196+ initializePusher ( ) ;
197+ }
198+
199+ const currentTimezone = Intl . DateTimeFormat ( ) . resolvedOptions ( ) . timeZone as SelectedTimezone ;
200+
201+ // If the current timezone is different than the user's timezone, and their timezone is set to automatic
202+ // then update their timezone.
203+ if ( ! isEmptyObject ( currentTimezone ) && timezone ?. automatic && timezone ?. selected !== currentTimezone ) {
204+ PersonalDetails . updateAutomaticTimezone ( {
205+ automatic : true ,
206+ selected : currentTimezone ,
207+ } ) ;
208+ }
209+ } , [ session , timezone ?. automatic , timezone ?. selected ] ) ;
210+
241211 const modal = useRef < OnyxTypes . Modal > ( { } ) ;
242212 const { isOnboardingCompleted} = useOnboardingFlowRouter ( ) ;
243213 const [ isOnboardingLoading ] = useOnyx ( ONYXKEYS . NVP_ONBOARDING , { canBeMissing : true , selector : ( value ) => ! ! value ?. isLoading } ) ;
@@ -302,7 +272,7 @@ function AuthScreens({session, lastOpenedPublicRoomID, initialLastUpdateIDApplie
302272 }
303273
304274 NetworkConnection . listenForReconnect ( ) ;
305- NetworkConnection . onReconnect ( handleNetworkReconnect ) ;
275+ NetworkConnection . onReconnect ( ( ) => handleNetworkReconnect ( ! ! isLoadingApp , lastUpdateIDAppliedToClient ) ) ;
306276 PusherConnectionManager . init ( ) ;
307277 initializePusher ( ) ;
308278 // Sometimes when we transition from old dot to new dot, the client is not the leader
0 commit comments