diff --git a/src/libs/OptionsListUtils.ts b/src/libs/OptionsListUtils.ts index 1b3dd5affacf1..d9ef0be7e9ab1 100644 --- a/src/libs/OptionsListUtils.ts +++ b/src/libs/OptionsListUtils.ts @@ -857,6 +857,15 @@ function getLastMessageTextForReport( lastMessageTextFromReport = getReportActionMessageText(lastReportAction); } + if (reportID) { + const chatReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${report?.chatReportID}`]; + // If the report is a one-transaction report, get the last message text from combined report actions so the LHN can display modifications to the transaction thread or the report itself + const transactionThreadReportID = getOneTransactionThreadReportID(report, chatReport, allSortedReportActions[reportID]); + if (transactionThreadReportID) { + lastMessageTextFromReport = getReportActionMessageText(lastReportAction); + } + } + return lastMessageTextFromReport || (report?.lastMessageText ?? ''); } diff --git a/tests/unit/SidebarUtilsTest.ts b/tests/unit/SidebarUtilsTest.ts index c7a11a5947004..b497641c48e8b 100644 --- a/tests/unit/SidebarUtilsTest.ts +++ b/tests/unit/SidebarUtilsTest.ts @@ -4,7 +4,7 @@ import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; import Onyx from 'react-native-onyx'; import useReportIsArchived from '@hooks/useReportIsArchived'; import DateUtils from '@libs/DateUtils'; -import {getReportActionMessageText} from '@libs/ReportActionsUtils'; +import {getOriginalMessage, getReportActionMessageText} from '@libs/ReportActionsUtils'; import {getAllReportErrors} from '@libs/ReportUtils'; import SidebarUtils from '@libs/SidebarUtils'; import initOnyxDerivedValues from '@userActions/OnyxDerived'; @@ -13,7 +13,8 @@ import ONYXKEYS from '@src/ONYXKEYS'; import type {Policy, Report, ReportAction, ReportActions, Transaction, TransactionViolation, TransactionViolations} from '@src/types/onyx'; import type {ReportCollectionDataSet} from '@src/types/onyx/Report'; import type {TransactionViolationsCollectionDataSet} from '@src/types/onyx/TransactionViolation'; -import {chatReportR14932} from '../../__mocks__/reportData/reports'; +import {actionR14932 as mockIOUAction} from '../../__mocks__/reportData/actions'; +import {chatReportR14932, iouReportR14932} from '../../__mocks__/reportData/reports'; import createRandomPolicy from '../utils/collections/policies'; import createRandomReportAction from '../utils/collections/reportActions'; import createRandomReport from '../utils/collections/reports'; @@ -1125,6 +1126,69 @@ describe('SidebarUtils', () => { oneTransactionThreadReport: undefined, }); + expect(result?.alternateText).toBe(`You: ${getReportActionMessageText(lastAction)}`); + }); + it('returns the last action message as an alternate text if the expense report is the one expense report', async () => { + const IOUTransactionID = `${ONYXKEYS.COLLECTION.TRANSACTION}TRANSACTION_IOU` as const; + + iouReportR14932.reportID = '5'; + chatReportR14932.reportID = '6'; + iouReportR14932.lastActorAccountID = undefined; + + const report: Report = { + ...createRandomReport(1), + chatType: 'policyExpenseChat', + pendingAction: null, + isOwnPolicyExpenseChat: true, + parentReportID: iouReportR14932.reportID, + parentReportActionID: mockIOUAction.reportActionID, + lastActorAccountID: undefined, + }; + + const linkedCreateAction: ReportAction = { + ...mockIOUAction, + originalMessage: {...getOriginalMessage(mockIOUAction), IOUTransactionID}, + childReportID: report.reportID, + reportActionID: '3', + }; + + const lastAction: ReportAction = { + ...createRandomReportAction(1), + message: [ + { + type: 'COMMENT', + html: 'test action', + text: 'test action', + }, + ], + originalMessage: { + whisperedTo: [], + }, + + created: DateUtils.getDBTime(), + lastModified: DateUtils.getDBTime(), + shouldShow: true, + pendingAction: null, + actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT, + actorAccountID: undefined, + }; + + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${iouReportR14932.reportID}`, iouReportR14932); + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${chatReportR14932.reportID}`, chatReportR14932); + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`, report); + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.reportID}`, {[lastAction.reportActionID]: lastAction}); + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReportR14932.reportID}`, {[linkedCreateAction.reportActionID]: linkedCreateAction}); + + const result = SidebarUtils.getOptionData({ + report: iouReportR14932, + reportAttributes: undefined, + reportNameValuePairs: {}, + personalDetails: {}, + policy: undefined, + parentReportAction: undefined, + oneTransactionThreadReport: undefined, + }); + expect(result?.alternateText).toBe(`You: ${getReportActionMessageText(lastAction)}`); }); });