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
8 changes: 6 additions & 2 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import mapValues from 'lodash/mapValues';
import lodashMaxBy from 'lodash/maxBy';
import type {ColorValue} from 'react-native';
import {InteractionManager} from 'react-native';
import type {OnyxCollection, OnyxEntry, OnyxUpdate} from 'react-native-onyx';
import type {NullishDeep, OnyxCollection, OnyxEntry, OnyxUpdate} from 'react-native-onyx';
import Onyx from 'react-native-onyx';
import type {SvgProps} from 'react-native-svg';
import type {OriginalMessageChangePolicy, OriginalMessageExportIntegration, OriginalMessageModifiedExpense} from 'src/types/onyx/OriginalMessage';
Expand Down Expand Up @@ -3771,7 +3771,11 @@ function buildOptimisticCancelPaymentReportAction(expenseReportID: string, amoun
* @param [actionsToMerge] - the optimistic merge actions that needs to be considered while fetching last visible message

*/
function getLastVisibleMessage(reportID: string | undefined, isReportArchived: boolean | undefined, actionsToMerge: ReportActions = {}): LastVisibleMessage {
function getLastVisibleMessage(
reportID: string | undefined,
isReportArchived: boolean | undefined,
actionsToMerge: Record<string, NullishDeep<ReportAction> | null> = {},
): LastVisibleMessage {
const report = getReportOrDraftReport(reportID);
const lastVisibleAction = getLastVisibleActionReportActionsUtils(reportID, canUserPerformWriteAction(report, isReportArchived), actionsToMerge);

Expand Down
17 changes: 4 additions & 13 deletions src/libs/actions/IOU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ import {buildAddMembersToWorkspaceOnyxData, buildUpdateWorkspaceMembersRoleOnyxD
import {buildOptimisticRecentlyUsedCurrencies, buildPolicyData, generatePolicyID} from './Policy/Policy';
import {buildOptimisticPolicyRecentlyUsedTags, getPolicyTagsData} from './Policy/Tag';
import type {GuidedSetupData} from './Report';
import {buildInviteToRoomOnyxData, completeOnboarding, getCurrentUserAccountID, notifyNewAction} from './Report';
import {buildInviteToRoomOnyxData, completeOnboarding, getCurrentUserAccountID, notifyNewAction, optimisticReportLastData} from './Report';
import {clearAllRelatedReportActionErrors} from './ReportActions';
import {getRecentWaypoints, sanitizeRecentWaypoints} from './Transaction';
import {removeDraftSplitTransaction, removeDraftTransaction, removeDraftTransactions} from './TransactionEdit';
Expand Down Expand Up @@ -8688,16 +8688,8 @@ function deleteMoneyRequest(
canUserPerformWriteAction = !!canUserPerformWriteActionReportUtils(chatReport, isChatIOUReportArchived);
}

const lastMessageText = getLastVisibleMessage(
iouReport?.chatReportID,
canUserPerformWriteAction,
reportPreviewAction?.reportActionID ? {[reportPreviewAction.reportActionID]: null} : {},
)?.lastMessageText;
const lastVisibleActionCreated = getLastVisibleAction(
iouReport?.chatReportID,
canUserPerformWriteAction,
reportPreviewAction?.reportActionID ? {[reportPreviewAction.reportActionID]: null} : {},
)?.created;
const optimisticReportActions = reportPreviewAction?.reportActionID ? {[reportPreviewAction.reportActionID]: null} : {};
const optimisticLastReportData = optimisticReportLastData(iouReport?.chatReportID ?? String(CONST.DEFAULT_NUMBER_ID), optimisticReportActions, canUserPerformWriteAction);

if (chatReport) {
optimisticData.push({
Expand All @@ -8706,8 +8698,7 @@ function deleteMoneyRequest(
value: {
hasOutstandingChildRequest: hasOutstandingChildRequest(chatReport, iouReport?.reportID),
iouReportID: null,
lastMessageText,
lastVisibleActionCreated,
...optimisticLastReportData,
},
});
}
Expand Down
70 changes: 35 additions & 35 deletions src/libs/actions/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1948,23 +1948,14 @@ function deleteReportComment(reportID: string | undefined, reportAction: ReportA

// If we are deleting the last visible message, let's find the previous visible one (or set an empty one if there are none) and update the lastMessageText in the LHN.
// Similarly, if we are deleting the last read comment we will want to update the lastVisibleActionCreated to use the previous visible message.
let optimisticReport: Partial<Report> = {
lastMessageText: '',
lastVisibleActionCreated: '',
};
const {lastMessageText = ''} = getLastVisibleMessage(originalReportID, isOriginalReportArchived, optimisticReportActions as ReportActions);
const report = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`];
const canUserPerformWriteAction = canUserPerformWriteActionReportUtils(report, isReportArchived);
if (lastMessageText) {
const lastVisibleAction = ReportActionsUtils.getLastVisibleAction(originalReportID, canUserPerformWriteAction, optimisticReportActions as ReportActions);
const lastVisibleActionCreated = lastVisibleAction?.created;
const lastActorAccountID = lastVisibleAction?.actorAccountID;
optimisticReport = {
lastMessageText,
lastVisibleActionCreated,
lastActorAccountID,
};
}
const optimisticLastReportData = optimisticReportLastData(originalReportID, optimisticReportActions as ReportActions, canUserPerformWriteAction, isOriginalReportArchived);

const optimisticReport: Partial<Report> = {
...optimisticLastReportData,
};

const didCommentMentionCurrentUser = ReportActionsUtils.didMessageMentionCurrentUser(reportAction);
if (didCommentMentionCurrentUser && reportAction.created === report?.lastMentionedTime) {
const reportActionsForReport = allReportActions?.[reportID];
Expand Down Expand Up @@ -4115,6 +4106,21 @@ function removeFromGroupChat(reportID: string, accountIDList: number[]) {
removeFromRoom(reportID, accountIDList);
}

function optimisticReportLastData(
reportID: string,
optimisticReportActions: Record<string, NullishDeep<ReportAction> | null> = {},
canUserPerformWriteAction?: boolean,
isReportArchived?: boolean,
) {
const lastMessageText = getLastVisibleMessage(reportID, isReportArchived, optimisticReportActions).lastMessageText ?? '';
const lastVisibleAction = ReportActionsUtils.getLastVisibleAction(reportID, canUserPerformWriteAction, optimisticReportActions);
return {
lastMessageText,
lastVisibleActionCreated: lastVisibleAction?.created ?? '',
lastActorAccountID: lastMessageText ? lastVisibleAction?.actorAccountID : CONST.DEFAULT_NUMBER_ID,
};
}

/** Flag a comment as offensive */
function flagComment(reportAction: OnyxEntry<ReportAction>, severity: string, originalReport: OnyxEntry<Report> | undefined, isOriginalReportArchived = false) {
const originalReportID = originalReport?.reportID;
Expand Down Expand Up @@ -4168,28 +4174,21 @@ function flagComment(reportAction: OnyxEntry<ReportAction>, severity: string, or
},
];

let optimisticReport: Partial<Report> = {
lastMessageText: '',
lastVisibleActionCreated: '',
};

const {lastMessageText = ''} = getLastVisibleMessage(originalReportID, isOriginalReportArchived, {
[reportActionID]: {...reportAction, message: [updatedMessage], pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE},
} as ReportActions);
const report = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${originalReportID}`];
const canUserPerformWriteAction = canUserPerformWriteActionReportUtils(report);

const canUserPerformWriteAction = canUserPerformWriteActionReportUtils(originalReport, isOriginalReportArchived);
if (lastMessageText) {
const lastVisibleAction = ReportActionsUtils.getLastVisibleAction(originalReportID, canUserPerformWriteAction, {
const optimisticLastReportData = optimisticReportLastData(
originalReportID ?? String(CONST.DEFAULT_NUMBER_ID),
{
[reportActionID]: {...reportAction, message: [updatedMessage], pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE},
} as ReportActions);
const lastVisibleActionCreated = lastVisibleAction?.created;
const lastActorAccountID = lastVisibleAction?.actorAccountID;
optimisticReport = {
lastMessageText,
lastVisibleActionCreated,
lastActorAccountID,
};
}
} as ReportActions,
canUserPerformWriteAction,
isOriginalReportArchived,
);

const optimisticReport: Partial<Report> = {
...optimisticLastReportData,
};

if (shouldHideMessage) {
optimisticData.push({
Expand Down Expand Up @@ -6271,5 +6270,6 @@ export {
removeFailedReport,
createTransactionThreadReport,
openUnreportedExpense,
optimisticReportLastData,
setOptimisticTransactionThread,
};
8 changes: 3 additions & 5 deletions src/libs/actions/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import type {ReportActions} from '@src/types/onyx/ReportAction';
import type ReportAction from '@src/types/onyx/ReportAction';
import type {OnyxData} from '@src/types/onyx/Request';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import {getMostRecentReportID, navigateToConciergeChatAndDeleteReport, notifyNewAction} from './Report';
import {getMostRecentReportID, navigateToConciergeChatAndDeleteReport, notifyNewAction, optimisticReportLastData} from './Report';
import {setSelfTourViewed} from './Welcome';

type OptimisticReport = Pick<OnyxTypes.Report, 'reportName' | 'managerID' | 'pendingFields' | 'participants'>;
Expand Down Expand Up @@ -1081,6 +1081,7 @@ function deleteTask(report: OnyxEntry<OnyxTypes.Report>, isReportArchived: boole
const optimisticReportActions = parentReportAction?.reportActionID ? {[parentReportAction?.reportActionID]: optimisticReportAction} : {};
const hasOutstandingChildTask = getOutstandingChildTask(report);

const optimisticLastReportData = optimisticReportLastData(parentReport?.reportID ?? String(CONST.DEFAULT_NUMBER_ID), optimisticReportActions as ReportActions, canUserPerformWriteAction);
const optimisticData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
Expand All @@ -1096,10 +1097,7 @@ function deleteTask(report: OnyxEntry<OnyxTypes.Report>, isReportArchived: boole
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${parentReport?.reportID}`,
value: {
lastMessageText:
ReportActionsUtils.getLastVisibleMessage(parentReport?.reportID, canUserPerformWriteAction, optimisticReportActions as OnyxTypes.ReportActions).lastMessageText ?? '',
lastVisibleActionCreated: ReportActionsUtils.getLastVisibleAction(parentReport?.reportID, canUserPerformWriteAction, optimisticReportActions as OnyxTypes.ReportActions)
?.created,
...optimisticLastReportData,
hasOutstandingChildTask,
},
},
Expand Down