Skip to content

Commit

Permalink
Refactor shouldFetchReport function to reduce updates
Browse files Browse the repository at this point in the history
  • Loading branch information
pasyukevich committed Jan 30, 2025
1 parent d46c4cc commit 2a3c29e
Showing 3 changed files with 7 additions and 7 deletions.
5 changes: 2 additions & 3 deletions src/libs/shouldFetchReport.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import type {OnyxEntry} from 'react-native-onyx';
import type Report from '@src/types/onyx/Report';
import type ReportMetadata from '@src/types/onyx/ReportMetadata';

export default function shouldFetchReport(report: OnyxEntry<Report>, reportMetadata: OnyxEntry<ReportMetadata>) {
export default function shouldFetchReport(report: OnyxEntry<Report>, isOptimisticReport?: boolean) {
// If the report is optimistic, there's no need to fetch it. The original action should create it.
// If there is an error for creating the chat, there's no need to fetch it since it doesn't exist
return !reportMetadata?.isOptimisticReport && !report?.errorFields?.createChat;
return !isOptimisticReport && !report?.errorFields?.createChat;
}
7 changes: 4 additions & 3 deletions src/pages/home/ReportScreen.tsx
Original file line number Diff line number Diff line change
@@ -518,7 +518,7 @@ function ReportScreen({route, navigation}: ReportScreenProps) {
return;
}

if (!shouldFetchReport(report, reportMetadata)) {
if (!shouldFetchReport(report, reportMetadata.isOptimisticReport)) {
return;
}
// When creating an optimistic report that already exists, we need to skip openReport
@@ -529,7 +529,7 @@ function ReportScreen({route, navigation}: ReportScreenProps) {
}

fetchReport();
}, [reportIDFromRoute, isLoadingApp, report, reportMetadata, fetchReport]);
}, [reportIDFromRoute, isLoadingApp, report, fetchReport, reportMetadata]);

const dismissBanner = useCallback(() => {
setIsBannerVisible(false);
@@ -575,8 +575,9 @@ function ReportScreen({route, navigation}: ReportScreenProps) {
return;
}
fetchReportIfNeeded();
console.log('fetchReportIfNeeded');

Check failure on line 578 in src/pages/home/ReportScreen.tsx

GitHub Actions / Changed files ESLint check

Unexpected console statement
// eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps
}, [isLoadingReportOnyx]);
}, [isLoadingReportOnyx, fetchReportIfNeeded]);

useEffect(() => {
if (isLoadingReportOnyx || !reportActionIDFromRoute || isLinkedMessagePageReady) {
2 changes: 1 addition & 1 deletion src/pages/home/report/ReportActionsView.tsx
Original file line number Diff line number Diff line change
@@ -118,7 +118,7 @@ function ReportActionsView({
const reportID = report.reportID;
const isReportFullyVisible = useMemo((): boolean => getIsReportFullyVisible(isFocused), [isFocused]);
const openReportIfNecessary = () => {
if (!shouldFetchReport(report, reportMetadata)) {
if (!shouldFetchReport(report, reportMetadata?.isOptimisticReport)) {
return;
}

0 comments on commit 2a3c29e

Please sign in to comment.