diff --git a/src/libs/Navigation/Navigation.js b/src/libs/Navigation/Navigation.js index 42c7bcdf206..7b92a91ff7c 100644 --- a/src/libs/Navigation/Navigation.js +++ b/src/libs/Navigation/Navigation.js @@ -82,6 +82,19 @@ function navigate(route = ROUTES.HOME, type) { linkTo(navigationRef.current, route, type); } +/** + * Replaces the current route in the main chat pane, without adding the existing route to the stack. + * Makes it so that the back button will not return to the previous route. + * @param {String} route + */ +function replaceCentralPaneScreen(route) { + if (!canNavigate('replace', {route})) { + return; + } + + navigationRef.current.dispatch(StackActions.replace(NAVIGATORS.CENTRAL_PANE_NAVIGATOR, route)); +} + /** * @param {String} fallbackRoute - Fallback route if pop/goBack action should, but is not possible within RHP * @param {Bool} shouldEnforceFallback - Enforces navigation to fallback route @@ -224,6 +237,7 @@ function setIsNavigationReady() { export default { canNavigate, navigate, + replaceCentralPaneScreen, setParams, dismissModal, isActiveRoute, diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index 9027b8fb2f3..52569d8a7be 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -804,6 +804,19 @@ function handleReportChanged(report) { return; } + // It is possible that we optimistically created a DM/group-DM for a set of users for which a report already exists. + // In this case, the API will let us know by returning a preexistingReportID. + // We should clear out the optimistically created report and re-route the user to the preexisting report. + if (report && report.reportID && report.preexistingReportID) { + Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`, null); + + // Only re-route them if they are still looking at the optimistically created report + if (Navigation.getActiveRoute().includes(`/r/${report.reportID}`)) { + Navigation.replaceCentralPaneScreen(ROUTES.getReportRoute(report.preexistingReportID)); + } + return; + } + if (report && report.reportID) { allReports[report.reportID] = report;