Skip to content

Commit

Permalink
getTopmostReportActionID
Browse files Browse the repository at this point in the history
  • Loading branch information
perunt committed Sep 25, 2023
1 parent ea94621 commit 0c164d1
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/libs/Navigation/Navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import linkingConfig from './linkingConfig';
import navigationRef from './navigationRef';
import NAVIGATORS from '../../NAVIGATORS';
import originalGetTopmostReportId from './getTopmostReportId';
import originalGetTopmostReportActionId from './getTopmostReportActionID';
import getStateFromPath from './getStateFromPath';
import SCREENS from '../../SCREENS';
import CONST from '../../CONST';
Expand Down Expand Up @@ -46,6 +47,9 @@ function canNavigate(methodName, params = {}) {
// Re-exporting the getTopmostReportId here to fill in default value for state. The getTopmostReportId isn't defined in this file to avoid cyclic dependencies.
const getTopmostReportId = (state = navigationRef.getState()) => originalGetTopmostReportId(state);

// Re-exporting the getTopmostReportActionID here to fill in default value for state. The getTopmostReportActionID isn't defined in this file to avoid cyclic dependencies.
const getTopmostReportActionId = (state = navigationRef.getState()) => originalGetTopmostReportActionId(state);

/**
* Method for finding on which index in stack we are.
* @param {Object} route
Expand Down Expand Up @@ -268,6 +272,7 @@ export default {
setIsNavigationReady,
getTopmostReportId,
getRouteNameFromStateEvent,
getTopmostReportActionId,
};

export {navigationRef};
42 changes: 42 additions & 0 deletions src/libs/Navigation/getTopmostReportActionID.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import lodashFindLast from 'lodash/findLast';
import lodashGet from 'lodash/get';

// This function is in a separate file than Navigation.js to avoid cyclic dependency.

/**
* Find the last visited report screen in the navigation state and get the linked reportActionID of it.
*
* @param {Object} state - The react-navigation state
* @returns {String | undefined} - It's possible that there is no report screen
*/
function getTopmostReportActionID(state) {
if (!state) {
return;
}
const topmostCentralPane = lodashFindLast(state.routes, (route) => route.name === 'CentralPaneNavigator');

if (!topmostCentralPane) {
return;
}

const directReportActionIDParam = lodashGet(topmostCentralPane, 'params.params.reportActionID');

if (!topmostCentralPane.state && !directReportActionIDParam) {
return;
}

if (directReportActionIDParam) {
return directReportActionIDParam;
}

const topmostReport = lodashFindLast(topmostCentralPane.state.routes, (route) => route.name === 'Report');
if (!topmostReport) {
return;
}

const topmostReportActionID = lodashGet(topmostReport, 'params.reportActionID');

return topmostReportActionID;
}

export default getTopmostReportActionID;
2 changes: 1 addition & 1 deletion src/pages/home/sidebar/SidebarLinks.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class SidebarLinks extends React.PureComponent {
// since getTopmostReportId always returns on other devices
if (
this.props.isCreateMenuOpen ||
(!this.props.isSmallScreenWidth && this.props.isActiveReport(option.reportID)) ||
(!this.props.isSmallScreenWidth && this.props.isActiveReport(option.reportID) && !Navigation.getTopmostReportActionId()) ||
(this.props.isSmallScreenWidth && Navigation.getTopmostReportId())
) {
return;
Expand Down

0 comments on commit 0c164d1

Please sign in to comment.