Skip to content

Commit b8768b8

Browse files
committed
update to use InitialUrlContext
1 parent 9c49560 commit b8768b8

4 files changed

Lines changed: 13 additions & 15 deletions

File tree

src/Expensify.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type {NativeEventSubscription} from 'react-native';
55
import {AppState, Linking, Platform} from 'react-native';
66
import type {OnyxEntry} from 'react-native-onyx';
77
import Onyx from 'react-native-onyx';
8+
import {InitialURLContext} from '@components/InitialURLContextProvider';
89
import ConfirmModal from './components/ConfirmModal';
910
import DeeplinkWrapper from './components/DeeplinkWrapper';
1011
import EmojiPicker from './components/EmojiPicker/EmojiPicker';
@@ -105,7 +106,7 @@ function Expensify() {
105106
useDebugShortcut();
106107

107108
const [initialUrl, setInitialUrl] = useState<Route | null>(null);
108-
109+
const {setIsAuthenticatedAtStartup} = useContext(InitialURLContext);
109110
useEffect(() => {
110111
if (isCheckingPublicRoom) {
111112
return;
@@ -201,6 +202,7 @@ function Expensify() {
201202

202203
appStateChangeListener.current = AppState.addEventListener('change', initializeClient);
203204

205+
setIsAuthenticatedAtStartup(isAuthenticated);
204206
// If the app is opened from a deep link, get the reportID (if exists) from the deep link and navigate to the chat report
205207
Linking.getInitialURL().then((url) => {
206208
setInitialUrl(url as Route);

src/components/InitialURLContextProvider.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
import React, {createContext, useEffect, useMemo, useState} from 'react';
22
import type {ReactNode} from 'react';
33
import {Linking} from 'react-native';
4-
import {hasAuthToken} from '@libs/actions/Session';
54
import type {Route} from '@src/ROUTES';
65

76
type InitialUrlContextType = {
87
initialURL: Route | null;
98
setInitialURL: React.Dispatch<React.SetStateAction<Route | null>>;
9+
isAuthenticatedAtStartup: boolean;
10+
setIsAuthenticatedAtStartup: React.Dispatch<React.SetStateAction<boolean>>;
1011
};
1112

1213
/** Initial url that will be opened when NewDot is embedded into Hybrid App. */
1314
const InitialURLContext = createContext<InitialUrlContextType>({
1415
initialURL: null,
1516
setInitialURL: () => {},
17+
isAuthenticatedAtStartup: false,
18+
setIsAuthenticatedAtStartup: () => {},
1619
});
1720

1821
type InitialURLContextProviderProps = {
@@ -22,7 +25,7 @@ type InitialURLContextProviderProps = {
2225

2326
function InitialURLContextProvider({children}: InitialURLContextProviderProps) {
2427
const [initialURL, setInitialURL] = useState<Route | null>(null);
25-
const [isAuthenticatedAtStartup, setIsAuthenticatedAtStartup] = useState<boolean>();
28+
const [isAuthenticatedAtStartup, setIsAuthenticatedAtStartup] = useState<boolean>(false);
2629

2730
useEffect(() => {
2831
Linking.getInitialURL().then((initURL) => {
@@ -33,16 +36,12 @@ function InitialURLContextProvider({children}: InitialURLContextProviderProps) {
3336
});
3437
}, []);
3538

36-
useEffect(() => {
37-
const isAuthenticated = hasAuthToken();
38-
setIsAuthenticatedAtStartup(isAuthenticated);
39-
}, []);
40-
4139
const initialUrlContext = useMemo(
4240
() => ({
4341
initialURL,
4442
setInitialURL,
4543
isAuthenticatedAtStartup,
44+
setIsAuthenticatedAtStartup,
4645
}),
4746
[initialURL, isAuthenticatedAtStartup],
4847
);

src/libs/Navigation/AppNavigator/AuthScreens.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ function AuthScreens({session, lastOpenedPublicRoomID, initialLastUpdateIDApplie
244244
const prevIsOnboardingLoading = usePrevious(isOnboardingLoading);
245245
const [shouldShowRequire2FAPage, setShouldShowRequire2FAPage] = useState(!!account?.needsTwoFactorAuthSetup && !account.requiresTwoFactorAuth);
246246
const navigation = useNavigation();
247-
const {initialURL} = useContext(InitialURLContext);
247+
const {initialURL, isAuthenticatedAtStartup, setIsAuthenticatedAtStartup} = useContext(InitialURLContext);
248248

249249
// State to track whether the delegator's authentication is completed before displaying data
250250
const [isDelegatorFromOldDotIsReady, setIsDelegatorFromOldDotIsReady] = useState(false);
@@ -326,8 +326,10 @@ function AuthScreens({session, lastOpenedPublicRoomID, initialLastUpdateIDApplie
326326
});
327327
} else {
328328
const reportID = getReportIDFromLink(initialURL ?? null);
329-
if (reportID) {
329+
if (reportID && !isAuthenticatedAtStartup) {
330330
Report.openReport(reportID);
331+
// Don't want to call `openReport` again when logging out and then logging in
332+
setIsAuthenticatedAtStartup(true);
331333
}
332334
App.openApp();
333335
}

src/libs/actions/Report.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3440,11 +3440,6 @@ function openReportFromDeepLink(url: string) {
34403440
return;
34413441
}
34423442

3443-
// Navigation for signed users is handled by react-navigation.
3444-
if (isAuthenticated) {
3445-
return;
3446-
}
3447-
34483443
const navigateHandler = (reportParam?: OnyxEntry<Report>) => {
34493444
// Check if the report exists in the collection
34503445
const report = reportParam ?? allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`];

0 commit comments

Comments
 (0)