Skip to content

Commit

Permalink
[native] Blocking navigation in edit mode in the TabRouter
Browse files Browse the repository at this point in the history
Summary: When we are in edit mode and the user made changes, we prevent him in this diff from changing tabs.

Test Plan:
On iOS:
- Navigate between threads - check if everything works.
- Navigate between tabs - check if everything works.
- Enter edit mode and make changes to the message,
- Change tab - should confirm with alert.

- Do the same in the sidebar (enter edit mode and check if the alert displays).

Reviewers: michal, inka, ashoat, tomek

Reviewed By: ashoat

Differential Revision: https://phab.comm.dev/D8304
  • Loading branch information
kosmydel committed Jul 13, 2023
1 parent 38d235d commit e47c698
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
11 changes: 9 additions & 2 deletions native/navigation/nav-selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ const drawerSwipeEnabledSelector: (context: ?NavContextType) => boolean =
},
);

function getChatNavState(
function getTabNavState(
navigationState: ?PossiblyStaleNavigationState,
): ?PossiblyStaleNavigationState {
if (!navigationState) {
Expand All @@ -335,6 +335,12 @@ function getChatNavState(
}

const tabState = getStateFromNavigatorRoute(firstCommunityDrawerSubroute);
return tabState;
}

function getChatNavStateFromTabNavState(
tabState: ?PossiblyStaleNavigationState,
): ?PossiblyStaleNavigationState {
if (!tabState) {
return null;
}
Expand Down Expand Up @@ -400,5 +406,6 @@ export {
drawerSwipeEnabledSelector,
useCurrentLeafRouteName,
getRemoveEditMode,
getChatNavState,
getTabNavState,
getChatNavStateFromTabNavState,
};
9 changes: 7 additions & 2 deletions native/navigation/overlay-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ import {
clearOverlayModalsActionType,
setRouteParamsActionType,
} from './action-types.js';
import { getRemoveEditMode, getChatNavState } from './nav-selectors.js';
import {
getChatNavStateFromTabNavState,
getRemoveEditMode,
getTabNavState,
} from './nav-selectors.js';
import { removeScreensFromStack } from './navigation-utils.js';

type ClearOverlayModalsAction = {
Expand Down Expand Up @@ -57,7 +61,8 @@ function OverlayRouter(
action: OverlayRouterNavigationAction,
options: RouterConfigOptions,
) => {
const chatNavState = getChatNavState(lastState);
const tabNavState = getTabNavState(lastState);
const chatNavState = getChatNavStateFromTabNavState(tabNavState);
const removeEditMode = getRemoveEditMode(chatNavState);
if (action.type === clearOverlayModalsActionType) {
const { keys } = action.payload;
Expand Down
10 changes: 10 additions & 0 deletions native/navigation/tab-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import type {
} from '@react-navigation/native';
import { TabRouter } from '@react-navigation/native';

import {
getChatNavStateFromTabNavState,
getRemoveEditMode,
} from './nav-selectors.js';

type TabRouterNavigationAction = empty;

export type TabRouterExtraNavigationHelpers = {};
Expand All @@ -24,6 +29,11 @@ function CustomTabRouter(
action: TabRouterNavigationAction,
options: RouterConfigOptions,
) => {
const chatNavState = getChatNavStateFromTabNavState(lastState);
const removeEditMode = getRemoveEditMode(chatNavState);
if (removeEditMode && removeEditMode(action) === 'ignore_action') {
return lastState;
}
return baseGetStateForAction(lastState, action, options);
},
};
Expand Down

0 comments on commit e47c698

Please sign in to comment.