diff --git a/src/libs/Navigation/AppNavigator/AuthScreens.js b/src/libs/Navigation/AppNavigator/AuthScreens.js index 7e7bb2cf88f8..b214542cf7f3 100644 --- a/src/libs/Navigation/AppNavigator/AuthScreens.js +++ b/src/libs/Navigation/AppNavigator/AuthScreens.js @@ -81,11 +81,16 @@ const propTypes = { // Is the network currently offline or not isOffline: PropTypes.bool, }), + + // The initial report for the home screen + initialReportID: PropTypes.string, + ...windowDimensionsPropTypes, }; const defaultProps = { network: {isOffline: true}, + initialReportID: null, }; class AuthScreens extends React.Component { @@ -94,6 +99,8 @@ class AuthScreens extends React.Component { Timing.start(CONST.TIMING.HOMEPAGE_INITIAL_RENDER); Timing.start(CONST.TIMING.HOMEPAGE_REPORTS_LOADED); + + this.initialReportID = props.initialReportID; } componentDidMount() { @@ -134,11 +141,20 @@ class AuthScreens extends React.Component { }, ['meta'], true); } - shouldComponentUpdate(prevProps) { - if (prevProps.isSmallScreenWidth !== this.props.isSmallScreenWidth) { + shouldComponentUpdate(nextProps) { + if (nextProps.isSmallScreenWidth !== this.props.isSmallScreenWidth) { return true; } + // Skip when `this.initialReportID` is already assigned. We no longer want to update it + if (!this.initialReportID) { + // Either we have a reportID or fetchAllReports resolved with no reports. Otherwise keep waiting + if (nextProps.initialReportID || nextProps.initialReportID === '') { + this.initialReportID = nextProps.initialReportID; + return true; + } + } + return false; } @@ -150,6 +166,11 @@ class AuthScreens extends React.Component { } render() { + // Wait to resolve initial Home route params. + if (!this.initialReportID) { + return null; + } + const modalScreenOptions = { headerShown: false, cardStyle: getNavigationModalCardStyle(this.props.isSmallScreenWidth), @@ -172,6 +193,12 @@ class AuthScreens extends React.Component { headerShown: false, title: 'Expensify.cash', }} + initialParams={{ + screen: 'Report', + params: { + reportID: this.initialReportID, + }, + }} component={MainDrawerNavigator} /> { - // On web we should be able to parse this. It will be null on native for now until deep links are - // hooked up - const path = getPathName(initialUrl); - let initialState = getStateFromPath(path, linkingConfig.config); - setCurrentURL(path); - - // If we are landing on something other than the report screen or site root then we MUST set the - // initial route to the currently viewed report so there some history to navigate back from - if (path !== `/${ROUTES.HOME}` && !path.includes(`/${ROUTES.REPORT}`)) { - const homeRoute = { - name: 'Home', - }; - - if (this.props.currentlyViewedReportID) { - homeRoute.params = { - screen: 'Report', - params: { - reportID: this.props.currentlyViewedReportID, - }, - }; - } - - if (!initialState) { - initialState = { - routes: [], - }; - } - - initialState.routes = [ - homeRoute, - ...initialState.routes, - ]; - } + /** + * Intercept state changes and perform different logic + * @param {NavigationState} state + */ + parseAndStoreRoute(state) { + if (!state) { + return; + } - this.setState({loading: false, initialState}); - }); + const path = getPathFromState(state, linkingConfig.config); + setCurrentURL(path); } render() { - if (this.state.loading) { - return ( - - - - ); - } - - // If we are on web, desktop, or a widescreen width we will use our custom navigator to create the wider layout return ( { - if (!state) { - return; - } - - const path = getPathFromState(state, linkingConfig.config); - if (path.includes(ROUTES.REPORT)) { - const reportID = Number(_.last(path.split('/'))); - if (reportID && !_.isNaN(reportID)) { - updateCurrentlyViewedReportID(reportID); - } - } - - setCurrentURL(path); - }} + fallback={} + onStateChange={this.parseAndStoreRoute} ref={navigationRef} linking={linkingConfig} documentTitle={{ @@ -123,9 +50,4 @@ class NavigationRoot extends Component { } NavigationRoot.propTypes = propTypes; -NavigationRoot.defaultProps = defaultProps; -export default withOnyx({ - currentlyViewedReportID: { - key: ONYXKEYS.CURRENTLY_VIEWED_REPORTID, - }, -})(NavigationRoot); +export default NavigationRoot; diff --git a/src/libs/Navigation/linkingConfig.js b/src/libs/Navigation/linkingConfig.js index 0a38fda7ccc2..9ed0dc0ec42b 100644 --- a/src/libs/Navigation/linkingConfig.js +++ b/src/libs/Navigation/linkingConfig.js @@ -11,7 +11,7 @@ export default { config: { screens: { Home: { - path: '', + path: ROUTES.HOME, initialRouteName: 'Report', screens: { // Report route diff --git a/src/pages/home/ReportScreen.js b/src/pages/home/ReportScreen.js index 0438c2f6b910..ddbb72a9e659 100644 --- a/src/pages/home/ReportScreen.js +++ b/src/pages/home/ReportScreen.js @@ -7,6 +7,7 @@ import HeaderView from './HeaderView'; import Navigation from '../../libs/Navigation/Navigation'; import ROUTES from '../../ROUTES'; import FullScreenLoadingIndicator from '../../components/FullscreenLoadingIndicator'; +import {updateCurrentlyViewedReportID} from '../../libs/actions/Report'; const propTypes = { /* Navigation route context info provided by react navigation */ @@ -37,6 +38,7 @@ class ReportScreen extends React.Component { if (reportChanged) { this.prepareTransition(); + this.storeCurrentlyViewedReport(); } } @@ -73,6 +75,14 @@ class ReportScreen extends React.Component { this.loadingTimerId = setTimeout(() => this.setState({isLoading: false}), 300); } + /** + * Persists the currently viewed report id + */ + storeCurrentlyViewedReport() { + const reportID = this.getReportID(); + updateCurrentlyViewedReportID(reportID); + } + render() { return (