Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Ideal nav] Fix getAlreadyOpenedSettingsScreen #46885

Merged
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
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
Loading