diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index d4c42697d70d..0e67763032dd 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -1372,6 +1372,7 @@ function handleReportChanged(report: OnyxEntry) { if (report?.reportID && report.preexistingReportID) { let callback = () => { Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`, null); + Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${report.preexistingReportID}`, {...report, reportID: report.preexistingReportID, preexistingReportID: null}); Onyx.set(`${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${report.reportID}`, null); }; // Only re-route them if they are still looking at the optimistically created report diff --git a/src/pages/home/ReportScreen.tsx b/src/pages/home/ReportScreen.tsx index ef08d498dabd..55b0f4dbcf6c 100644 --- a/src/pages/home/ReportScreen.tsx +++ b/src/pages/home/ReportScreen.tsx @@ -4,7 +4,7 @@ import type {StackScreenProps} from '@react-navigation/stack'; import lodashIsEqual from 'lodash/isEqual'; import React, {memo, useCallback, useEffect, useMemo, useRef, useState} from 'react'; import type {FlatList, ViewStyle} from 'react-native'; -import {InteractionManager, View} from 'react-native'; +import {DeviceEventEmitter, InteractionManager, View} from 'react-native'; import type {OnyxEntry} from 'react-native-onyx'; import {useOnyx} from 'react-native-onyx'; import Banner from '@components/Banner'; @@ -104,6 +104,7 @@ function ReportScreen({route, currentReportID = '', navigation}: ReportScreenPro const isFocused = useIsFocused(); const prevIsFocused = usePrevious(isFocused); const firstRenderRef = useRef(true); + const isSkippingOpenReport = useRef(false); const flatListRef = useRef(null); const {canUseDefaultRooms} = usePermissions(); const reactionListRef = useRef(null); @@ -418,6 +419,19 @@ function ReportScreen({route, currentReportID = '', navigation}: ReportScreenPro Report.updateLastVisitTime(reportID); }, [reportID, isFocused]); + useEffect(() => { + const skipOpenReportListener = DeviceEventEmitter.addListener(`switchToPreExistingReport_${reportID}`, ({preexistingReportID}: {preexistingReportID: string}) => { + if (!preexistingReportID) { + return; + } + isSkippingOpenReport.current = true; + }); + + return () => { + skipOpenReportListener.remove(); + }; + }, [reportID]); + const fetchReportIfNeeded = useCallback(() => { // Report ID will be empty when the reports collection is empty. // This could happen when we are loading the collection for the first time after logging in. @@ -440,6 +454,12 @@ function ReportScreen({route, currentReportID = '', navigation}: ReportScreenPro if (!shouldFetchReport(report) && (isInitialPageReady || isLinkedMessagePageReady)) { return; } + // When creating an optimistic report that already exists, we need to skip openReport + // when replacing the optimistic report with the real one received from the server. + if (isSkippingOpenReport.current) { + isSkippingOpenReport.current = false; + return; + } fetchReport(); }, [report, fetchReport, reportIDFromRoute, isLoadingApp, isInitialPageReady, isLinkedMessagePageReady]);