diff --git a/src/components/ReportActionItem/MoneyRequestReceiptView.tsx b/src/components/ReportActionItem/MoneyRequestReceiptView.tsx index b0efa98af423..0d0259e5f85e 100644 --- a/src/components/ReportActionItem/MoneyRequestReceiptView.tsx +++ b/src/components/ReportActionItem/MoneyRequestReceiptView.tsx @@ -149,6 +149,8 @@ function MoneyRequestReceiptView({ const [isLoading, setIsLoading] = useState(true); const parentReportAction = report?.parentReportActionID ? parentReportActions?.[report.parentReportActionID] : undefined; + const [parentReportActionChildReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${getNonEmptyStringOnyxID(parentReportAction?.childReportID)}`); + const originalReportID = useOriginalReportID(report?.reportID, parentReportAction); const {iouReport, chatReport: chatIOUReport, isChatIOUReportArchived} = useGetIOUReportFromReportAction(parentReportAction); const isTrackExpense = !mergeTransactionID && isTrackExpenseReportNew(report, parentReport, parentReportAction); @@ -429,6 +431,7 @@ function MoneyRequestReceiptView({ transaction?.transactionID ?? linkedTransactionID, parentReportAction, report.reportID, + parentReportActionChildReport, iouReport, chatIOUReport, isChatIOUReportArchived, diff --git a/src/components/Search/SearchList/ListItem/ChatListItem.tsx b/src/components/Search/SearchList/ListItem/ChatListItem.tsx index 2283a98d05ba..b9f2bddb9392 100644 --- a/src/components/Search/SearchList/ListItem/ChatListItem.tsx +++ b/src/components/Search/SearchList/ListItem/ChatListItem.tsx @@ -30,6 +30,7 @@ function ChatListItem({ }: ChatListItemProps) { const reportActionItem = item as unknown as ReportActionListItemType; const [reportStable] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${reportActionItem?.reportID}`, {selector: getStableReportSelector}); + const [transactionThreadReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${reportActionItem?.childReportID}`); const personalDetails = usePersonalDetails(); const styles = useThemeStyles(); const theme = useTheme(); @@ -79,6 +80,7 @@ function ChatListItem({ , iouReport: OnyxEntry, chatReport: OnyxEntry, isChatIOUReportArchived: boolean | undefined, originalReportID: string | undefined, isSingleTransactionView = false, ) { - const allReports = getAllReports(); - const transactionThreadReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportAction.childReportID}`]; - const {shouldDeleteTransactionThread, shouldDeleteIOUReport, updatedReportAction, updatedIOUReport, updatedReportPreviewAction, transactionThreadID, reportPreviewAction} = prepareToCleanUpMoneyRequest(transactionID, reportAction, transactionThreadReport, iouReport, chatReport, isChatIOUReportArchived, false); diff --git a/src/pages/Debug/ReportAction/DebugReportActionCreatePage.tsx b/src/pages/Debug/ReportAction/DebugReportActionCreatePage.tsx index e89fe551af14..35ba6510515b 100644 --- a/src/pages/Debug/ReportAction/DebugReportActionCreatePage.tsx +++ b/src/pages/Debug/ReportAction/DebugReportActionCreatePage.tsx @@ -1,4 +1,4 @@ -import React, {useCallback, useState} from 'react'; +import React, {useCallback, useMemo, useState} from 'react'; import {View} from 'react-native'; import type {OnyxEntry} from 'react-native-onyx'; import Button from '@components/Button'; @@ -28,6 +28,19 @@ import type {PersonalDetailsList, ReportAction, Session} from '@src/types/onyx'; type DebugReportActionCreatePageProps = PlatformStackScreenProps; +function isParsedReportAction(value: unknown): value is ReportAction { + return typeof value === 'object' && value !== null && 'reportActionID' in value; +} + +function parseReportActionJSON(draftReportAction: string): ReportAction | null { + try { + const parsedReportAction: unknown = JSON.parse(draftReportAction.replaceAll('\n', '')); + return isParsedReportAction(parsedReportAction) ? parsedReportAction : null; + } catch { + return null; + } +} + const getInitialReportAction = (reportID: string, session: OnyxEntry, personalDetailsList: OnyxEntry) => DebugUtils.stringifyJSON({ actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT, @@ -50,15 +63,21 @@ function DebugReportActionCreatePage({ const [personalDetailsList] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST); const [draftReportAction, setDraftReportAction] = useState(() => getInitialReportAction(reportID, session, personalDetailsList)); + const reportAction = useMemo(() => parseReportActionJSON(draftReportAction), [draftReportAction]); + + const [transactionThreadReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${reportAction?.childReportID}`); + const [error, setError] = useState(); const createReportAction = useCallback(() => { - const parsedReportAction = JSON.parse(draftReportAction.replaceAll('\n', '')) as ReportAction; + if (!reportAction) { + return; + } Debug.mergeDebugData(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, { - [parsedReportAction.reportActionID]: parsedReportAction, + [reportAction.reportActionID]: reportAction, }); Navigation.navigate(ROUTES.DEBUG_REPORT_TAB_ACTIONS.getRoute(reportID)); - }, [draftReportAction, reportID]); + }, [reportAction, reportID]); const editJSON = useCallback( (updatedJSON: string) => { @@ -105,9 +124,10 @@ function DebugReportActionCreatePage({ {translate('debug.preview')} - {!error ? ( + {!error && reportAction ? ( ; /** The transaction thread report associated with the report for this action, if any */ - transactionThreadReport?: OnyxEntry; + transactionThreadReport: OnyxEntry; /** Report action belonging to the report's parent */ parentReportAction: OnyxEntry; @@ -234,7 +234,7 @@ function PureReportActionItem({ const dismissError = () => { const transactionID = isMoneyRequestAction(action) ? getOriginalMessage(action)?.IOUTransactionID : undefined; if (isSendingMoney && transactionID && reportID) { - cleanUpMoneyRequest(transactionID, action, reportID, report, chatReport, undefined, originalReportID, true); + cleanUpMoneyRequest(transactionID, action, reportID, transactionThreadReport, report, chatReport, undefined, originalReportID, true); return; } if (action.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD && isReportActionLinked) { diff --git a/tests/ui/ClearReportActionErrorsUITest.tsx b/tests/ui/ClearReportActionErrorsUITest.tsx index d7f456a1765c..0048259e2c36 100644 --- a/tests/ui/ClearReportActionErrorsUITest.tsx +++ b/tests/ui/ClearReportActionErrorsUITest.tsx @@ -93,6 +93,7 @@ describe('ClearReportActionErrors UI', () => { { { { { { { { { { { { { { { { { parentReportActionID: 'parentAction', ownerAccountID: 0, }} + transactionThreadReport={undefined} parentReportAction={undefined} action={action} displayAsGroup={false} @@ -1589,6 +1605,7 @@ describe('PureReportActionItem', () => { { { { { { { { { { { {