From d47bdfd07e54772aa28da8829dc7291061ac14f7 Mon Sep 17 00:00:00 2001 From: war-in Date: Fri, 7 Nov 2025 13:53:45 +0100 Subject: [PATCH] fix: move InteractionManager from ReportUtils.ts to handlePreexistingReport --- src/libs/ReportUtils.ts | 6 +-- src/libs/actions/Report.ts | 79 ++++++++++++++++++++------------------ 2 files changed, 42 insertions(+), 43 deletions(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index ee1857a546ad..6a57154ab585 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -9,7 +9,6 @@ import isNumber from 'lodash/isNumber'; import mapValues from 'lodash/mapValues'; import lodashMaxBy from 'lodash/maxBy'; import type {ColorValue} from 'react-native'; -import {InteractionManager} from 'react-native'; import type {NullishDeep, OnyxCollection, OnyxEntry, OnyxUpdate} from 'react-native-onyx'; import Onyx from 'react-native-onyx'; import type {SvgProps} from 'react-native-svg'; @@ -1013,10 +1012,7 @@ Onyx.connect({ return acc; } - // eslint-disable-next-line @typescript-eslint/no-deprecated - InteractionManager.runAfterInteractions(() => { - handlePreexistingReport(report); - }); + handlePreexistingReport(report); // Get all reports, which are the ones that are: // - Owned by the same user diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index a0b73292f9b6..07539a849c92 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -1864,49 +1864,52 @@ function handlePreexistingReport(report: 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. - let callback = () => { - const existingReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${preexistingReportID}`]; - - Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, null); - Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${preexistingReportID}`, { - ...report, - reportID: preexistingReportID, - preexistingReportID: null, - // Replacing the existing report's participants to avoid duplicates - participants: existingReport?.participants ?? report.participants, - }); - Onyx.set(`${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${reportID}`, null); - }; - // Only re-route them if they are still looking at the optimistically created report - if (Navigation.getActiveRoute().includes(`/r/${reportID}`)) { - const currCallback = callback; - callback = () => { - currCallback(); - Navigation.setParams({reportID: preexistingReportID.toString()}); + // eslint-disable-next-line @typescript-eslint/no-deprecated + InteractionManager.runAfterInteractions(() => { + // 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. + let callback = () => { + const existingReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${preexistingReportID}`]; + + Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, null); + Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${preexistingReportID}`, { + ...report, + reportID: preexistingReportID, + preexistingReportID: null, + // Replacing the existing report's participants to avoid duplicates + participants: existingReport?.participants ?? report.participants, + }); + Onyx.set(`${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${reportID}`, null); }; + // Only re-route them if they are still looking at the optimistically created report + if (Navigation.getActiveRoute().includes(`/r/${reportID}`)) { + const currCallback = callback; + callback = () => { + currCallback(); + Navigation.setParams({reportID: preexistingReportID.toString()}); + }; - // The report screen will listen to this event and transfer the draft comment to the existing report - // This will allow the newest draft comment to be transferred to the existing report - DeviceEventEmitter.emit(`switchToPreExistingReport_${reportID}`, { - preexistingReportID, - callback, - }); + // The report screen will listen to this event and transfer the draft comment to the existing report + // This will allow the newest draft comment to be transferred to the existing report + DeviceEventEmitter.emit(`switchToPreExistingReport_${reportID}`, { + preexistingReportID, + callback, + }); - return; - } + return; + } - // In case the user is not on the report screen, we will transfer the report draft comment directly to the existing report - // after that clear the optimistically created report - const draftReportComment = allReportDraftComments?.[`${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${reportID}`]; - if (!draftReportComment) { - callback(); - return; - } + // In case the user is not on the report screen, we will transfer the report draft comment directly to the existing report + // after that clear the optimistically created report + const draftReportComment = allReportDraftComments?.[`${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${reportID}`]; + if (!draftReportComment) { + callback(); + return; + } - saveReportDraftComment(preexistingReportID, draftReportComment, callback); + saveReportDraftComment(preexistingReportID, draftReportComment, callback); + }); } /** Deletes a comment from the report, basically sets it as empty string */