Skip to content

Commit

Permalink
Merge pull request #129 from software-mansion-labs/poc/fix-go-up
Browse files Browse the repository at this point in the history
Adjust goUp to handle navigating back when targetState is rootState
  • Loading branch information
adamgrzybowski authored Oct 31, 2024
2 parents 0ad8a87 + 8e44d87 commit ed0ebca
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/libs/Navigation/Navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ const defaultGoUpOptions: Required<GoUpOptions> = {
compareParams: true,
};

function isRootNavigatorState(state: State): state is State<RootStackParamList> {
return state.key === navigationRef.current?.getRootState().key;
}

function goUp(fallbackRoute: Route, options?: GoUpOptions) {
const compareParams = options?.compareParams ?? defaultGoUpOptions.compareParams;

Expand Down Expand Up @@ -230,7 +234,10 @@ function goUp(fallbackRoute: Route, options?: GoUpOptions) {

const indexOfFallbackRoute = targetState.routes.findLastIndex((route) => doesRouteMatchToMinimalActionPayload(route, minimalAction, compareParams));

if (indexOfFallbackRoute === -1) {
const distanceToPop = targetState.routes.length - indexOfFallbackRoute - 1;

// If we need to pop more than one route from rootState, we replace the current route to not lose visited routes from the navigation state
if (indexOfFallbackRoute === -1 || (isRootNavigatorState(targetState) && distanceToPop > 1)) {
const replaceAction = {...minimalAction, type: 'REPLACE'} as NavigationAction;
navigationRef.current.dispatch(replaceAction);
return;
Expand All @@ -243,7 +250,6 @@ function goUp(fallbackRoute: Route, options?: GoUpOptions) {
return;
}

const distanceToPop = targetState.routes.length - indexOfFallbackRoute - 1;
navigationRef.current.dispatch({...StackActions.pop(distanceToPop), target: targetState.key});
}

Expand Down
5 changes: 4 additions & 1 deletion src/pages/home/sidebar/BottomTabAvatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ function BottomTabAvatar({isCreateMenuOpen = false, isSelected = false}: BottomT
}

if (route.name === SCREENS.WORKSPACE.INITIAL) {
Navigation.goUp(ROUTES.SETTINGS_WORKSPACES);
Navigation.goUp(ROUTES.SETTINGS);
if (shouldUseNarrowLayout) {
Navigation.navigate(ROUTES.SETTINGS, CONST.NAVIGATION.ACTION_TYPE.REPLACE);
}
return;
}

Expand Down

0 comments on commit ed0ebca

Please sign in to comment.