diff --git a/src/components/ReportActionItem/TaskPreview.tsx b/src/components/ReportActionItem/TaskPreview.tsx index 46dfe1fd46fe..7dc0a10188ce 100644 --- a/src/components/ReportActionItem/TaskPreview.tsx +++ b/src/components/ReportActionItem/TaskPreview.tsx @@ -29,13 +29,13 @@ import ControlSelection from '@libs/ControlSelection'; import {canUseTouchScreen} from '@libs/DeviceCapabilities'; import getButtonState from '@libs/getButtonState'; import getNonEmptyStringOnyxID from '@libs/getNonEmptyStringOnyxID'; +import getReportRouteForCurrentContext from '@libs/Navigation/helpers/getReportRouteForCurrentContext'; import Navigation from '@libs/Navigation/Navigation'; import Parser from '@libs/Parser'; import {getOriginalMessage} from '@libs/ReportActionsUtils'; import {isCanceledTaskReport, isOpenTaskReport, isReportManager} from '@libs/ReportUtils'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; -import ROUTES from '@src/ROUTES'; import type {Report, ReportAction} from '@src/types/onyx'; import {isEmptyObject} from '@src/types/utils/EmptyObject'; @@ -121,7 +121,7 @@ function TaskPreview({action, chatReportID, currentUserPersonalDetails, isHovere return ( Navigation.navigate(ROUTES.REPORT_WITH_ID.getRoute(taskReportID, undefined, undefined, Navigation.getActiveRoute()))} + onPress={() => Navigation.navigate(getReportRouteForCurrentContext({reportID: taskReportID}))} onPressIn={() => canUseTouchScreen() && ControlSelection.block()} onPressOut={() => ControlSelection.unblock()} onLongPress={(event) => diff --git a/src/libs/Navigation/helpers/getReportRouteForCurrentContext.ts b/src/libs/Navigation/helpers/getReportRouteForCurrentContext.ts new file mode 100644 index 000000000000..e9b2dc25b8f7 --- /dev/null +++ b/src/libs/Navigation/helpers/getReportRouteForCurrentContext.ts @@ -0,0 +1,23 @@ +import Navigation from '@libs/Navigation/Navigation'; +import type {Route} from '@src/ROUTES'; +import ROUTES from '@src/ROUTES'; +import isSearchTopmostFullScreenRoute from './isSearchTopmostFullScreenRoute'; + +type GetReportRouteForCurrentContextParams = { + reportID: string | undefined; +}; + +function getReportRouteForCurrentContext({reportID}: GetReportRouteForCurrentContextParams): Route { + if (!reportID) { + return ROUTES.REPORT_WITH_ID.getRoute(''); + } + + const backTo = Navigation.getActiveRoute(); + if (isSearchTopmostFullScreenRoute()) { + return ROUTES.SEARCH_REPORT.getRoute({reportID, backTo}); + } + + return ROUTES.REPORT_WITH_ID.getRoute(reportID, undefined, undefined, backTo); +} + +export default getReportRouteForCurrentContext; diff --git a/tests/unit/Navigation/getReportRouteForCurrentContextTest.ts b/tests/unit/Navigation/getReportRouteForCurrentContextTest.ts new file mode 100644 index 000000000000..3c63c5236cde --- /dev/null +++ b/tests/unit/Navigation/getReportRouteForCurrentContextTest.ts @@ -0,0 +1,36 @@ +import getReportRouteForCurrentContext from '@libs/Navigation/helpers/getReportRouteForCurrentContext'; +import isSearchTopmostFullScreenRoute from '@libs/Navigation/helpers/isSearchTopmostFullScreenRoute'; +import Navigation from '@libs/Navigation/Navigation'; +import ROUTES from '@src/ROUTES'; + +jest.mock('@libs/Navigation/Navigation', () => ({ + getActiveRoute: jest.fn(), +})); + +jest.mock('@libs/Navigation/helpers/isSearchTopmostFullScreenRoute', () => jest.fn()); + +describe('getReportRouteForCurrentContext', () => { + const mockGetActiveRoute = Navigation.getActiveRoute as jest.MockedFunction; + const mockIsSearchTopmostFullScreenRoute = isSearchTopmostFullScreenRoute as jest.MockedFunction; + + beforeEach(() => { + jest.clearAllMocks(); + mockGetActiveRoute.mockReturnValue('search?q=type:chat'); + mockIsSearchTopmostFullScreenRoute.mockReturnValue(false); + }); + + it('returns the default report route when Search is not the topmost fullscreen route', () => { + expect(getReportRouteForCurrentContext({reportID: '42'})).toBe(ROUTES.REPORT_WITH_ID.getRoute('42', undefined, undefined, 'search?q=type:chat')); + }); + + it('returns the search report route when Search is the topmost fullscreen route', () => { + mockIsSearchTopmostFullScreenRoute.mockReturnValue(true); + + expect(getReportRouteForCurrentContext({reportID: '42'})).toBe( + ROUTES.SEARCH_REPORT.getRoute({ + reportID: '42', + backTo: 'search?q=type:chat', + }), + ); + }); +});