From 7ed468988185d01f39067eac031b19bca2455d67 Mon Sep 17 00:00:00 2001 From: Abdelrahman Khattab Date: Sun, 16 Jun 2024 22:06:06 +0200 Subject: [PATCH] Fix app crash on loading null reportActions --- src/hooks/useReportIDs.tsx | 34 ++++++++++++++++++---------------- src/libs/ReportActionsUtils.ts | 2 +- src/libs/ReportUtils.ts | 12 +++++++++--- 3 files changed, 28 insertions(+), 20 deletions(-) diff --git a/src/hooks/useReportIDs.tsx b/src/hooks/useReportIDs.tsx index fa97e2047bca..d5089d9f7990 100644 --- a/src/hooks/useReportIDs.tsx +++ b/src/hooks/useReportIDs.tsx @@ -76,22 +76,24 @@ const chatReportSelector = (report: OnyxEntry): ChatReportSele const reportActionsSelector = (reportActions: OnyxEntry): ReportActionsSelector => (reportActions && - Object.values(reportActions).map((reportAction) => { - const {reportActionID, actionName, errors = [], originalMessage} = reportAction; - const decision = reportAction.message?.[0]?.moderationDecision?.decision; - - return { - reportActionID, - actionName, - errors, - message: [ - { - moderationDecision: {decision}, - }, - ] as Message[], - originalMessage, - }; - })) as ReportActionsSelector; + Object.values(reportActions) + .filter(Boolean) + .map((reportAction) => { + const {reportActionID, actionName, errors = [], originalMessage} = reportAction; + const decision = reportAction.message?.[0]?.moderationDecision?.decision; + + return { + reportActionID, + actionName, + errors, + message: [ + { + moderationDecision: {decision}, + }, + ] as Message[], + originalMessage, + }; + })) as ReportActionsSelector; const policySelector = (policy: OnyxEntry): PolicySelector => (policy && { diff --git a/src/libs/ReportActionsUtils.ts b/src/libs/ReportActionsUtils.ts index ab951919f8b3..d365cd756b68 100644 --- a/src/libs/ReportActionsUtils.ts +++ b/src/libs/ReportActionsUtils.ts @@ -703,7 +703,7 @@ function getSortedReportActionsForDisplay(reportActions: OnyxEntry shouldReportActionBeVisible(reportAction, key)) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index de3708067213..40d7155397f1 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -6756,7 +6756,9 @@ function getIndicatedMissingPaymentMethod(userWallet: OnyxEntry, rep */ function hasMissingPaymentMethod(userWallet: OnyxEntry, iouReportID: string): boolean { const reportActions = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReportID}`] ?? {}; - return Object.values(reportActions).some((action) => getIndicatedMissingPaymentMethod(userWallet, iouReportID, action) !== undefined); + return Object.values(reportActions) + .filter(Boolean) + .some((action) => getIndicatedMissingPaymentMethod(userWallet, iouReportID, action) !== undefined); } /** @@ -6782,7 +6784,9 @@ function getTripTransactions(tripRoomReportID: string | undefined): Transaction[ */ function hasActionsWithErrors(reportID: string): boolean { const reportActions = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`] ?? {}; - return Object.values(reportActions).some((action) => !isEmptyObject(action.errors)); + return Object.values(reportActions) + .filter(Boolean) + .some((action) => !isEmptyObject(action.errors)); } function isNonAdminOrOwnerOfPolicyExpenseChat(report: OnyxInputOrEntry, policy: OnyxInputOrEntry): boolean { @@ -6884,7 +6888,9 @@ function createDraftTransactionAndNavigateToParticipantSelector(transactionID: s return; } - const linkedTrackedExpenseReportAction = Object.values(reportActions).find((action) => (action.originalMessage as IOUMessage)?.IOUTransactionID === transactionID); + const linkedTrackedExpenseReportAction = Object.values(reportActions) + .filter(Boolean) + .find((action) => (action.originalMessage as IOUMessage)?.IOUTransactionID === transactionID); const {created, amount, currency, merchant, mccGroup} = getTransactionDetails(transaction) ?? {}; const comment = getTransactionCommentObject(transaction);