Skip to content
Merged
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
26 changes: 16 additions & 10 deletions src/libs/Navigation/AppNavigator/MainDrawerNavigator.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
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';

// 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
Expand All @@ -34,17 +32,24 @@ 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
if (_.size(props.reports) === 0) {
return <FullScreenLoadingIndicator visible />;
}

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 */
Expand All @@ -67,7 +72,7 @@ const MainDrawerNavigator = (props) => {
<Drawer.Screen
name={SCREENS.REPORT}
component={ReportScreen}
initialParams={{reportID: initialReportID.toString()}}
initialParams={getInitialReportScreenParams(props.reports)}
/>
</Drawer.Navigator>
);
Expand All @@ -85,3 +90,4 @@ export default compose(
},
}),
)(MainDrawerNavigator);
export {getInitialReportScreenParams};
Copy link
Contributor

Choose a reason for hiding this comment

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

Old PR now, but any idea why this export was added? Doesn't seem to be used anywhere AFAICT

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we were using it somewhere at some point. I don't really know tbh.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Originally it was part of reportUtils and imported to MainDrawerNavigator: bc63c49#diff-82f2652821babb48b600a87486fa1191db7b3cc300845573a5757676e13cfc26L44

I decided it's tied to the MainDrawerNavigator (see _.once decorator and description) and moved it for better context
I guess I used automatic refactoring, which preserves exporting the file after it's moved

7 changes: 4 additions & 3 deletions src/libs/reportUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ function isReportMessageAttachment(reportMessageText) {
/**
* Given a collection of reports returns the most recently accessed one
*
* @param {Record<String, {lastVisitedTimestamp}>|Array<{lastVisitedTimestamp}>} reports
* @param {Record<String, {lastVisitedTimestamp, reportID}>|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();
Expand All @@ -38,5 +39,5 @@ function getLastAccessedReport(reports) {
export {
getReportParticipantsTitle,
isReportMessageAttachment,
getLastAccessedReport,
findLastAccessedReport,
};