Skip to content

Improve HybridApp initialProps #61912

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import CONFIG from './CONFIG';
import Expensify from './Expensify';
import {CurrentReportIDContextProvider} from './hooks/useCurrentReportID';
import useDefaultDragAndDrop from './hooks/useDefaultDragAndDrop';
import HybridAppSignIn from './HybridAppSignIn';
import OnyxUpdateManager from './libs/actions/OnyxUpdateManager';
import {ReportAttachmentsProvider} from './pages/home/report/ReportAttachmentsContext';
import type {Route} from './ROUTES';
Expand All @@ -53,8 +54,6 @@ type AppProps = {
url?: Route;
/** Serialized configuration data required to initialize the React Native app (e.g. authentication details) */
hybridAppSettings?: string;
/** A timestamp indicating when the initial properties were last updated, used to detect changes */
timestamp?: string;
};

LogBox.ignoreLogs([
Expand All @@ -70,18 +69,14 @@ const fill = {flex: 1};

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

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

return (
<StrictModeWrapper>
<SplashScreenStateContextProvider>
<InitialURLContextProvider
url={url}
hybridAppSettings={hybridAppSettings}
timestamp={timestamp}
>
<InitialURLContextProvider url={url}>
<GestureHandlerRootView style={fill}>
<ComposeProviders
components={[
Expand Down Expand Up @@ -118,6 +113,12 @@ function App({url, hybridAppSettings, timestamp}: AppProps) {
<CustomStatusBarAndBackground />
<ErrorBoundary errorMessage="NewExpensify crash caught by error boundary">
<ColorSchemeWrapper>
{CONFIG.IS_HYBRID_APP && (
<HybridAppSignIn
url={url}
hybridAppSettings={hybridAppSettings}
/>
)}
<Expensify />
</ColorSchemeWrapper>
</ErrorBoundary>
Expand Down
3 changes: 3 additions & 0 deletions src/Expensify.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ function Expensify() {

// Open chat report from a deep link (only mobile native)
Linking.addEventListener('url', (state) => {
if (CONFIG.IS_HYBRID_APP) {
return;
}
Report.openReportFromDeepLink(state.url);
});

Expand Down
53 changes: 53 additions & 0 deletions src/HybridAppSignIn.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import {findFocusedRoute} from '@react-navigation/native';
import {useContext, useEffect, useState} from 'react';
import {Linking} from 'react-native';
import type {AppProps} from './App';
import CONST from './CONST';
import {signInAfterTransitionFromOldDot} from './libs/actions/Session';
import Navigation, {navigationRef} from './libs/Navigation/Navigation';
import type {Route} from './ROUTES';
import ROUTES from './ROUTES';
import SCREENS from './SCREENS';
import SplashScreenStateContext from './SplashScreenStateContext';

function handleHybridUrlNavigation(url: Route) {
const parsedUrl = Navigation.parseHybridAppUrl(url);

Navigation.isNavigationReady().then(() => {
if (parsedUrl.startsWith(`/${ROUTES.SHARE_ROOT}`)) {
const focusRoute = findFocusedRoute(navigationRef.getRootState());
if (focusRoute?.name === SCREENS.SHARE.SHARE_DETAILS || focusRoute?.name === SCREENS.SHARE.SUBMIT_DETAILS) {
Navigation.goBack(ROUTES.SHARE_ROOT);
return;
}
}
Navigation.navigate(parsedUrl);
});
}

function HybridAppSignIn({url, hybridAppSettings}: AppProps) {
const [signInHandled, setSignInHandled] = useState(false);
const {setSplashScreenState} = useContext(SplashScreenStateContext);

useEffect(() => {
Linking.addEventListener('url', (state) => {
handleHybridUrlNavigation(state.url as Route);
});
}, []);
Comment on lines +32 to +36
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we remove the listener when the components unmounts?


if (!url || !hybridAppSettings || signInHandled) {
return null;
}

signInAfterTransitionFromOldDot(hybridAppSettings).then(() => {
handleHybridUrlNavigation(url);
setSplashScreenState(CONST.BOOT_SPLASH_STATE.READY_TO_BE_HIDDEN);
setSignInHandled(true);
});

return null;
}

HybridAppSignIn.displayName = 'HybridAppSignIn';

export default HybridAppSignIn;
37 changes: 4 additions & 33 deletions src/components/InitialURLContextProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
import {findFocusedRoute} from '@react-navigation/native';
import React, {createContext, useEffect, useMemo, useState} from 'react';
import type {ReactNode} from 'react';
import {Linking} from 'react-native';
import {signInAfterTransitionFromOldDot} from '@libs/actions/Session';
import Navigation, {navigationRef} from '@navigation/Navigation';
import type {AppProps} from '@src/App';
import CONST from '@src/CONST';
import type {Route} from '@src/ROUTES';
import ROUTES from '@src/ROUTES';
import SCREENS from '@src/SCREENS';
import {useSplashScreenStateContext} from '@src/SplashScreenStateContext';

type InitialUrlContextType = {
initialURL: Route | undefined;
Expand All @@ -27,40 +20,18 @@ type InitialURLContextProviderProps = AppProps & {
children: ReactNode;
};

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

useEffect(() => {
if (url && hybridAppSettings) {
signInAfterTransitionFromOldDot(hybridAppSettings).then(() => {
setInitialURL(url);

const parsedUrl = Navigation.parseHybridAppUrl(url);

Navigation.isNavigationReady().then(() => {
if (parsedUrl.startsWith(`/${ROUTES.SHARE_ROOT}`)) {
const focusRoute = findFocusedRoute(navigationRef.getRootState());
if (focusRoute?.name === SCREENS.SHARE.SHARE_DETAILS || focusRoute?.name === SCREENS.SHARE.SUBMIT_DETAILS) {
Navigation.goBack(ROUTES.SHARE_ROOT);
return;
}
}
Navigation.navigate(parsedUrl);
});

if (splashScreenState === CONST.BOOT_SPLASH_STATE.HIDDEN) {
return;
}
setSplashScreenState(CONST.BOOT_SPLASH_STATE.READY_TO_BE_HIDDEN);
});
if (url) {
setInitialURL(url);
return;
}
Linking.getInitialURL().then((initURL) => {
setInitialURL(initURL as Route);
});
// eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps
}, [url, hybridAppSettings, timestamp]);
}, [url]);

const initialUrlContext = useMemo(
() => ({
Expand Down
Loading