Skip to content

Commit d2264bd

Browse files
committed
using context
1 parent e5874da commit d2264bd

6 files changed

Lines changed: 24 additions & 24 deletions

File tree

src/components/InitialURLContextProvider.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
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';
45
import type {AppProps} from '@src/App';
56
import type {Route} from '@src/ROUTES';
67

@@ -22,6 +23,9 @@ type InitialURLContextProviderProps = AppProps & {
2223

2324
function InitialURLContextProvider({children, url}: InitialURLContextProviderProps) {
2425
const [initialURL, setInitialURL] = useState<Route | undefined>();
26+
const [isAuthenticatedAtStartup, setIsAuthenticatedAtStartup] = useState<boolean>();
27+
28+
console.log('debug', {initialURL, isAuthenticatedAtStartup});
2529

2630
useEffect(() => {
2731
if (url) {
@@ -33,12 +37,18 @@ function InitialURLContextProvider({children, url}: InitialURLContextProviderPro
3337
});
3438
}, [url]);
3539

40+
useEffect(() => {
41+
const isAuthenticated = hasAuthToken();
42+
setIsAuthenticatedAtStartup(isAuthenticated);
43+
}, []);
44+
3645
const initialUrlContext = useMemo(
3746
() => ({
3847
initialURL,
3948
setInitialURL,
49+
isAuthenticatedAtStartup,
4050
}),
41-
[initialURL],
51+
[initialURL, isAuthenticatedAtStartup],
4252
);
4353

4454
return <InitialURLContext.Provider value={initialUrlContext}>{children}</InitialURLContext.Provider>;

src/libs/Navigation/AppNavigator/AuthScreens.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import type {RouteProp} from '@react-navigation/native';
22
import {useNavigation} from '@react-navigation/native';
3-
import React, {memo, useEffect, useMemo, useRef, useState} from 'react';
3+
import React, {memo, useContext, useEffect, useMemo, useRef, useState} from 'react';
44
import type {OnyxEntry} from 'react-native-onyx';
55
import Onyx, {withOnyx} from 'react-native-onyx';
66
import ComposeProviders from '@components/ComposeProviders';
77
import DelegateNoAccessModalProvider from '@components/DelegateNoAccessModalProvider';
88
import FullScreenLoadingIndicator from '@components/FullscreenLoadingIndicator';
9+
import {InitialURLContext} from '@components/InitialURLContextProvider';
910
import LockedAccountModalProvider from '@components/LockedAccountModalProvider';
1011
import OptionsListContextProvider from '@components/OptionListContextProvider';
1112
import PriorityModeController from '@components/PriorityModeController';
@@ -89,9 +90,6 @@ type AuthScreensProps = {
8990

9091
/** The last Onyx update ID was applied to the client */
9192
initialLastUpdateIDAppliedToClient: OnyxEntry<number>;
92-
93-
/** Initial url */
94-
initialUrl: string | null;
9593
};
9694

9795
const loadAttachmentModalScreen = () => require<ReactComponentModule>('../../../pages/media/AttachmentModalScreen').default;
@@ -226,7 +224,7 @@ const modalScreenListenersWithCancelSearch = {
226224
},
227225
};
228226

229-
function AuthScreens({session, lastOpenedPublicRoomID, initialLastUpdateIDAppliedToClient, initialUrl}: AuthScreensProps) {
227+
function AuthScreens({session, lastOpenedPublicRoomID, initialLastUpdateIDAppliedToClient}: AuthScreensProps) {
230228
const theme = useTheme();
231229
const StyleUtils = useStyleUtils();
232230
const {shouldUseNarrowLayout} = useResponsiveLayout();
@@ -246,6 +244,7 @@ function AuthScreens({session, lastOpenedPublicRoomID, initialLastUpdateIDApplie
246244
const prevIsOnboardingLoading = usePrevious(isOnboardingLoading);
247245
const [shouldShowRequire2FAPage, setShouldShowRequire2FAPage] = useState(!!account?.needsTwoFactorAuthSetup && !account.requiresTwoFactorAuth);
248246
const navigation = useNavigation();
247+
const {initialURL} = useContext(InitialURLContext);
249248

250249
// State to track whether the delegator's authentication is completed before displaying data
251250
const [isDelegatorFromOldDotIsReady, setIsDelegatorFromOldDotIsReady] = useState(false);
@@ -326,7 +325,7 @@ function AuthScreens({session, lastOpenedPublicRoomID, initialLastUpdateIDApplie
326325
setIsDelegatorFromOldDotIsReady(true);
327326
});
328327
} else {
329-
const reportID = getReportIDFromLink(initialUrl);
328+
const reportID = getReportIDFromLink(initialURL ?? null);
330329
if (reportID) {
331330
Report.openReport(reportID);
332331
}
@@ -789,7 +788,7 @@ const AuthScreensMemoized = memo(AuthScreens, () => true);
789788
// Further analysis required and more details can be seen here:
790789
// https://github.com/Expensify/App/issues/50560
791790
// eslint-disable-next-line
792-
export default withOnyx<AuthScreensProps, Omit<AuthScreensProps, 'initialUrl'>>({
791+
export default withOnyx<AuthScreensProps, AuthScreensProps>({
793792
session: {
794793
key: ONYXKEYS.SESSION,
795794
},

src/libs/Navigation/AppNavigator/index.native.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,14 @@ import type ReactComponentModule from '@src/types/utils/ReactComponentModule';
44
type AppNavigatorProps = {
55
/** If we have an authToken this is true */
66
authenticated: boolean;
7-
8-
/** Initial url */
9-
initialUrl: string | null;
107
};
118

12-
function AppNavigator({authenticated, initialUrl}: AppNavigatorProps) {
9+
function AppNavigator({authenticated}: AppNavigatorProps) {
1310
if (authenticated) {
14-
const AuthScreens = require<ReactComponentModule<{initialUrl: string | null}>>('./AuthScreens').default;
11+
const AuthScreens = require<ReactComponentModule>('./AuthScreens').default;
1512

1613
// These are the protected screens and only accessible when an authToken is present
17-
return <AuthScreens initialUrl={initialUrl} />;
14+
return <AuthScreens />;
1815
}
1916

2017
const PublicScreens = require<ReactComponentModule>('./PublicScreens').default;

src/libs/Navigation/AppNavigator/index.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,14 @@ const PublicScreens = lazy(() => lazyRetry(() => import('./PublicScreens')));
77
type AppNavigatorProps = {
88
/** If we have an authToken this is true */
99
authenticated: boolean;
10-
11-
/** Initial url */
12-
initialUrl: string | null;
1310
};
1411

15-
function AppNavigator({authenticated, initialUrl}: AppNavigatorProps) {
12+
function AppNavigator({authenticated}: AppNavigatorProps) {
1613
if (authenticated) {
1714
// These are the protected screens and only accessible when an authToken is present
1815
return (
1916
<Suspense fallback={null}>
20-
<AuthScreens initialUrl={initialUrl}/>
17+
<AuthScreens />
2118
</Suspense>
2219
);
2320
}

src/libs/Navigation/NavigationRoot.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,7 @@ function NavigationRoot({authenticated, lastVisitedPath, initialUrl, onReady}: N
251251
enabled: false,
252252
}}
253253
>
254-
<AppNavigator
255-
authenticated={authenticated}
256-
initialUrl={initialUrl}
257-
/>
254+
<AppNavigator authenticated={authenticated} />
258255
</NavigationContainer>
259256
);
260257
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
type ReactComponentModule<T = unknown> = {default: React.ComponentType<T>};
1+
type ReactComponentModule = {default: React.ComponentType};
22

33
export default ReactComponentModule;

0 commit comments

Comments
 (0)