Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix initial chat switch jarring #49731

Merged
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
1 change: 1 addition & 0 deletions src/libs/actions/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1372,6 +1372,7 @@ function handleReportChanged(report: OnyxEntry<Report>) {
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
Expand Down
22 changes: 21 additions & 1 deletion src/pages/home/ReportScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<FlatList>(null);
const {canUseDefaultRooms} = usePermissions();
const reactionListRef = useRef<ReactionListRef>(null);
Expand Down Expand Up @@ -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.
Expand All @@ -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]);
Expand Down
Loading