Skip to content

Commit

Permalink
Fix bug where OpenApp is called after opening app from background
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuuszzzzz committed Dec 6, 2024
1 parent a9bacb8 commit 118505f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 19 deletions.
19 changes: 13 additions & 6 deletions src/components/InitialURLContextProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {createContext, useEffect, useMemo, useState} from 'react';
import React, {createContext, useEffect, useMemo, useRef, useState} from 'react';
import type {ReactNode} from 'react';
import {Linking} from 'react-native';
import {useOnyx} from 'react-native-onyx';
Expand All @@ -9,6 +9,7 @@ import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Route} from '@src/ROUTES';
import {useSplashScreenStateContext} from '@src/SplashScreenStateContext';
import isLoadingOnyxValue from '@src/types/utils/isLoadingOnyxValue';

type InitialUrlContextType = {
initialURL: Route | undefined;
Expand All @@ -35,6 +36,9 @@ function InitialURLContextProvider({children, url, hybridAppSettings}: InitialUR
const [initialURL, setInitialURL] = useState<Route | ValueOf<typeof CONST.HYBRID_APP> | undefined>(url);
const [lastVisitedPath] = useOnyx(ONYXKEYS.LAST_VISITED_PATH);
const {splashScreenState, setSplashScreenState} = useSplashScreenStateContext();
const [tryNewDot, tryNewDotMetadata] = useOnyx(ONYXKEYS.NVP_TRYNEWDOT);
// We use `setupCalled` ref to guarantee that `signInAfterTransitionFromOldDot` is called once.
const setupCalled = useRef(false);

useEffect(() => {
if (url !== CONST.HYBRID_APP.REORDERING_REACT_NATIVE_ACTIVITY_TO_FRONT) {
Expand All @@ -53,16 +57,19 @@ function InitialURLContextProvider({children, url, hybridAppSettings}: InitialUR
}

if (url && hybridAppSettings) {
signInAfterTransitionFromOldDot(url, hybridAppSettings).then((route) => {
setInitialURL(route);
setSplashScreenState(CONST.BOOT_SPLASH_STATE.READY_TO_BE_HIDDEN);
});
if (!isLoadingOnyxValue(tryNewDotMetadata) && !setupCalled.current) {
setupCalled.current = true;
signInAfterTransitionFromOldDot(url, hybridAppSettings, tryNewDot).then((route) => {
setInitialURL(route);
setSplashScreenState(CONST.BOOT_SPLASH_STATE.READY_TO_BE_HIDDEN);
});
}
return;
}
Linking.getInitialURL().then((initURL) => {
setInitialURL(initURL as Route);
});
}, [hybridAppSettings, setSplashScreenState, url]);
}, [hybridAppSettings, setSplashScreenState, tryNewDot, tryNewDotMetadata, url]);

const initialUrlContext = useMemo(
() => ({
Expand Down
28 changes: 21 additions & 7 deletions src/libs/actions/Session/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import NetworkConnection from '@libs/NetworkConnection';
import * as Pusher from '@libs/Pusher/pusher';
import * as ReportUtils from '@libs/ReportUtils';
import * as SessionUtils from '@libs/SessionUtils';
import {resetDidUserLogInDuringSession} from '@libs/SessionUtils';
import {clearSoundAssetsCache} from '@libs/Sound';
import Timers from '@libs/Timers';
import {hideContextMenu} from '@pages/home/report/ContextMenu/ReportActionContextMenu';
Expand All @@ -54,6 +55,7 @@ import ONYXKEYS from '@src/ONYXKEYS';
import type {HybridAppRoute, Route} from '@src/ROUTES';
import ROUTES from '@src/ROUTES';
import SCREENS from '@src/SCREENS';
import type {TryNewDot} from '@src/types/onyx';
import type Credentials from '@src/types/onyx/Credentials';
import type Session from '@src/types/onyx/Session';
import type {AutoAuthState} from '@src/types/onyx/Session';
Expand Down Expand Up @@ -494,7 +496,7 @@ function signUpUser() {
});
}

function signInAfterTransitionFromOldDot(route: Route, hybridAppSettings: string) {
function signInAfterTransitionFromOldDot(route: Route, hybridAppSettings: string, tryNewDot: TryNewDot | undefined) {
const parsedHybridAppSettings = HybridAppActions.parseHybridAppSettings(hybridAppSettings);
const {initialOnyxValues} = parsedHybridAppSettings;
const {hybridApp, ...newDotOnyxValues} = initialOnyxValues;
Expand All @@ -507,16 +509,28 @@ function signInAfterTransitionFromOldDot(route: Route, hybridAppSettings: string
return Onyx.clear();
};

const resetDidUserLoginDuringSessionIfNeeded = () => {
if (newDotOnyxValues.nvp_tryNewDot === undefined || tryNewDot?.classicRedirect.dismissed !== true) {
return Promise.resolve();
}

Log.info("[HybridApp] OpenApp hasn't been called yet. Calling `resetDidUserLogInDuringSession`");
resetDidUserLogInDuringSession();
};

const handleDelegateAccess = () => {
if (!hybridApp.shouldRemoveDelegatedAccess) {
return;
}
return Onyx.clear(KEYS_TO_PRESERVE_DELEGATE_ACCESS);
};

return new Promise<Route>((resolve) => {
clearOnyxBeforeSignIn()
.then(() => HybridAppActions.prepareHybridAppAfterTransitionToNewDot(hybridApp))
.then(resetDidUserLoginDuringSessionIfNeeded)
.then(() => Onyx.multiSet(newDotOnyxValues))
.then(() => {
if (!hybridApp.shouldRemoveDelegatedAccess) {
return;
}
return Onyx.clear(KEYS_TO_PRESERVE_DELEGATE_ACCESS);
})
.then(handleDelegateAccess)
.catch((error) => {
Log.hmmm('[HybridApp] Initialization of HybridApp has failed. Forcing transition', {error});
})
Expand Down
6 changes: 0 additions & 6 deletions src/libs/actions/SignInRedirect.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import {NativeModules} from 'react-native';
import Onyx from 'react-native-onyx';
import * as ErrorUtils from '@libs/ErrorUtils';
import type {OnyxKey} from '@src/ONYXKEYS';
import ONYXKEYS from '@src/ONYXKEYS';
import * as HybridAppActions from './HybridApp';
import * as Policy from './Policy/Policy';

let currentIsOffline: boolean | undefined;
Expand Down Expand Up @@ -41,10 +39,6 @@ function clearStorageAndRedirect(errorMessage?: string): Promise<void> {

// `Onyx.clear` reinitializes the Onyx instance with initial values so use `Onyx.merge` instead of `Onyx.set`
Onyx.merge(ONYXKEYS.SESSION, {errors: ErrorUtils.getMicroSecondOnyxErrorWithMessage(errorMessage)});

if (NativeModules.HybridAppModule) {
HybridAppActions.resetSignInFlow();
}
});
}

Expand Down

0 comments on commit 118505f

Please sign in to comment.