Skip to content

Commit

Permalink
Merge pull request Expensify#44764 from dominictb/fix/43737-lhn-syste…
Browse files Browse the repository at this point in the history
…m-msg

fix: restore report lastMessageText after resolve whisper msg
  • Loading branch information
Julesssss committed Jul 31, 2024
2 parents 8ef59e7 + 275cdbe commit 73ec7f8
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 1 deletion.
31 changes: 31 additions & 0 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7316,6 +7316,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 @@ -7638,6 +7668,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[] = [
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

0 comments on commit 73ec7f8

Please sign in to comment.