Skip to content

Commit 97ceb10

Browse files
authored
Merge pull request Expensify#68001 from DylanDylann/refactor-AuthScreens-to-remove-connect-method
Refactor src/libs/Navigation/AppNavigator/AuthScreens.tsx to remove Onyx.connect() references
2 parents 67ddf46 + d769bf7 commit 97ceb10

2 files changed

Lines changed: 34 additions & 64 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"test:debug": "TZ=utc NODE_OPTIONS='--inspect-brk --experimental-vm-modules' jest --runInBand",
4747
"perf-test": "NODE_OPTIONS=--experimental-vm-modules npx reassure",
4848
"typecheck": "NODE_OPTIONS=--max_old_space_size=8192 tsc",
49-
"lint": "NODE_OPTIONS=--max_old_space_size=8192 eslint . --max-warnings=259 --cache --cache-location=node_modules/.cache/eslint",
49+
"lint": "NODE_OPTIONS=--max_old_space_size=8192 eslint . --max-warnings=255 --cache --cache-location=node_modules/.cache/eslint",
5050
"lint-changed": "NODE_OPTIONS=--max_old_space_size=8192 ./scripts/lintChanged.sh",
5151
"lint-watch": "npx eslint-watch --watch --changed",
5252
"shellcheck": "./scripts/shellCheck.sh",

src/libs/Navigation/AppNavigator/AuthScreens.tsx

Lines changed: 33 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type {RouteProp} from '@react-navigation/native';
22
import {useNavigation} from '@react-navigation/native';
33
import React, {memo, useContext, useEffect, useMemo, useRef, useState} from 'react';
44
import type {OnyxEntry} from 'react-native-onyx';
5-
import Onyx, {withOnyx} from 'react-native-onyx';
5+
import {withOnyx} from 'react-native-onyx';
66
import ComposeProviders from '@components/ComposeProviders';
77
import DelegateNoAccessModalProvider from '@components/DelegateNoAccessModalProvider';
88
import FullScreenLoadingIndicator from '@components/FullscreenLoadingIndicator';
@@ -62,7 +62,7 @@ import ONYXKEYS from '@src/ONYXKEYS';
6262
import ROUTES from '@src/ROUTES';
6363
import SCREENS from '@src/SCREENS';
6464
import 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';
6666
import {isEmptyObject} from '@src/types/utils/EmptyObject';
6767
import type ReactComponentModule from '@src/types/utils/ReactComponentModule';
6868
import 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

Comments
 (0)