Skip to content

Commit

Permalink
Merge pull request #47689 from callstack-internal/hur/fix-46118
Browse files Browse the repository at this point in the history
  • Loading branch information
blimpich authored Aug 26, 2024
2 parents 6d98f9c + a5bf00d commit 1862abc
Showing 1 changed file with 4 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ type SearchPageProps = StackScreenProps<BottomTabNavigatorParamList, typeof SCRE
function setupCustomAndroidBackHandler() {
const onBackPress = () => {
const rootState = navigationRef.getRootState();

const bottomTabRoute = rootState.routes.find((route) => route.name === NAVIGATORS.BOTTOM_TAB_NAVIGATOR);
const bottomTabRoute = rootState?.routes?.find((route) => route.name === NAVIGATORS.BOTTOM_TAB_NAVIGATOR);
const bottomTabRoutes = bottomTabRoute?.state?.routes;
const focusedRoute = findFocusedRoute(rootState);

Expand All @@ -23,7 +22,7 @@ function setupCustomAndroidBackHandler() {
return false;
}

const isLastScreenOnStack = bottomTabRoutes.length === 1 && rootState.routes.length === 1;
const isLastScreenOnStack = bottomTabRoutes.length === 1 && rootState?.routes?.length === 1;

if (NativeModules.HybridAppModule && isLastScreenOnStack) {
NativeModules.HybridAppModule.exitApp();
Expand All @@ -35,7 +34,7 @@ function setupCustomAndroidBackHandler() {
navigationRef.dispatch({...StackActions.pop(), target: bottomTabRoute?.state?.key});
navigationRef.dispatch({...StackActions.pop()});

const centralPaneRouteAfterPop = getTopmostCentralPaneRoute({routes: [rootState.routes.at(-2)]} as State<RootStackParamList>);
const centralPaneRouteAfterPop = getTopmostCentralPaneRoute({routes: [rootState?.routes?.at(-2)]} as State<RootStackParamList>);
const bottomTabRouteAfterPop = bottomTabRoutes.at(-2);

// It's possible that central pane search is desynchronized with the bottom tab search.
Expand All @@ -57,7 +56,7 @@ function setupCustomAndroidBackHandler() {
// It's possible that central pane search is desynchronized with the bottom tab search.
// e.g. opening a tab different than search will wipe out central pane screens.
// In that case we have to push the proper one.
if (bottomTabRoutes && bottomTabRoutes?.length >= 2 && bottomTabRoutes[bottomTabRoutes.length - 2].name === SCREENS.SEARCH.BOTTOM_TAB && rootState.routes.length === 1) {
if (bottomTabRoutes && bottomTabRoutes?.length >= 2 && bottomTabRoutes[bottomTabRoutes.length - 2].name === SCREENS.SEARCH.BOTTOM_TAB && rootState?.routes?.length === 1) {
const {policyID, ...restParams} = bottomTabRoutes[bottomTabRoutes.length - 2].params as SearchPageProps['route']['params'];
navigationRef.dispatch({...StackActions.push(SCREENS.SEARCH.CENTRAL_PANE, {...restParams, policyIDs: policyID})});
navigationRef.dispatch({...StackActions.pop(), target: bottomTabRoute?.state?.key});
Expand Down

0 comments on commit 1862abc

Please sign in to comment.