From 91393c8a5690376904897337a7cd4ec6deb010dc Mon Sep 17 00:00:00 2001 From: Tomasz Misiukiewicz Date: Tue, 10 Dec 2024 12:43:23 +0100 Subject: [PATCH 1/2] fix displaying header for group chat --- src/libs/actions/Report.ts | 2 +- src/libs/shouldFetchReport.ts | 5 ++--- src/pages/home/ReportScreen.tsx | 4 ++-- src/pages/home/report/ReportActionsView.tsx | 3 ++- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index a346a71ddb78..2cdb4c2c1400 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -956,7 +956,7 @@ function openReport( value: {[optimisticCreatedAction.reportActionID]: optimisticCreatedAction}, }, { - onyxMethod: Onyx.METHOD.MERGE, + onyxMethod: Onyx.METHOD.SET, key: `${ONYXKEYS.COLLECTION.REPORT_METADATA}${reportID}`, value: { isOptimisticReport: true, diff --git a/src/libs/shouldFetchReport.ts b/src/libs/shouldFetchReport.ts index 3a29e95ecfb1..ae0384bf728d 100644 --- a/src/libs/shouldFetchReport.ts +++ b/src/libs/shouldFetchReport.ts @@ -1,9 +1,8 @@ import type {OnyxEntry} from 'react-native-onyx'; import type Report from '@src/types/onyx/Report'; -import * as ReportUtils from './ReportUtils'; +import type ReportMetadata from '@src/types/onyx/ReportMetadata'; -export default function shouldFetchReport(report: OnyxEntry) { - const reportMetadata = ReportUtils.getReportMetadata(report?.reportID); +export default function shouldFetchReport(report: OnyxEntry, reportMetadata: OnyxEntry) { // 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; diff --git a/src/pages/home/ReportScreen.tsx b/src/pages/home/ReportScreen.tsx index 97582f75b7b1..fb82f81af178 100644 --- a/src/pages/home/ReportScreen.tsx +++ b/src/pages/home/ReportScreen.tsx @@ -484,7 +484,7 @@ function ReportScreen({route, currentReportID = '', navigation}: ReportScreenPro return; } - if (!shouldFetchReport(report)) { + if (!shouldFetchReport(report, reportMetadata)) { return; } // When creating an optimistic report that already exists, we need to skip openReport @@ -495,7 +495,7 @@ function ReportScreen({route, currentReportID = '', navigation}: ReportScreenPro } fetchReport(); - }, [report, fetchReport, reportIDFromRoute, isLoadingApp]); + }, [reportIDFromRoute, isLoadingApp, report, reportMetadata, fetchReport]); const dismissBanner = useCallback(() => { setIsBannerVisible(false); diff --git a/src/pages/home/report/ReportActionsView.tsx b/src/pages/home/report/ReportActionsView.tsx index ee7c929acc7d..07aff453fb0c 100755 --- a/src/pages/home/report/ReportActionsView.tsx +++ b/src/pages/home/report/ReportActionsView.tsx @@ -93,6 +93,7 @@ function ReportActionsView({ ReportActionsUtils.getSortedReportActionsForDisplay(reportActions, ReportUtils.canUserPerformWriteAction(report), true), }); const [transactionThreadReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${transactionThreadReportID ?? -1}`); + const [reportMetadata] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_METADATA}${report.reportID}`); const prevTransactionThreadReport = usePrevious(transactionThreadReport); const reportActionID = route?.params?.reportActionID; const prevReportActionID = usePrevious(reportActionID); @@ -116,7 +117,7 @@ function ReportActionsView({ const reportID = report.reportID; const isReportFullyVisible = useMemo((): boolean => getIsReportFullyVisible(isFocused), [isFocused]); const openReportIfNecessary = () => { - if (!shouldFetchReport(report)) { + if (!shouldFetchReport(report, reportMetadata)) { return; } From 05e49db8b14d3aa95b3ec9d3485ff18b95a9bbe4 Mon Sep 17 00:00:00 2001 From: Tomasz Misiukiewicz Date: Fri, 20 Dec 2024 09:03:02 +0100 Subject: [PATCH 2/2] fix lint changed --- src/libs/ReportActionsUtils.ts | 6 +++++- src/libs/actions/Report.ts | 12 +++++++++-- src/pages/home/report/ReportActionsView.tsx | 22 ++++++++++++--------- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/libs/ReportActionsUtils.ts b/src/libs/ReportActionsUtils.ts index dd17adbda338..455a125ad0c3 100644 --- a/src/libs/ReportActionsUtils.ts +++ b/src/libs/ReportActionsUtils.ts @@ -975,7 +975,11 @@ function getMostRecentReportActionLastModified(): string { /** * @returns The report preview action or `null` if one couldn't be found */ -function getReportPreviewAction(chatReportID: string, iouReportID: string): OnyxEntry> { +function getReportPreviewAction(chatReportID: string | undefined, iouReportID: string | undefined): OnyxEntry> { + if (!chatReportID || !iouReportID) { + return; + } + return Object.values(allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReportID}`] ?? {}).find( (reportAction): reportAction is ReportAction => reportAction && isActionOfType(reportAction, CONST.REPORT.ACTIONS.TYPE.REPORT_PREVIEW) && getOriginalMessage(reportAction)?.linkedReportID === iouReportID, diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index 24d67f3ba4b9..ab924906352e 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -1208,7 +1208,11 @@ function navigateToAndOpenChildReport(childReportID = '-1', parentReportAction: * Gets the older actions that have not been read yet. * Normally happens when you scroll up on a chat, and the actions have not been read yet. */ -function getOlderActions(reportID: string, reportActionID: string) { +function getOlderActions(reportID: string | undefined, reportActionID: string | undefined) { + if (!reportID || !reportActionID) { + return; + } + const optimisticData: OnyxUpdate[] = [ { onyxMethod: Onyx.METHOD.MERGE, @@ -1262,7 +1266,11 @@ function getOlderActions(reportID: string, reportActionID: string) { * Gets the newer actions that have not been read yet. * Normally happens when you are not located at the bottom of the list and scroll down on a chat. */ -function getNewerActions(reportID: string, reportActionID: string) { +function getNewerActions(reportID: string | undefined, reportActionID: string | undefined) { + if (!reportID || !reportActionID) { + return; + } + const optimisticData: OnyxUpdate[] = [ { onyxMethod: Onyx.METHOD.MERGE, diff --git a/src/pages/home/report/ReportActionsView.tsx b/src/pages/home/report/ReportActionsView.tsx index 750a1ca3aa2b..4b62c0e985fb 100755 --- a/src/pages/home/report/ReportActionsView.tsx +++ b/src/pages/home/report/ReportActionsView.tsx @@ -88,11 +88,11 @@ function ReportActionsView({ const reactionListRef = useContext(ReactionListContext); const route = useRoute>(); const [session] = useOnyx(ONYXKEYS.SESSION); - const [transactionThreadReportActions] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThreadReportID ?? -1}`, { + const [transactionThreadReportActions] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThreadReportID ?? CONST.DEFAULT_NUMBER_ID}`, { selector: (reportActions: OnyxEntry) => ReportActionsUtils.getSortedReportActionsForDisplay(reportActions, ReportUtils.canUserPerformWriteAction(report), true), }); - const [transactionThreadReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${transactionThreadReportID ?? -1}`); + const [transactionThreadReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${transactionThreadReportID ?? CONST.DEFAULT_NUMBER_ID}`); const [reportMetadata] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_METADATA}${report.reportID}`); const prevTransactionThreadReport = usePrevious(transactionThreadReport); const reportActionID = route?.params?.reportActionID; @@ -168,7 +168,7 @@ function ReportActionsView({ actions.push(optimisticCreatedAction); } - const reportPreviewAction = ReportActionsUtils.getReportPreviewAction(report.chatReportID ?? '', report.reportID); + const reportPreviewAction = ReportActionsUtils.getReportPreviewAction(report.chatReportID, report.reportID); const moneyRequestActions = allReportActions.filter((action) => { const originalMessage = ReportActionsUtils.isMoneyRequestAction(action) ? ReportActionsUtils.getOriginalMessage(action) : undefined; return ( @@ -284,11 +284,11 @@ function ReportActionsView({ if (!isEmptyObject(transactionThreadReport)) { // Get newer actions based on the newest reportAction for the current report const newestActionCurrentReport = reportActionIDMap.find((item) => item.reportID === reportID); - Report.getNewerActions(newestActionCurrentReport?.reportID ?? '-1', newestActionCurrentReport?.reportActionID ?? '-1'); + Report.getNewerActions(newestActionCurrentReport?.reportID, newestActionCurrentReport?.reportActionID); // Get newer actions based on the newest reportAction for the transaction thread report const newestActionTransactionThreadReport = reportActionIDMap.find((item) => item.reportID === transactionThreadReport.reportID); - Report.getNewerActions(newestActionTransactionThreadReport?.reportID ?? '-1', newestActionTransactionThreadReport?.reportActionID ?? '-1'); + Report.getNewerActions(newestActionTransactionThreadReport?.reportID, newestActionTransactionThreadReport?.reportActionID); } else { Report.getNewerActions(reportID, newestReportAction.reportActionID); } @@ -330,7 +330,11 @@ function ReportActionsView({ }, []); const handleReportActionPagination = useCallback( - ({firstReportActionID}: {firstReportActionID: string}) => { + ({firstReportActionID}: {firstReportActionID: string | undefined}) => { + if (!firstReportActionID) { + return; + } + // This function is a placeholder as the actual pagination is handled by visibleReportActions if (!hasMoreCached && !hasNewestReportAction) { isFirstLinkedActionRender.current = false; @@ -374,11 +378,11 @@ function ReportActionsView({ if (!isEmptyObject(transactionThreadReport)) { // Get older actions based on the oldest reportAction for the current report const oldestActionCurrentReport = reportActionIDMap.findLast((item) => item.reportID === reportID); - Report.getOlderActions(oldestActionCurrentReport?.reportID ?? '-1', oldestActionCurrentReport?.reportActionID ?? '-1'); + Report.getOlderActions(oldestActionCurrentReport?.reportID, oldestActionCurrentReport?.reportActionID); // Get older actions based on the oldest reportAction for the transaction thread report const oldestActionTransactionThreadReport = reportActionIDMap.findLast((item) => item.reportID === transactionThreadReport.reportID); - Report.getOlderActions(oldestActionTransactionThreadReport?.reportID ?? '-1', oldestActionTransactionThreadReport?.reportActionID ?? '-1'); + Report.getOlderActions(oldestActionTransactionThreadReport?.reportID, oldestActionTransactionThreadReport?.reportActionID); } else { // Retrieve the next REPORT.ACTIONS.LIMIT sized page of comments Report.getOlderActions(reportID, oldestReportAction.reportActionID); @@ -416,7 +420,7 @@ function ReportActionsView({ didLoadNewerChats.current = true; if ((reportActionID && indexOfLinkedAction > -1) || !reportActionID) { - handleReportActionPagination({firstReportActionID: newestReportAction?.reportActionID ?? '-1'}); + handleReportActionPagination({firstReportActionID: newestReportAction?.reportActionID}); } }, [