Skip to content

Commit

Permalink
Reconnect from last udpate id
Browse files Browse the repository at this point in the history
  • Loading branch information
jnowakow committed Nov 18, 2024
1 parent 2b38733 commit 5e30c9b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
24 changes: 19 additions & 5 deletions src/components/InitialURLContextProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import React, {createContext, useEffect, useMemo, useState} from 'react';
import type {ReactNode} from 'react';
import {Linking} from 'react-native';
import type {OnyxEntry} from 'react-native-onyx';
import {withOnyx} from 'react-native-onyx';
import {signInAfterTransitionFromOldDot} from '@libs/actions/Session';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Route} from '@src/ROUTES';
import {useSplashScreenStateContext} from '@src/SplashScreenStateContext';

Expand All @@ -17,21 +20,26 @@ const InitialURLContext = createContext<InitialUrlContextType>({
setInitialURL: () => {},
});

type InitialURLContextProviderProps = {
type InitialURLContextProviderPropsFromOnyx = {
/** In order to reconnect ND in hybrid app from right place */
initialLastUpdateIDAppliedToClient: OnyxEntry<number>;
};

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

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

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

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

const initialUrlContext = useMemo(
() => ({
Expand All @@ -55,5 +63,11 @@ function InitialURLContextProvider({children, url}: InitialURLContextProviderPro

InitialURLContextProvider.displayName = 'InitialURLContextProvider';

export default InitialURLContextProvider;
// this `withOnyx` is used intentionally because we want to delay rendering until the last update's id is available for the app
export default withOnyx<InitialURLContextProviderProps, InitialURLContextProviderPropsFromOnyx>({
initialLastUpdateIDAppliedToClient: {
key: ONYXKEYS.ONYX_UPDATES_LAST_UPDATE_ID_APPLIED_TO_CLIENT,
},
})(InitialURLContextProvider);

export {InitialURLContext};
4 changes: 2 additions & 2 deletions src/libs/actions/Session/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ function signUpUser() {
API.write(WRITE_COMMANDS.SIGN_UP_USER, params, {optimisticData, successData, failureData});
}

function signInAfterTransitionFromOldDot(transitionURL: string) {
function signInAfterTransitionFromOldDot(transitionURL: string, lastUpdateId?: number) {
const [route, queryParams] = transitionURL.split('?');

const {
Expand Down Expand Up @@ -533,7 +533,7 @@ function signInAfterTransitionFromOldDot(transitionURL: string) {
// We clear Onyx when this flag is set to true so we have to download all data
App.openApp();
} else {
App.reconnectApp();
App.reconnectApp(lastUpdateId);
}
})
.catch((error) => {
Expand Down

0 comments on commit 5e30c9b

Please sign in to comment.