@@ -2,7 +2,7 @@ import type {RouteProp} from '@react-navigation/native';
22import { useNavigation } from '@react-navigation/native' ;
33import React , { memo , 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' ;
@@ -60,7 +60,7 @@ import ONYXKEYS from '@src/ONYXKEYS';
6060import ROUTES from '@src/ROUTES' ;
6161import SCREENS from '@src/SCREENS' ;
6262import type * as OnyxTypes from '@src/types/onyx' ;
63- import type { SelectedTimezone , Timezone } from '@src/types/onyx/PersonalDetails' ;
63+ import type { SelectedTimezone } from '@src/types/onyx/PersonalDetails' ;
6464import { isEmptyObject } from '@src/types/utils/EmptyObject' ;
6565import type ReactComponentModule from '@src/types/utils/ReactComponentModule' ;
6666import attachmentModalScreenOptions from './attachmentModalScreenOptions' ;
@@ -117,66 +117,7 @@ function initializePusher() {
117117 } ) ;
118118}
119119
120- let timezone : Timezone | null ;
121- let currentAccountID = - 1 ;
122- let isLoadingApp = false ;
123- let lastUpdateIDAppliedToClient : OnyxEntry < number > ;
124-
125- Onyx . connect ( {
126- key : ONYXKEYS . SESSION ,
127- callback : ( value ) => {
128- // When signed out, val hasn't accountID
129- if ( ! ( value && 'accountID' in value ) ) {
130- currentAccountID = - 1 ;
131- timezone = null ;
132- return ;
133- }
134-
135- currentAccountID = value . accountID ?? CONST . DEFAULT_NUMBER_ID ;
136-
137- if ( Navigation . isActiveRoute ( ROUTES . SIGN_IN_MODAL ) ) {
138- // This means sign in in RHP was successful, so we can subscribe to user events
139- initializePusher ( ) ;
140- }
141- } ,
142- } ) ;
143-
144- Onyx . connect ( {
145- key : ONYXKEYS . PERSONAL_DETAILS_LIST ,
146- callback : ( value ) => {
147- if ( ! value || ! isEmptyObject ( timezone ) ) {
148- return ;
149- }
150-
151- timezone = value ?. [ currentAccountID ] ?. timezone ?? { } ;
152- const currentTimezone = Intl . DateTimeFormat ( ) . resolvedOptions ( ) . timeZone as SelectedTimezone ;
153-
154- // If the current timezone is different than the user's timezone, and their timezone is set to automatic
155- // then update their timezone.
156- if ( ! isEmptyObject ( currentTimezone ) && timezone ?. automatic && timezone ?. selected !== currentTimezone ) {
157- PersonalDetails . updateAutomaticTimezone ( {
158- automatic : true ,
159- selected : currentTimezone ,
160- } ) ;
161- }
162- } ,
163- } ) ;
164-
165- Onyx . connect ( {
166- key : ONYXKEYS . IS_LOADING_APP ,
167- callback : ( value ) => {
168- isLoadingApp = ! ! value ;
169- } ,
170- } ) ;
171-
172- Onyx . connect ( {
173- key : ONYXKEYS . ONYX_UPDATES_LAST_UPDATE_ID_APPLIED_TO_CLIENT ,
174- callback : ( value ) => {
175- lastUpdateIDAppliedToClient = value ;
176- } ,
177- } ) ;
178-
179- function handleNetworkReconnect ( ) {
120+ function handleNetworkReconnect ( isLoadingApp : boolean , lastUpdateIDAppliedToClient : number | undefined ) {
180121 if ( isLoadingApp ) {
181122 App . openApp ( ) ;
182123 } else {
@@ -236,6 +177,47 @@ function AuthScreens({session, lastOpenedPublicRoomID, initialLastUpdateIDApplie
236177 canBeMissing : true ,
237178 } ) ;
238179 const [ onboardingCompanySize ] = useOnyx ( ONYXKEYS . ONBOARDING_COMPANY_SIZE , { canBeMissing : true } ) ;
180+ const [ personalDetailsList ] = useOnyx ( ONYXKEYS . PERSONAL_DETAILS_LIST , { canBeMissing : true } ) ;
181+ const [ isLoadingApp ] = useOnyx ( ONYXKEYS . IS_LOADING_APP , { canBeMissing : true } ) ;
182+ const [ lastUpdateIDAppliedToClient ] = useOnyx ( ONYXKEYS . ONYX_UPDATES_LAST_UPDATE_ID_APPLIED_TO_CLIENT , { canBeMissing : true } ) ;
183+ const currentAccountID = useMemo ( ( ) => {
184+ if ( ! ( session && 'accountID' in session ) ) {
185+ return - 1 ;
186+ }
187+ return session . accountID ?? CONST . DEFAULT_NUMBER_ID ;
188+ } , [ session ] )
189+
190+
191+ const timezone = useMemo ( ( ) => {
192+ if ( ! personalDetailsList || ! isEmptyObject ( timezone ) ) {
193+ return ;
194+ }
195+
196+ return personalDetailsList ?. [ currentAccountID ] ?. timezone ?? { } ;
197+
198+ } , [ currentAccountID , personalDetailsList ] )
199+
200+
201+
202+ useEffect ( ( ) => {
203+ if ( Navigation . isActiveRoute ( ROUTES . SIGN_IN_MODAL ) ) {
204+ // This means sign in in RHP was successful, so we can subscribe to user events
205+ initializePusher ( ) ;
206+ }
207+
208+ const currentTimezone = Intl . DateTimeFormat ( ) . resolvedOptions ( ) . timeZone as SelectedTimezone ;
209+
210+ // If the current timezone is different than the user's timezone, and their timezone is set to automatic
211+ // then update their timezone.
212+ if ( ! isEmptyObject ( currentTimezone ) && timezone ?. automatic && timezone ?. selected !== currentTimezone ) {
213+ PersonalDetails . updateAutomaticTimezone ( {
214+ automatic : true ,
215+ selected : currentTimezone ,
216+ } ) ;
217+ }
218+
219+ } , [ session , personalDetailsList , timezone ?. automatic , timezone ?. selected ] ) ;
220+
239221 const modal = useRef < OnyxTypes . Modal > ( { } ) ;
240222 const { isOnboardingCompleted} = useOnboardingFlowRouter ( ) ;
241223 const [ isOnboardingLoading ] = useOnyx ( ONYXKEYS . NVP_ONBOARDING , { canBeMissing : true , selector : ( value ) => ! ! value ?. isLoading } ) ;
@@ -299,7 +281,7 @@ function AuthScreens({session, lastOpenedPublicRoomID, initialLastUpdateIDApplie
299281 }
300282
301283 NetworkConnection . listenForReconnect ( ) ;
302- NetworkConnection . onReconnect ( handleNetworkReconnect ) ;
284+ NetworkConnection . onReconnect ( ( ) => handleNetworkReconnect ( ! ! isLoadingApp , lastUpdateIDAppliedToClient ) ) ;
303285 PusherConnectionManager . init ( ) ;
304286 initializePusher ( ) ;
305287 // Sometimes when we transition from old dot to new dot, the client is not the leader
@@ -792,3 +774,4 @@ export default withOnyx<AuthScreensProps, AuthScreensProps>({
792774 key : ONYXKEYS . ONYX_UPDATES_LAST_UPDATE_ID_APPLIED_TO_CLIENT ,
793775 } ,
794776} ) ( AuthScreensMemoized ) ;
777+
0 commit comments