Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7292,6 +7292,36 @@ function findPolicyExpenseChatByPolicyID(policyID: string): OnyxEntry<Report> {
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<Report> = {
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<ReportAction>): string {
const message = Array.isArray(reportAction?.message) ? reportAction?.message?.at(-1) ?? null : reportAction?.message ?? null;
const html = message?.html ?? '';
Expand Down Expand Up @@ -7613,6 +7643,7 @@ export {
canBeExported,
isExported,
hasOnlyNonReimbursableTransactions,
getReportLastMessage,
getMostRecentlyVisitedReport,
getSourceIDFromReportAction,
getReport,
Expand Down
58 changes: 57 additions & 1 deletion src/libs/actions/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -3713,6 +3731,11 @@ function resolveActionableMentionWhisper(reportId: string, reportAction: OnyxEnt
},
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${reportId}`,
value: reportUpdateDataWithPreviousLastMessage,
},
];

const failureData: OnyxUpdate[] = [
Comment thread
dominictb marked this conversation as resolved.
Expand All @@ -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 = {
Expand All @@ -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,
Expand All @@ -3757,7 +3803,12 @@ function resolveActionableReportMentionWhisper(
resolution,
},
},
},
} as ReportActions,
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${reportId}`,
value: reportUpdateDataWithPreviousLastMessage,
},
];

Expand All @@ -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 = {
Expand Down