Skip to content
Merged
Show file tree
Hide file tree
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
17 changes: 15 additions & 2 deletions src/libs/Navigation/Navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,25 @@ const closeRHPFlow = (ref = navigationRef) => originalCloseRHPFlow(ref);
/**
* Close the side panel on narrow layout when navigating to a different screen.
*/
function closeSidePanelOnNarrowScreen() {
function closeSidePanelOnNarrowScreen(route: Route) {
const isExtraLargeScreenWidth = Dimensions.get('window').width > variables.sidePanelResponsiveWidthBreakpoint;

if (!sidePanelNVP?.openNarrowScreen || isExtraLargeScreenWidth) {
return;
}

// Split "r/:reportID/attachment/add" by ":reportID" to get the prefix "r/" and suffix "/attachment/add"
const addAttachmentPrefix = ROUTES.REPORT_ADD_ATTACHMENT.route.split(':reportID').at(0) ?? '';
const addAttachmentSuffix = ROUTES.REPORT_ADD_ATTACHMENT.route.split(':reportID').at(1) ?? '';
const attachmentPreviewRoute = ROUTES.REPORT_ATTACHMENTS.route;
const isAddingAttachment = typeof route === 'string' && route.startsWith(addAttachmentPrefix) && route.includes(addAttachmentSuffix);
const isPreviewingAttachment = typeof route === 'string' && route.startsWith(attachmentPreviewRoute);
// If the user is navigating to an attachment route (previewing or adding), keep the side panel open
// so they still have access to the chat.
if (isAddingAttachment || isPreviewingAttachment) {
return;
}

SidePanelActions.closeSidePanel(true);
}

Expand Down Expand Up @@ -308,7 +321,7 @@ function navigate(route: Route, options?: LinkToOptions) {

const targetRoute = route.startsWith(CONST.SAML_REDIRECT_URL) ? ROUTES.HOME : route;
linkTo(navigationRef.current, targetRoute, options);
closeSidePanelOnNarrowScreen();
closeSidePanelOnNarrowScreen(route);
}
/**
* When routes are compared to determine whether the fallback route passed to the goUp function is in the state,
Expand Down
94 changes: 94 additions & 0 deletions tests/navigation/NavigateTests.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {act, render} from '@testing-library/react-native';
import React from 'react';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import SidePanelActions from '@libs/actions/SidePanel';
import getIsNarrowLayout from '@libs/getIsNarrowLayout';
import Navigation from '@libs/Navigation/Navigation';
import navigationRef from '@libs/Navigation/navigationRef';
Expand All @@ -9,6 +10,7 @@ import NAVIGATORS from '@src/NAVIGATORS';
import ROUTES from '@src/ROUTES';
import SCREENS from '@src/SCREENS';
import TestNavigationContainer from '../utils/TestNavigationContainer';
import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';

jest.mock('@hooks/useResponsiveLayout', () => jest.fn());
jest.mock('@libs/getIsNarrowLayout', () => jest.fn());
Expand Down Expand Up @@ -246,5 +248,97 @@ describe('Navigate', () => {
expect(lastSplitAfterNavigate?.name).toBe(NAVIGATORS.RIGHT_MODAL_NAVIGATOR);
expect(lastSplitAfterNavigate?.state?.routes.at(-1)?.name).toBe(SCREENS.RIGHT_MODAL.SETTINGS);
});

it.each([ROUTES.REPORT_ADD_ATTACHMENT.getRoute('1'), ROUTES.REPORT_ATTACHMENTS.getRoute()])(
'does not close the side panel when navigating to attachment routes (%s)',
async (route) => {
// Given the initialized navigation on the narrow layout with the reports split navigator
render(
<TestNavigationContainer
initialState={{
index: 0,
routes: [
{
name: NAVIGATORS.REPORTS_SPLIT_NAVIGATOR,
state: {
index: 1,
routes: [
{
name: SCREENS.INBOX,
},
{
name: SCREENS.REPORT,
params: {reportID: '1'},
},
],
},
},
],
}}
/>,
);

const closeSidePanelSpy = jest.spyOn(SidePanelActions, 'closeSidePanel');

// Open side panel on narrow screen
act(() => {
SidePanelActions.openSidePanel(true);
});
await waitForBatchedUpdates();

// When navigate to an attachment route
act(() => {
Navigation.navigate(route);
});

// Then side panel should remain open
expect(closeSidePanelSpy).not.toHaveBeenCalled();
},
);

it('closes the side panel when navigating to workspaces list', async () => {
// Given the initialized navigation on the narrow layout with the reports split navigator
render(
<TestNavigationContainer
initialState={{
index: 0,
routes: [
{
name: NAVIGATORS.REPORTS_SPLIT_NAVIGATOR,
state: {
index: 1,
routes: [
{
name: SCREENS.INBOX,
},
{
name: SCREENS.REPORT,
params: {reportID: '1'},
},
],
},
},
],
}}
/>,
);

const closeSidePanelSpy = jest.spyOn(SidePanelActions, 'closeSidePanel');

// Open side panel on narrow screen
act(() => {
SidePanelActions.openSidePanel(true);
});
await waitForBatchedUpdates();

// When navigate to a non-attachment route
act(() => {
Navigation.navigate(ROUTES.WORKSPACES_LIST.getRoute());
});

// Then side panel should close on narrow screen
expect(closeSidePanelSpy).toHaveBeenCalledWith(true);
expect(closeSidePanelSpy).toHaveBeenCalledTimes(1);
});
});
});
Loading