Skip to content

Commit

Permalink
Merge pull request #46885 from software-mansion-labs/adamgrzybowski/f…
Browse files Browse the repository at this point in the history
…ix-getAlreadyOpenedSettingsScreen

[Ideal nav] Fix getAlreadyOpenedSettingsScreen
  • Loading branch information
luacmartins authored Aug 6, 2024
2 parents c6fb0cc + 8cf9471 commit 7975157
Showing 1 changed file with 4 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,43 +31,18 @@ const getTopMostReportIDFromRHP = (state: State): string => {
return '';
};

// Check if the given route has a policyID equal to the id provided in the function params
function hasRouteMatchingPolicyID(route: NavigationPartialRoute<CentralPaneName>, policyID?: string) {
if (!route?.params) {
return false;
}

const params = `params` in route.params ? (route.params.params as Record<string, string | undefined>) : undefined;

// If params are not defined, then we need to check if the policyID exists
if (!params) {
return !policyID;
}

return 'policyID' in params && params.policyID === policyID;
}

// Get already opened settings screen within the policy
function getAlreadyOpenedSettingsScreen(rootState?: State, policyID?: string): keyof AuthScreensParamList | undefined {
function getAlreadyOpenedSettingsScreen(rootState?: State): keyof AuthScreensParamList | undefined {
if (!rootState) {
return undefined;
}

// If one of the screen from TAB_TO_CENTRAL_PANE_MAPPING[SCREENS.SETTINGS.ROOT] is now in the navigation state, we can decide which screen we should display.
// A screen from the navigation state can be pushed to the navigation state again only if it has a matching policyID with the currently selected workspace.
// Otherwise, when we switch the workspace, we want to display the initial screen in the settings tab.
const alreadyOpenedSettingsTab = rootState.routes
.filter((item) => item.params && 'screen' in item.params && TAB_TO_CENTRAL_PANE_MAPPING[SCREENS.SETTINGS.ROOT].includes(item.name as CentralPaneName))
.at(-1);

if (!hasRouteMatchingPolicyID(alreadyOpenedSettingsTab as NavigationPartialRoute<CentralPaneName>, policyID)) {
return undefined;
}

const settingsScreen =
alreadyOpenedSettingsTab?.params && 'screen' in alreadyOpenedSettingsTab.params ? (alreadyOpenedSettingsTab?.params?.screen as keyof AuthScreensParamList) : undefined;
const alreadyOpenedSettingsScreen = rootState.routes.filter((item) => TAB_TO_CENTRAL_PANE_MAPPING[SCREENS.SETTINGS.ROOT].includes(item.name as CentralPaneName)).at(-1);

return settingsScreen;
return alreadyOpenedSettingsScreen?.name as keyof AuthScreensParamList;
}

// Get matching central pane route for bottom tab navigator. e.g HOME -> REPORT
Expand All @@ -82,8 +57,7 @@ function getMatchingCentralPaneRouteForState(state: State<RootStackParamList>, r

if (topmostBottomTabRoute.name === SCREENS.SETTINGS.ROOT) {
// When we go back to the settings tab without switching the workspace id, we want to return to the previously opened screen
const policyID = topmostBottomTabRoute?.params && 'policyID' in topmostBottomTabRoute.params ? (topmostBottomTabRoute.params.policyID as string) : undefined;
const screen = getAlreadyOpenedSettingsScreen(rootState, policyID) ?? centralPaneName;
const screen = getAlreadyOpenedSettingsScreen(rootState) ?? centralPaneName;
return {name: screen as CentralPaneName, params: topmostBottomTabRoute.params};
}

Expand Down

0 comments on commit 7975157

Please sign in to comment.