Skip to content

Commit

Permalink
Add hybridAppSettings AppProp
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuuszzzzz committed Oct 30, 2024
1 parent 290748d commit a2266c8
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 34 deletions.
10 changes: 7 additions & 3 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ import CONFIG from './CONFIG';
import Expensify from './Expensify';
import useDefaultDragAndDrop from './hooks/useDefaultDragAndDrop';
import {ReportIDsContextProvider} from './hooks/useReportIDs';
import type HybridAppSettings from './libs/actions/HybridApp';
import OnyxUpdateManager from './libs/actions/OnyxUpdateManager';
import {ReportAttachmentsProvider} from './pages/home/report/ReportAttachmentsContext';
import type {Route} from './ROUTES';
import {SplashScreenStateContextProvider} from './SplashScreenStateContext';

type AppProps = {
/** URL passed to our top-level React Native component by HybridApp. Will always be undefined in "pure" NewDot builds. */
url?: Route;
hybridAppSettings?: HybridAppSettings;
};

LogBox.ignoreLogs([
Expand All @@ -56,14 +57,17 @@ const fill = {flex: 1};

const StrictModeWrapper = CONFIG.USE_REACT_STRICT_MODE_IN_DEV ? React.StrictMode : ({children}: {children: React.ReactElement}) => children;

function App({url}: AppProps) {
function App({url, hybridAppSettings}: AppProps) {
useDefaultDragAndDrop();
OnyxUpdateManager();

return (
<StrictModeWrapper>
<SplashScreenStateContextProvider>
<InitialURLContextProvider url={url}>
<InitialURLContextProvider
url={url}
hybridAppSettings={hybridAppSettings}
>
<GestureHandlerRootView style={fill}>
<ComposeProviders
components={[
Expand Down
2 changes: 1 addition & 1 deletion src/Expensify.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function Expensify() {
const [screenShareRequest] = useOnyx(ONYXKEYS.SCREEN_SHARE_REQUEST);
const [focusModeNotification] = useOnyx(ONYXKEYS.FOCUS_MODE_NOTIFICATION, {initWithStoredValues: false});
const [lastVisitedPath] = useOnyx(ONYXKEYS.LAST_VISITED_PATH);
const [useNewDotSignInPage] = useOnyx(ONYXKEYS.USE_NEWDOT_SIGN_IN_PAGE);
const [useNewDotSignInPage] = useOnyx(ONYXKEYS.HYBRID_APP, {selector: (data) => data?.useNewDotSignInPage});

useEffect(() => {
if (!account?.needsTwoFactorAuthSetup || account.requiresTwoFactorAuth) {
Expand Down
4 changes: 0 additions & 4 deletions src/ONYXKEYS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,9 +448,6 @@ const ONYXKEYS = {
/** Company cards custom names */
NVP_EXPENSIFY_COMPANY_CARDS_CUSTOM_NAMES: 'nvp_expensify_ccCustomNames',

/** Stores the information if HybridApp uses NewDot's sign in flow */
USE_NEWDOT_SIGN_IN_PAGE: 'useNewDotSignInPage',

HYBRID_APP: 'hybridApp',

/** Collection Keys */
Expand Down Expand Up @@ -1017,7 +1014,6 @@ type OnyxValuesMapping = {
[ONYXKEYS.IS_USING_IMPORTED_STATE]: boolean;
[ONYXKEYS.SHOULD_SHOW_SAVED_SEARCH_RENAME_TOOLTIP]: boolean;
[ONYXKEYS.NVP_EXPENSIFY_COMPANY_CARDS_CUSTOM_NAMES]: Record<string, string>;
[ONYXKEYS.USE_NEWDOT_SIGN_IN_PAGE]: boolean;
[ONYXKEYS.HYBRID_APP]: OnyxTypes.HybridApp;
};
type OnyxValues = OnyxValuesMapping & OnyxCollectionValuesMapping & OnyxFormValuesMapping & OnyxFormDraftValuesMapping;
Expand Down
12 changes: 7 additions & 5 deletions src/components/InitialURLContextProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, {createContext, useEffect, useMemo, useState} from 'react';
import type {ReactNode} from 'react';
import {Linking} from 'react-native';
import type HybridAppSettings from '@libs/actions/HybridApp';
import {signInAfterTransitionFromOldDot} from '@libs/actions/Session';
import CONST from '@src/CONST';
import type {Route} from '@src/ROUTES';
Expand All @@ -18,20 +19,21 @@ const InitialURLContext = createContext<InitialUrlContextType>({
});

type InitialURLContextProviderProps = {
/** URL passed to our top-level React Native component by HybridApp. Will always be undefined in "pure" NewDot builds. */
url?: Route;

hybridAppSettings?: HybridAppSettings;

/** Children passed to the context provider */
children: ReactNode;
};

function InitialURLContextProvider({children, url}: InitialURLContextProviderProps) {
function InitialURLContextProvider({children, url, hybridAppSettings}: InitialURLContextProviderProps) {
const [initialURL, setInitialURL] = useState<Route | undefined>(url);
const {setSplashScreenState} = useSplashScreenStateContext();

useEffect(() => {
if (url) {
signInAfterTransitionFromOldDot(url).then((route) => {
if (url && hybridAppSettings) {
signInAfterTransitionFromOldDot(url, hybridAppSettings).then((route) => {
setInitialURL(route);
setSplashScreenState(CONST.BOOT_SPLASH_STATE.READY_TO_BE_HIDDEN);
});
Expand All @@ -40,7 +42,7 @@ function InitialURLContextProvider({children, url}: InitialURLContextProviderPro
Linking.getInitialURL().then((initURL) => {
setInitialURL(initURL as Route);
});
}, [setSplashScreenState, url]);
}, [hybridAppSettings, setSplashScreenState, url]);

const initialUrlContext = useMemo(
() => ({
Expand Down
3 changes: 3 additions & 0 deletions src/libs/actions/HybridApp/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import type HybridAppSettings from './types';

export default HybridAppSettings;
12 changes: 12 additions & 0 deletions src/libs/actions/HybridApp/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type ONYXKEYS from '@src/ONYXKEYS';
import type {TryNewDot} from '@src/types/onyx';
import type HybridApp from '@src/types/onyx/HybridApp';

type HybridAppSettings = {
initialOnyxValues: {
[ONYXKEYS.HYBRID_APP]: HybridApp;
[ONYXKEYS.NVP_TRYNEWDOT]?: TryNewDot;
};
};

export default HybridAppSettings;
29 changes: 8 additions & 21 deletions src/libs/actions/Session/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {InteractionManager, Linking, NativeModules} from 'react-native';
import type {OnyxEntry, OnyxUpdate} from 'react-native-onyx';
import Onyx from 'react-native-onyx';
import type {ValueOf} from 'type-fest';
import type HybridAppSettings from '@libs/actions/HybridApp';
import * as PersistedRequests from '@libs/actions/PersistedRequests';
import * as API from '@libs/API';
import type {
Expand Down Expand Up @@ -479,29 +480,20 @@ function signUpUser() {
API.write(WRITE_COMMANDS.SIGN_UP_USER, params, {optimisticData, successData, failureData});
}

function signInAfterTransitionFromOldDot(transitionURL: string) {
const [route, queryParams] = transitionURL.split('?');
const queryParamsObject = queryParams
? Object.fromEntries(
queryParams.split('&').map((param) => {
const [key, value] = param.split('=');
return [key, value];
}),
)
: {};

const {useNewDotSignInPage, isSingleNewDotEntry} = queryParamsObject;
function signInAfterTransitionFromOldDot(route: Route, hybridAppSettings: HybridAppSettings) {
const {initialOnyxValues} = hybridAppSettings;
const {hybridApp} = initialOnyxValues;

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

return Onyx.clear();
};

const initAppAfterTransition = () => {
if (useNewDotSignInPage === 'true') {
if (hybridApp.useNewDotSignInPage) {
return Promise.resolve();
}

Expand All @@ -510,18 +502,13 @@ function signInAfterTransitionFromOldDot(transitionURL: string) {

const setSessionDataAndOpenApp = new Promise<Route>((resolve) => {
clearOnyxBeforeSignIn()
.then(() =>
Onyx.multiSet({
[ONYXKEYS.USE_NEWDOT_SIGN_IN_PAGE]: useNewDotSignInPage === 'true',
[ONYXKEYS.NVP_TRYNEWDOT]: {classicRedirect: {dismissed: 'true'}}, // This data is mocked and should be returned by BeginSignUp/SignInUser API commands
}),
)
.then(() => Onyx.multiSet(initialOnyxValues))
.then(initAppAfterTransition)
.catch((error) => {
Log.hmmm('[HybridApp] Initialization of HybridApp has failed. Forcing transition', {error});
})
.finally(() => {
resolve(`${route}?singleNewDotEntry=${isSingleNewDotEntry}` as Route);
resolve(route);
});
});

Expand Down
3 changes: 3 additions & 0 deletions src/types/onyx/HybridApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
type HybridApp = {
/** */
isSigningIn?: boolean;

/** Stores the information if HybridApp uses NewDot's sign in flow */
useNewDotSignInPage: boolean;
};

export default HybridApp;

0 comments on commit a2266c8

Please sign in to comment.