diff --git a/src/libs/Navigation/AppNavigator/MainDrawerNavigator.js b/src/libs/Navigation/AppNavigator/MainDrawerNavigator.js
index 7dd353a5927e2..f53b61989864b 100644
--- a/src/libs/Navigation/AppNavigator/MainDrawerNavigator.js
+++ b/src/libs/Navigation/AppNavigator/MainDrawerNavigator.js
@@ -1,16 +1,13 @@
import React from 'react';
import PropTypes from 'prop-types';
import _ from 'underscore';
+import lodashGet from 'lodash/get';
import {createDrawerNavigator} from '@react-navigation/drawer';
import {withOnyx} from 'react-native-onyx';
import withWindowDimensions, {windowDimensionsPropTypes} from '../../../components/withWindowDimensions';
import FullScreenLoadingIndicator from '../../../components/FullscreenLoadingIndicator';
-import {getLastAccessedReport} from '../../reportUtils';
-import styles, {
- getNavigationDrawerType,
- getNavigationDrawerStyle,
-} from '../../../styles/styles';
+import styles, {getNavigationDrawerStyle, getNavigationDrawerType} from '../../../styles/styles';
import ONYXKEYS from '../../../ONYXKEYS';
import compose from '../../compose';
import SCREENS from '../../../SCREENS';
@@ -18,6 +15,7 @@ import SCREENS from '../../../SCREENS';
// Screens
import SidebarScreen from '../../../pages/home/sidebar/SidebarScreen';
import ReportScreen from '../../../pages/home/ReportScreen';
+import {findLastAccessedReport} from '../../reportUtils';
const propTypes = {
// Available reports that would be displayed in this navigator
@@ -34,8 +32,17 @@ const defaultProps = {
const Drawer = createDrawerNavigator();
-// Decorated to always returning the result of the first call - keeps Screen initialParams from changing
-const getInitialReport = _.once(getLastAccessedReport);
+/**
+ * We are decorating findLastAccessedReport so that it caches the first result once the reports load.
+ * This will ensure the initialParams are only set once for the ReportScreen.
+ */
+const getInitialReportScreenParams = _.once((reports) => {
+ const last = findLastAccessedReport(reports);
+
+ // Fallback to empty if for some reason reportID cannot be derived - prevents the app from crashing
+ const reportID = lodashGet(last, 'reportID', '');
+ return {reportID};
+});
const MainDrawerNavigator = (props) => {
// When there are no reports there's no point to render the empty navigator
@@ -43,8 +50,6 @@ const MainDrawerNavigator = (props) => {
return ;
}
- const initialReportID = getInitialReport(props.reports).reportID;
-
/* After the app initializes and reports are available the home navigation is mounted
* This way routing information is updated (if needed) based on the initial report ID resolved.
* This is usually needed after login/create account and re-launches */
@@ -67,7 +72,7 @@ const MainDrawerNavigator = (props) => {
);
@@ -85,3 +90,4 @@ export default compose(
},
}),
)(MainDrawerNavigator);
+export {getInitialReportScreenParams};
diff --git a/src/libs/reportUtils.js b/src/libs/reportUtils.js
index 668aca5c41b77..b46e3acfe60b4 100644
--- a/src/libs/reportUtils.js
+++ b/src/libs/reportUtils.js
@@ -24,12 +24,13 @@ function isReportMessageAttachment(reportMessageText) {
/**
* Given a collection of reports returns the most recently accessed one
*
- * @param {Record|Array<{lastVisitedTimestamp}>} reports
+ * @param {Record|Array<{lastVisitedTimestamp, reportID}>} reports
* @returns {Object}
*/
-function getLastAccessedReport(reports) {
+function findLastAccessedReport(reports) {
return _.chain(reports)
.toArray()
+ .filter(report => report && report.reportID)
.sortBy('lastVisitedTimestamp')
.last()
.value();
@@ -38,5 +39,5 @@ function getLastAccessedReport(reports) {
export {
getReportParticipantsTitle,
isReportMessageAttachment,
- getLastAccessedReport,
+ findLastAccessedReport,
};