Skip to content

Commit

Permalink
Remove redundant keys
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuuszzzzz committed Nov 26, 2024
1 parent e1a6154 commit 277f004
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 112 deletions.
11 changes: 9 additions & 2 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 {signInAfterTransitionFromOldDot} from '@libs/actions/Session';
Expand Down Expand Up @@ -30,19 +30,26 @@ function InitialURLContextProvider({children, url, hybridAppSettings}: InitialUR
const [initialURL, setInitialURL] = useState<Route | undefined>(url);
const {setSplashScreenState} = useSplashScreenStateContext();

const firstRenderRef = useRef(true);

useEffect(() => {
if (url && hybridAppSettings) {
if (url && hybridAppSettings && firstRenderRef.current) {
signInAfterTransitionFromOldDot(url, hybridAppSettings).then((route) => {
setInitialURL(route);
setSplashScreenState(CONST.BOOT_SPLASH_STATE.READY_TO_BE_HIDDEN);
});
firstRenderRef.current = false;
return;
}
Linking.getInitialURL().then((initURL) => {
setInitialURL(initURL as Route);
});
}, [hybridAppSettings, setSplashScreenState, url]);

useEffect(() => {
console.debug('VALUE OF firstRenderRef', {firstRenderRef: firstRenderRef.current});
});

const initialUrlContext = useMemo(
() => ({
initialURL,
Expand Down
55 changes: 17 additions & 38 deletions src/libs/HybridApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,7 @@ import type {OnyxEntry} from 'react-native-onyx';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Credentials, HybridApp, Session, TryNewDot} from '@src/types/onyx';
import {
resetSigningInLogic,
setIsSigningIn,
setNewDotSignInState,
setOldDotSignInError,
setOldDotSignInState,
setReadyToShowAuthScreens,
setReadyToSwitchToClassicExperience,
} from './actions/HybridApp';
import * as HybridAppActions from './actions/HybridApp';
import type {Init} from './ActiveClientManager/types';
import Log from './Log';

Expand All @@ -22,13 +14,15 @@ let currentTryNewDot: OnyxEntry<TryNewDot>;
Onyx.connect({
key: ONYXKEYS.HYBRID_APP,
callback: (hybridApp) => {
console.debug('Last hybridApp value', {hybridApp});
handleSignInFlow(hybridApp, currentTryNewDot);
},
});

Onyx.connect({
key: ONYXKEYS.NVP_TRYNEWDOT,
callback: (tryNewDot) => {
console.debug('Last tryNewDot value', {tryNewDot});
handleSignInFlow(currentHybridApp, tryNewDot);
},
});
Expand All @@ -38,7 +32,7 @@ Onyx.connect({
key: ONYXKEYS.SESSION,
callback: (session: OnyxEntry<Session>) => {
if (!currentSession?.authToken && session?.authToken && currentHybridApp?.newDotSignInState === CONST.HYBRID_APP_SIGN_IN_STATE.STARTED) {
setNewDotSignInState(CONST.HYBRID_APP_SIGN_IN_STATE.FINISHED);
HybridAppActions.setNewDotSignInState(CONST.HYBRID_APP_SIGN_IN_STATE.FINISHED);
}
currentSession = session;
},
Expand All @@ -61,21 +55,14 @@ function handleSignInFlow(hybridApp: OnyxEntry<HybridApp>, tryNewDot: OnyxEntry<
return;
}

// reset sign in logic
if (hybridApp?.shouldResetSigningInLogic) {
Log.info('[HybridApp] Resetting sign-in state');
resetSigningInLogic();
return;
}

if (currentHybridApp?.oldDotSignInState === CONST.HYBRID_APP_SIGN_IN_STATE.RETRYING_AFTER_FAILURE && hybridApp?.oldDotSignInState === CONST.HYBRID_APP_SIGN_IN_STATE.FINISHED) {
if (hybridApp?.oldDotSignInError) {
Log.info('Unable to open OldDot. Sign-in has failed');
setOldDotSignInState(CONST.HYBRID_APP_SIGN_IN_STATE.FAILED_AGAIN);
Log.info('[HybridApp] Unable to open OldDot. Sign-in has failed again');
HybridAppActions.setOldDotSignInState(CONST.HYBRID_APP_SIGN_IN_STATE.FAILED_AGAIN);
return;
}

Log.info('Closing NewDot as retrying sign-in to OldDot succeeded');
Log.info('[HybridApp] Closing NewDot as retrying sign-in to OldDot succeeded');
NativeModules.HybridAppModule.closeReactNativeApp(false, true);
}

Expand All @@ -86,10 +73,11 @@ function handleSignInFlow(hybridApp: OnyxEntry<HybridApp>, tryNewDot: OnyxEntry<
shouldUseOldApp(tryNewDot)
) {
if (hybridApp?.oldDotSignInError) {
Log.info('Unable to open OldDot. Sign-in has failed');
Log.info('[HybridApp] Unable to open OldDot. Sign-in has failed');
return;
}
Log.info('Closing NewDot as sign-in to OldDot succeeded');

Log.info('[HybridApp] Closing NewDot as sign-in to OldDot succeeded');
NativeModules.HybridAppModule.closeReactNativeApp(false, false);
}

Expand All @@ -100,32 +88,23 @@ function handleSignInFlow(hybridApp: OnyxEntry<HybridApp>, tryNewDot: OnyxEntry<
credentials?.autoGeneratedPassword &&
tryNewDot !== undefined
) {
if (shouldUseOldApp(tryNewDot)) {
setIsSigningIn(true);
} else {
setReadyToShowAuthScreens(true);
if (!shouldUseOldApp(tryNewDot)) {
Log.info('[HybridApp] The user should see NewDot. There is no need to block the user on the `SignInPage` until the sign-in process is completed on the OldDot side.');
HybridAppActions.setReadyToShowAuthScreens(true);
}

Log.info('[HybridApp] Performing sign-in on OldDot side');
setOldDotSignInState(CONST.HYBRID_APP_SIGN_IN_STATE.STARTED);
NativeModules.HybridAppModule.signInToOldDot(credentials.autoGeneratedLogin, credentials.autoGeneratedPassword);
Log.info(`[HybridApp] Performing sign-in${shouldUseOldApp(tryNewDot) ? '' : ' (in background)'} on OldDot side`);
HybridAppActions.startOldDotSignIn(credentials.autoGeneratedLogin, credentials.autoGeneratedPassword);
}

currentHybridApp = hybridApp;
currentTryNewDot = tryNewDot;
}

function onOldDotSignInFinished(data: string) {
Log.info(`[HybridApp] onSignInFinished event received with data: ${data}`, true);
const eventData = JSON.parse(data) as {errorMessage: string};

setIsSigningIn(false);
setOldDotSignInError(eventData.errorMessage);
setOldDotSignInState(CONST.HYBRID_APP_SIGN_IN_STATE.FINISHED);

if (eventData.errorMessage === null) {
setReadyToSwitchToClassicExperience(true);
}
Log.info(`[HybridApp] onSignInFinished event received`, true, {eventData});
HybridAppActions.finishOldDotSignIn(eventData.errorMessage);
}

const init: Init = () => {
Expand Down
69 changes: 49 additions & 20 deletions src/libs/actions/HybridApp/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import {NativeModules} from 'react-native';
import Onyx from 'react-native-onyx';
import type {ValueOf} from 'type-fest';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {HybridApp} from '@src/types/onyx';
import type HybridAppSettings from './types';

function parseHybridAppSettings(hybridAppSettings: string): HybridAppSettings {
return JSON.parse(hybridAppSettings) as HybridAppSettings;
}

function setIsSigningIn(isSigningIn: boolean) {
Onyx.merge(ONYXKEYS.HYBRID_APP, {isSigningIn});
}

function setOldDotSignInError(oldDotSignInError: string | null) {
Onyx.merge(ONYXKEYS.HYBRID_APP, {oldDotSignInError});
}
Expand All @@ -20,14 +18,6 @@ function setReadyToShowAuthScreens(readyToShowAuthScreens: boolean) {
Onyx.merge(ONYXKEYS.HYBRID_APP, {readyToShowAuthScreens});
}

function setReadyToSwitchToClassicExperience(readyToSwitchToClassicExperience: boolean) {
Onyx.merge(ONYXKEYS.HYBRID_APP, {readyToSwitchToClassicExperience});
}

function setShouldResetSigningInLogic(shouldResetSigningInLogic: boolean) {
Onyx.merge(ONYXKEYS.HYBRID_APP, {shouldResetSigningInLogic});
}

function setUseNewDotSignInPage(useNewDotSignInPage: boolean) {
Onyx.merge(ONYXKEYS.HYBRID_APP, {useNewDotSignInPage});
}
Expand All @@ -44,29 +34,68 @@ function setOldDotSignInState(oldDotSignInState: ValueOf<typeof CONST.HYBRID_APP
Onyx.merge(ONYXKEYS.HYBRID_APP, {oldDotSignInState});
}

function resetSigningInLogic() {
function resetHybridAppSignInState() {
Onyx.merge(ONYXKEYS.HYBRID_APP, {
readyToShowAuthScreens: false,
readyToSwitchToClassicExperience: false,
isSigningIn: false,
useNewDotSignInPage: true,
oldDotSignInError: null,
oldDotSignInState: CONST.HYBRID_APP_SIGN_IN_STATE.NOT_STARTED,
newDotSignInState: CONST.HYBRID_APP_SIGN_IN_STATE.NOT_STARTED,
shouldResetSigningInLogic: false,
});
}

function startOldDotSignIn(autoGeneratedLogin: string, autoGeneratedPassword: string) {
Onyx.merge(ONYXKEYS.HYBRID_APP, {
oldDotSignInState: CONST.HYBRID_APP_SIGN_IN_STATE.STARTED,
});

NativeModules.HybridAppModule.signInToOldDot(autoGeneratedLogin, autoGeneratedPassword);
}

function retryOldDotSignInAfterFailure(autoGeneratedLogin: string, autoGeneratedPassword: string) {
Onyx.merge(ONYXKEYS.HYBRID_APP, {
oldDotSignInError: null,
oldDotSignInState: CONST.HYBRID_APP_SIGN_IN_STATE.RETRYING_AFTER_FAILURE,
});

NativeModules.HybridAppModule.signInToOldDot(autoGeneratedLogin, autoGeneratedPassword);
}

function finishOldDotSignIn(errorMessage: string | null) {
Onyx.merge(ONYXKEYS.HYBRID_APP, {
oldDotSignInError: errorMessage,
oldDotSignInState: CONST.HYBRID_APP_SIGN_IN_STATE.FINISHED,
});
}

function prepareHybridAppAfterTransitionToNewDot(hybridApp: HybridApp) {
if (hybridApp?.useNewDotSignInPage) {
return Onyx.merge(ONYXKEYS.HYBRID_APP, {
...hybridApp,
readyToShowAuthScreens: false,
oldDotSignInError: null,
oldDotSignInState: CONST.HYBRID_APP_SIGN_IN_STATE.NOT_STARTED,
newDotSignInState: CONST.HYBRID_APP_SIGN_IN_STATE.NOT_STARTED,
});
}

return Onyx.merge(ONYXKEYS.HYBRID_APP, {
...hybridApp,
readyToShowAuthScreens: true,
});
}

export {
parseHybridAppSettings,
setOldDotSignInError,
setIsSigningIn,
setReadyToShowAuthScreens,
setReadyToSwitchToClassicExperience,
setShouldResetSigningInLogic,
setUseNewDotSignInPage,
setLoggedOutFromOldDot,
setNewDotSignInState,
setOldDotSignInState,
resetSigningInLogic,
resetHybridAppSignInState,
retryOldDotSignInAfterFailure,
finishOldDotSignIn,
startOldDotSignIn,
prepareHybridAppAfterTransitionToNewDot,
};
23 changes: 5 additions & 18 deletions src/libs/actions/Session/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,7 @@ import * as App from '@userActions/App';
import {KEYS_TO_PRESERVE, openApp} from '@userActions/App';
import {KEYS_TO_PRESERVE_DELEGATE_ACCESS} from '@userActions/Delegate';
import * as Device from '@userActions/Device';
import {
parseHybridAppSettings,
setLoggedOutFromOldDot,
setNewDotSignInState,
setOldDotSignInState,
setReadyToShowAuthScreens,
setReadyToSwitchToClassicExperience,
setUseNewDotSignInPage,
} from '@userActions/HybridApp';
import * as HybridAppActions from '@userActions/HybridApp';
import * as PriorityMode from '@userActions/PriorityMode';
import redirectToSignIn from '@userActions/SignInRedirect';
import Timing from '@userActions/Timing';
Expand Down Expand Up @@ -490,14 +482,12 @@ function signUpUser() {
}

function signInAfterTransitionFromOldDot(route: Route, hybridAppSettings: string) {
const parsedHybridAppSettings = parseHybridAppSettings(hybridAppSettings);
const parsedHybridAppSettings = HybridAppActions.parseHybridAppSettings(hybridAppSettings);
const {initialOnyxValues} = parsedHybridAppSettings;
const {hybridApp, ...newDotOnyxValues} = initialOnyxValues;

const clearOnyxBeforeSignIn = () => {
if (!hybridApp.useNewDotSignInPage) {
setReadyToShowAuthScreens(true);
setReadyToSwitchToClassicExperience(true);
return Promise.resolve();
}

Expand All @@ -506,8 +496,6 @@ function signInAfterTransitionFromOldDot(route: Route, hybridAppSettings: string

const initAppAfterTransition = () => {
if (hybridApp.useNewDotSignInPage) {
setNewDotSignInState(CONST.HYBRID_APP_SIGN_IN_STATE.NOT_STARTED);
setOldDotSignInState(CONST.HYBRID_APP_SIGN_IN_STATE.NOT_STARTED);
return Promise.resolve();
}

Expand All @@ -516,7 +504,7 @@ function signInAfterTransitionFromOldDot(route: Route, hybridAppSettings: string

return new Promise<Route>((resolve) => {
clearOnyxBeforeSignIn()
.then(() => Onyx.merge(ONYXKEYS.HYBRID_APP, hybridApp))
.then(() => HybridAppActions.prepareHybridAppAfterTransitionToNewDot(hybridApp))
.then(() => Onyx.multiSet(newDotOnyxValues))
.then(() => {
if (!hybridApp.shouldRemoveDelegatedAccess) {
Expand All @@ -525,9 +513,8 @@ function signInAfterTransitionFromOldDot(route: Route, hybridAppSettings: string
return Onyx.clear(KEYS_TO_PRESERVE_DELEGATE_ACCESS);
})
.then(() => {
// This data is mocked and should be returned by BeginSignUp/SignInUser API commands
setUseNewDotSignInPage(!!hybridApp.useNewDotSignInPage);
setLoggedOutFromOldDot(!!hybridApp.loggedOutFromOldDot);
HybridAppActions.setUseNewDotSignInPage(!!hybridApp.useNewDotSignInPage);
HybridAppActions.setLoggedOutFromOldDot(!!hybridApp.loggedOutFromOldDot);
})
.then(initAppAfterTransition)
.catch((error) => {
Expand Down
16 changes: 6 additions & 10 deletions src/pages/settings/InitialSettingsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import {hasGlobalWorkspaceSettingsRBR} from '@libs/WorkspacesSettingsUtils';
import * as ReportActionContextMenu from '@pages/home/report/ContextMenu/ReportActionContextMenu';
import variables from '@styles/variables';
import * as App from '@userActions/App';
import {setIsSigningIn, setOldDotSignInError, setOldDotSignInState, setShouldResetSigningInLogic} from '@userActions/HybridApp';
import * as HybridAppActions from '@userActions/HybridApp';
import * as Link from '@userActions/Link';
import * as PaymentMethods from '@userActions/PaymentMethods';
import * as Session from '@userActions/Session';
Expand Down Expand Up @@ -256,21 +256,17 @@ function InitialSettingsPage({currentUserPersonalDetails}: InitialSettingsPagePr
...(NativeModules.HybridAppModule
? {
action: () => {
console.log('[HybridApp] ready to switch', hybridApp);
if (hybridApp?.readyToSwitchToClassicExperience) {
if (hybridApp?.oldDotSignInState === CONST.HYBRID_APP_SIGN_IN_STATE.FINISHED && !hybridApp?.oldDotSignInError) {
NativeModules.HybridAppModule.closeReactNativeApp(false, true);
setInitialURL(undefined);
return;
}

if (credentials?.autoGeneratedLogin && credentials?.autoGeneratedPassword) {
NativeModules.HybridAppModule.signInToOldDot(credentials.autoGeneratedLogin, credentials.autoGeneratedPassword);
setIsSigningIn(true);
setOldDotSignInError(null);
setOldDotSignInState(CONST.HYBRID_APP_SIGN_IN_STATE.RETRYING_AFTER_FAILURE);
HybridAppActions.retryOldDotSignInAfterFailure(credentials.autoGeneratedLogin, credentials.autoGeneratedPassword);
} else {
signOutAndRedirectToSignIn(undefined, undefined, false);
setShouldResetSigningInLogic(true);
HybridAppActions.resetHybridAppSignInState();
}
},
}
Expand Down Expand Up @@ -306,7 +302,7 @@ function InitialSettingsPage({currentUserPersonalDetails}: InitialSettingsPagePr
icon: Expensicons.Exit,
action: () => {
signOut(false);
setShouldResetSigningInLogic(true);
HybridAppActions.resetHybridAppSignInState();
},
},
],
Expand Down Expand Up @@ -484,7 +480,7 @@ function InitialSettingsPage({currentUserPersonalDetails}: InitialSettingsPagePr
confirmText={translate('workspace.upgrade.completed.gotIt')}
isVisible={hybridApp?.oldDotSignInState === CONST.HYBRID_APP_SIGN_IN_STATE.FAILED_AGAIN}
onConfirm={() => {
setOldDotSignInState(CONST.HYBRID_APP_SIGN_IN_STATE.FINISHED);
HybridAppActions.setOldDotSignInState(CONST.HYBRID_APP_SIGN_IN_STATE.FINISHED);
}}
shouldShowCancelButton={false}
/>
Expand Down
Loading

0 comments on commit 277f004

Please sign in to comment.