Skip to content

Commit

Permalink
Refactor getTopmostReportParam to getTopmostReportParams
Browse files Browse the repository at this point in the history
  • Loading branch information
WojtekBoman committed Nov 29, 2024
1 parent d795508 commit eda65ce
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/libs/Navigation/Navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
getMinimalAction,
getPolicyIDFromState,
getStateFromPath,
getTopmostReportParam,
getTopmostReportParams,
isReportOpenInRHP,
isSplitNavigatorName,
linkTo,
Expand Down Expand Up @@ -70,10 +70,10 @@ function canNavigate(methodName: string, params: Record<string, unknown> = {}):
}

// Extracts from the topmost report its id.
const getTopmostReportId = (state = navigationRef.getState()) => getTopmostReportParam(state, 'reportID');
const getTopmostReportId = (state = navigationRef.getState()) => getTopmostReportParams(state)?.reportID;

// Extracts from the topmost report its action id.
const getTopmostReportActionId = (state = navigationRef.getState()) => getTopmostReportParam(state, 'reportActionID');
const getTopmostReportActionId = (state = navigationRef.getState()) => getTopmostReportParams(state)?.reportActionID;

// Re-exporting the closeRHPFlow here to fill in default value for navigationRef. The closeRHPFlow isn't defined in this file to avoid cyclic dependencies.
const closeRHPFlow = (ref = navigationRef) => originalCloseRHPFlow(ref);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import type {NavigationState, PartialState} from '@react-navigation/native';
import type {RootStackParamList} from '@libs/Navigation/types';
import type {ReportsSplitNavigatorParamList, RootStackParamList} from '@libs/Navigation/types';
import NAVIGATORS from '@src/NAVIGATORS';
import SCREENS from '@src/SCREENS';

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

/**
* Find the last visited report screen in the navigation state and get its specific param (id or action id).
* Find the last visited report screen in the navigation state and get its params.
*
* @param state - The react-navigation state
* @param reportParam - param to get from the report route params
* @returns - It's possible that there is no report screen
*/

type State = NavigationState | NavigationState<RootStackParamList> | PartialState<NavigationState>;
type ReportParam = 'reportID' | 'reportActionID';

function getTopmostReportParam(state: State, reportParam: ReportParam): string | undefined {
function getTopmostReportParams(state: State): ReportsSplitNavigatorParamList[typeof SCREENS.REPORT] | undefined {
if (!state) {
return;
}
Expand All @@ -33,9 +31,7 @@ function getTopmostReportParam(state: State, reportParam: ReportParam): string |
return;
}

const topmostReportParams = topmostReport?.params as Record<string, string>;

return topmostReportParams?.[reportParam];
return topmostReport?.params as ReportsSplitNavigatorParamList[typeof SCREENS.REPORT];
}

export default getTopmostReportParam;
export default getTopmostReportParams;
2 changes: 1 addition & 1 deletion src/libs/Navigation/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export {default as getOnboardingAdaptedState} from './getOnboardingAdaptedState'
export {default as getParamsFromRoute} from './getParamsFromRoute';
export {default as getPolicyIDFromState} from './getPolicyIDFromState';
export {default as getStateFromPath} from './getStateFromPath';
export {default as getTopmostReportParam} from './getTopmostReportParam';
export {default as getTopmostReportParams} from './getTopmostReportParams';
export {default as getTopmostRouteName} from './getTopmostRouteName';
export {default as isReportOpenInRHP} from './isReportOpenInRHP';
export {default as isSearchTopmostFullScreenRoute} from './isSearchTopmostFullScreenRoute';
Expand Down

0 comments on commit eda65ce

Please sign in to comment.