Skip to content

Commit

Permalink
Handle navigating back to sidebar screen
Browse files Browse the repository at this point in the history
  • Loading branch information
WojtekBoman committed Sep 30, 2024
1 parent 1e51511 commit 49210d1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function adaptStateIfNecessary({state, options: {sidebarScreen, defaultCentralSc
const workspaceCentralPane = state.routes.at(-1);

// There should always be sidebarScreen screen in the state to make sure go back works properly if we deeplinkg to a subpage of settings.
if (!isAtLeastOneInState(state, sidebarScreen)) {
if (!isAtLeastOneInState(state, sidebarScreen) && !isNarrowLayout) {
// @ts-expect-error Updating read only property
// noinspection JSConstantReassignment
state.stale = true; // eslint-disable-line
Expand Down
87 changes: 31 additions & 56 deletions src/libs/Navigation/Navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,21 @@ import navigationRef from './navigationRef';
import linkTo from './newLinkTo';
import getMinimalAction from './newLinkTo/getMinimalAction';
import setNavigationActionToMicrotaskQueue from './setNavigationActionToMicrotaskQueue';
import type {NavigationStateRoute, RootStackParamList, StackNavigationAction, State, StateOrRoute} from './types';
import type {NavigationStateRoute, RootStackParamList, SplitNavigatorLHNScreen, SplitNavigatorName, SplitNavigatorParamListType, StackNavigationAction, State, StateOrRoute} from './types';

Check failure on line 30 in src/libs/Navigation/Navigation.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

'SplitNavigatorParamListType' is defined but never used

const SPLIT_NAVIGATOR_TO_SIDEBAR_MAP: Record<SplitNavigatorName, SplitNavigatorLHNScreen> = {
[NAVIGATORS.REPORTS_SPLIT_NAVIGATOR]: SCREENS.HOME,
[NAVIGATORS.SETTINGS_SPLIT_NAVIGATOR]: SCREENS.SETTINGS.ROOT,
[NAVIGATORS.WORKSPACE_SPLIT_NAVIGATOR]: SCREENS.WORKSPACE.INITIAL,
};

function getSidebarScreenParams(splitNavigatorRoute: NavigationStateRoute) {
if (splitNavigatorRoute.name === NAVIGATORS.WORKSPACE_SPLIT_NAVIGATOR) {
return splitNavigatorRoute.state?.routes?.at(0)?.params;
}

return undefined;
}

let resolveNavigationIsReadyPromise: () => void;
const navigationIsReadyPromise = new Promise<void>((resolve) => {
Expand Down Expand Up @@ -273,66 +287,27 @@ function goBack(fallbackRoute?: Route, shouldEnforceFallback = false, shouldPopT
return;
}

const rootState = navigationRef.current?.getRootState();
const lastRoute = rootState?.routes.at(-1);

if (lastRoute?.name.endsWith('SplitNavigator') && lastRoute?.state?.routes?.length === 1) {
const name = SPLIT_NAVIGATOR_TO_SIDEBAR_MAP[lastRoute?.name as SplitNavigatorName];
const params = getSidebarScreenParams(lastRoute);
navigationRef.dispatch({
type: 'REPLACE',
payload: {
name,
params,
},
});
return;
}

if (!navigationRef.current?.canGoBack()) {
Log.hmmm('[Navigation] Unable to go back');
return;
}

// const isFirstRouteInNavigator = !getActiveRouteIndex(navigationRef.current.getState());
// if (isFirstRouteInNavigator) {
// const rootState = navigationRef.getRootState();
// const lastRoute = rootState.routes.at(-1);
// // If the user comes from a different flow (there is more than one route in ModalNavigator) we should go back to the previous flow on UP button press instead of using the fallbackRoute.
// if ((lastRoute?.name === NAVIGATORS.RIGHT_MODAL_NAVIGATOR || lastRoute?.name === NAVIGATORS.LEFT_MODAL_NAVIGATOR) && (lastRoute.state?.index ?? 0) > 0) {
// navigationRef.current.goBack();
// return;
// }
// }

// if (shouldEnforceFallback || (isFirstRouteInNavigator && fallbackRoute)) {
// navigate(fallbackRoute, 'REPLACE');
// return;
// }

// const isCentralPaneFocused = isCentralPaneName(findFocusedRoute(navigationRef.current.getState())?.name);
// const distanceFromPathInRootNavigator = getDistanceFromPathInRootNavigator(navigationRef.getRootState(), fallbackRoute ?? '');

// if (isCentralPaneFocused && fallbackRoute) {
// // Allow CentralPane to use UP with fallback route if the path is not found in root navigator.
// if (distanceFromPathInRootNavigator === -1) {
// navigate(fallbackRoute, 'REPLACE');
// return;
// }

// // Add possibility to go back more than one screen in root navigator if that screen is on the stack.
// if (distanceFromPathInRootNavigator > 0) {
// navigationRef.current.dispatch(StackActions.pop(distanceFromPathInRootNavigator));
// return;
// }
// }

// // If the central pane is focused, it's possible that we navigated from other central pane with different matching bottom tab.
// if (isCentralPaneFocused) {
// const rootState = navigationRef.getRootState();
// const stateAfterPop = {routes: rootState.routes.slice(0, -1)} as State<RootStackParamList>;
// const topmostCentralPaneRouteAfterPop = getTopmostCentralPaneRoute(stateAfterPop);

// const topmostBottomTabRoute = getTopmostBottomTabRoute(rootState as State<RootStackParamList>);
// const matchingBottomTabRoute = getMatchingBottomTabRouteForState(stateAfterPop);

// // If the central pane is defined after the pop action, we need to check if it's synced with the bottom tab screen.
// // If not, we need to pop to the bottom tab screen/screens to sync it with the new central pane.
// if (topmostCentralPaneRouteAfterPop && topmostBottomTabRoute?.name !== matchingBottomTabRoute.name) {
// const bottomTabNavigator = rootState.routes.find((item: NavigationStateRoute) => item.name === NAVIGATORS.BOTTOM_TAB_NAVIGATOR)?.state;

// if (bottomTabNavigator && bottomTabNavigator.index) {
// const matchingIndex = bottomTabNavigator.routes.findLastIndex((item) => item.name === matchingBottomTabRoute.name);
// const indexToPop = matchingIndex !== -1 ? bottomTabNavigator.index - matchingIndex : undefined;
// navigationRef.current.dispatch({...StackActions.pop(indexToPop), target: bottomTabNavigator?.key});
// }
// }
// }

navigationRef.current.goBack();
}

Expand Down
3 changes: 3 additions & 0 deletions src/libs/Navigation/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ type SplitNavigatorParamListType = {
[NAVIGATORS.WORKSPACE_SPLIT_NAVIGATOR]: WorkspaceSplitNavigatorParamList;
};

type SplitNavigatorName = keyof SplitNavigatorParamListType;

type SplitNavigatorByLHN<T extends SplitNavigatorLHNScreen> = (typeof LHN_TO_SPLIT_NAVIGATOR_NAME)[T];

type CentralPaneScreensParamList = {
Expand Down Expand Up @@ -1645,4 +1647,5 @@ export type {
SplitNavigatorLHNScreen,
SplitNavigatorParamListType,
SplitNavigatorByLHN,
SplitNavigatorName,
};

0 comments on commit 49210d1

Please sign in to comment.