diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 305bde1dc387..7fc05873acd8 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -7292,6 +7292,36 @@ function findPolicyExpenseChatByPolicyID(policyID: string): OnyxEntry { return Object.values(ReportConnection.getAllReports() ?? {}).find((report) => isPolicyExpenseChat(report) && report?.policyID === policyID); } +/** + * A function to get the report last message. This is usually used to restore the report message preview in LHN after report actions change. + * @param reportID + * @param actionsToMerge + * @returns containing the calculated message preview data of the report + */ +function getReportLastMessage(reportID: string, actionsToMerge?: ReportActions) { + let result: Partial = { + lastMessageTranslationKey: '', + lastMessageText: '', + lastVisibleActionCreated: '', + }; + + const {lastMessageText = '', lastMessageTranslationKey = ''} = getLastVisibleMessage(reportID, actionsToMerge); + + if (lastMessageText || lastMessageTranslationKey) { + const lastVisibleAction = ReportActionsUtils.getLastVisibleAction(reportID, actionsToMerge); + const lastVisibleActionCreated = lastVisibleAction?.created; + const lastActorAccountID = lastVisibleAction?.actorAccountID; + result = { + lastMessageTranslationKey, + lastMessageText, + lastVisibleActionCreated, + lastActorAccountID, + }; + } + + return result; +} + function getSourceIDFromReportAction(reportAction: OnyxEntry): string { const message = Array.isArray(reportAction?.message) ? reportAction?.message?.at(-1) ?? null : reportAction?.message ?? null; const html = message?.html ?? ''; @@ -7613,6 +7643,7 @@ export { canBeExported, isExported, hasOnlyNonReimbursableTransactions, + getReportLastMessage, getMostRecentlyVisitedReport, getSourceIDFromReportAction, getReport, diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index f106c6cdb18d..2a6ef8da9214 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -3700,6 +3700,24 @@ function resolveActionableMentionWhisper(reportId: string, reportAction: OnyxEnt resolution, }; + const optimisticReportActions = { + [reportAction.reportActionID]: { + originalMessage: { + resolution, + }, + }, + }; + + const reportUpdateDataWithPreviousLastMessage = ReportUtils.getReportLastMessage(reportId, optimisticReportActions as ReportActions); + + const report = ReportConnection.getAllReports()?.[`${ONYXKEYS.COLLECTION.REPORT}${reportId}`]; + const reportUpdateDataWithCurrentLastMessage = { + lastMessageTranslationKey: report?.lastMessageTranslationKey, + lastMessageText: report?.lastMessageText, + lastVisibleActionCreated: report?.lastVisibleActionCreated, + lastActorAccountID: report?.lastActorAccountID, + }; + const optimisticData: OnyxUpdate[] = [ { onyxMethod: Onyx.METHOD.MERGE, @@ -3713,6 +3731,11 @@ function resolveActionableMentionWhisper(reportId: string, reportAction: OnyxEnt }, }, }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${reportId}`, + value: reportUpdateDataWithPreviousLastMessage, + }, ]; const failureData: OnyxUpdate[] = [ @@ -3728,6 +3751,11 @@ function resolveActionableMentionWhisper(reportId: string, reportAction: OnyxEnt }, }, }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${reportId}`, + value: reportUpdateDataWithCurrentLastMessage, // revert back to the current report last message data in case of failure + }, ]; const parameters: ResolveActionableMentionWhisperParams = { @@ -3747,6 +3775,24 @@ function resolveActionableReportMentionWhisper( return; } + const optimisticReportActions = { + [reportAction.reportActionID]: { + originalMessage: { + resolution, + }, + }, + }; + + const reportUpdateDataWithPreviousLastMessage = ReportUtils.getReportLastMessage(reportId, optimisticReportActions as ReportActions); + + const report = ReportConnection.getAllReports()?.[`${ONYXKEYS.COLLECTION.REPORT}${reportId}`]; + const reportUpdateDataWithCurrentLastMessage = { + lastMessageTranslationKey: report?.lastMessageTranslationKey, + lastMessageText: report?.lastMessageText, + lastVisibleActionCreated: report?.lastVisibleActionCreated, + lastActorAccountID: report?.lastActorAccountID, + }; + const optimisticData: OnyxUpdate[] = [ { onyxMethod: Onyx.METHOD.MERGE, @@ -3757,7 +3803,12 @@ function resolveActionableReportMentionWhisper( resolution, }, }, - }, + } as ReportActions, + }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${reportId}`, + value: reportUpdateDataWithPreviousLastMessage, }, ]; @@ -3773,6 +3824,11 @@ function resolveActionableReportMentionWhisper( }, }, }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${reportId}`, + value: reportUpdateDataWithCurrentLastMessage, // revert back to the current report last message data in case of failure + }, ]; const parameters: ResolveActionableReportMentionWhisperParams = {