Skip to content

Commit 4ee43a3

Browse files
authored
Merge pull request Expensify#43531 from nkdengineer/fix/42472
fix: a 'new comment' when opening the one expense report for the first time
2 parents d4e82ac + 66eb796 commit 4ee43a3

File tree

1 file changed

+64
-64
lines changed

1 file changed

+64
-64
lines changed

src/pages/home/report/ReportActionsView.tsx

+64-64
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,72 @@ function ReportActionsView({
155155
// eslint-disable-next-line react-hooks/exhaustive-deps
156156
}, [route, isLoadingInitialReportActions, reportActionID]);
157157

158+
// When we are offline before opening an IOU/Expense report,
159+
// the total of the report and sometimes the expense aren't displayed because these actions aren't returned until `OpenReport` API is complete.
160+
// We generate a fake created action here if it doesn't exist to display the total whenever possible because the total just depends on report data
161+
// and we also generate an expense action if the number of expenses in allReportActions is less than the total number of expenses
162+
// to display at least one expense action to match the total data.
163+
const reportActionsToDisplay = useMemo(() => {
164+
if (!ReportUtils.isMoneyRequestReport(report) || !allReportActions.length) {
165+
return allReportActions;
166+
}
167+
168+
const actions = [...allReportActions];
169+
const lastAction = allReportActions[allReportActions.length - 1];
170+
171+
if (!ReportActionsUtils.isCreatedAction(lastAction)) {
172+
const optimisticCreatedAction = ReportUtils.buildOptimisticCreatedReportAction(String(report?.ownerAccountID), DateUtils.subtractMillisecondsFromDateTime(lastAction.created, 1));
173+
optimisticCreatedAction.pendingAction = null;
174+
actions.push(optimisticCreatedAction);
175+
}
176+
177+
const reportPreviewAction = ReportActionsUtils.getReportPreviewAction(report.chatReportID ?? '', report.reportID);
178+
const moneyRequestActions = allReportActions.filter((action) => {
179+
const originalMessage = ReportActionsUtils.isMoneyRequestAction(action) ? ReportActionsUtils.getOriginalMessage(action) : undefined;
180+
return (
181+
ReportActionsUtils.isMoneyRequestAction(action) &&
182+
originalMessage &&
183+
(originalMessage?.type === CONST.IOU.REPORT_ACTION_TYPE.CREATE ||
184+
!!(originalMessage?.type === CONST.IOU.REPORT_ACTION_TYPE.PAY && originalMessage?.IOUDetails) ||
185+
originalMessage?.type === CONST.IOU.REPORT_ACTION_TYPE.TRACK)
186+
);
187+
});
188+
189+
if (report.total && moneyRequestActions.length < (reportPreviewAction?.childMoneyRequestCount ?? 0) && isEmptyObject(transactionThreadReport)) {
190+
const optimisticIOUAction = ReportUtils.buildOptimisticIOUReportAction(
191+
CONST.IOU.REPORT_ACTION_TYPE.CREATE,
192+
0,
193+
CONST.CURRENCY.USD,
194+
'',
195+
[],
196+
NumberUtils.rand64(),
197+
undefined,
198+
report.reportID,
199+
false,
200+
false,
201+
{},
202+
false,
203+
DateUtils.subtractMillisecondsFromDateTime(actions[actions.length - 1].created, 1),
204+
) as OnyxTypes.ReportAction;
205+
moneyRequestActions.push(optimisticIOUAction);
206+
actions.splice(actions.length - 1, 0, optimisticIOUAction);
207+
}
208+
209+
// Update pending action of created action if we have some requests that are pending
210+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
211+
const createdAction = actions.pop()!;
212+
if (moneyRequestActions.filter((action) => !!action.pendingAction).length > 0) {
213+
createdAction.pendingAction = CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE;
214+
}
215+
216+
return [...actions, createdAction];
217+
}, [allReportActions, report, transactionThreadReport]);
218+
158219
// Get a sorted array of reportActions for both the current report and the transaction thread report associated with this report (if there is one)
159220
// so that we display transaction-level and report-level report actions in order in the one-transaction view
160221
const combinedReportActions = useMemo(
161-
() => ReportActionsUtils.getCombinedReportActions(allReportActions, transactionThreadReportID ?? null, transactionThreadReportActions),
162-
[allReportActions, transactionThreadReportActions, transactionThreadReportID],
222+
() => ReportActionsUtils.getCombinedReportActions(reportActionsToDisplay, transactionThreadReportID ?? null, transactionThreadReportActions),
223+
[reportActionsToDisplay, transactionThreadReportActions, transactionThreadReportID],
163224
);
164225

165226
const parentReportActionForTransactionThread = useMemo(
@@ -461,67 +522,6 @@ function ReportActionsView({
461522
};
462523
}, [isTheFirstReportActionIsLinked]);
463524

464-
// When we are offline before opening an IOU/Expense report,
465-
// the total of the report and sometimes the expense aren't displayed because these actions aren't returned until `OpenReport` API is complete.
466-
// We generate a fake created action here if it doesn't exist to display the total whenever possible because the total just depends on report data
467-
// and we also generate an expense action if the number of expenses in reportActions is less than the total number of expenses
468-
// to display at least one expense action to match the total data.
469-
const reportActionsToDisplay = useMemo(() => {
470-
if (!ReportUtils.isMoneyRequestReport(report) || !reportActions.length) {
471-
return reportActions;
472-
}
473-
474-
const actions = [...reportActions];
475-
const lastAction = reportActions[reportActions.length - 1];
476-
477-
if (!ReportActionsUtils.isCreatedAction(lastAction)) {
478-
const optimisticCreatedAction = ReportUtils.buildOptimisticCreatedReportAction(String(report?.ownerAccountID), DateUtils.subtractMillisecondsFromDateTime(lastAction.created, 1));
479-
optimisticCreatedAction.pendingAction = null;
480-
actions.push(optimisticCreatedAction);
481-
}
482-
483-
const reportPreviewAction = ReportActionsUtils.getReportPreviewAction(report.chatReportID ?? '-1', report.reportID);
484-
const moneyRequestActions = reportActions.filter((action) => {
485-
const originalMessage = ReportActionsUtils.isMoneyRequestAction(action) ? ReportActionsUtils.getOriginalMessage(action) : undefined;
486-
return (
487-
ReportActionsUtils.isMoneyRequestAction(action) &&
488-
originalMessage &&
489-
(originalMessage?.type === CONST.IOU.REPORT_ACTION_TYPE.CREATE ||
490-
!!(originalMessage?.type === CONST.IOU.REPORT_ACTION_TYPE.PAY && originalMessage?.IOUDetails) ||
491-
originalMessage?.type === CONST.IOU.REPORT_ACTION_TYPE.TRACK)
492-
);
493-
});
494-
495-
if (report.total && moneyRequestActions.length < (reportPreviewAction?.childMoneyRequestCount ?? 0) && isEmptyObject(transactionThreadReport)) {
496-
const optimisticIOUAction = ReportUtils.buildOptimisticIOUReportAction(
497-
CONST.IOU.REPORT_ACTION_TYPE.CREATE,
498-
0,
499-
CONST.CURRENCY.USD,
500-
'',
501-
[],
502-
NumberUtils.rand64(),
503-
undefined,
504-
report.reportID,
505-
false,
506-
false,
507-
{},
508-
false,
509-
DateUtils.subtractMillisecondsFromDateTime(actions[actions.length - 1].created, 1),
510-
) as OnyxTypes.ReportAction;
511-
moneyRequestActions.push(optimisticIOUAction);
512-
actions.splice(actions.length - 1, 0, optimisticIOUAction);
513-
}
514-
515-
// Update pending action of created action if we have some requests that are pending
516-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
517-
const createdAction = actions.pop()!;
518-
if (moneyRequestActions.filter((action) => !!action.pendingAction).length > 0) {
519-
createdAction.pendingAction = CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE;
520-
}
521-
522-
return [...actions, createdAction];
523-
}, [reportActions, report, transactionThreadReport]);
524-
525525
// Comments have not loaded at all yet do nothing
526526
if (!reportActions.length) {
527527
return null;
@@ -537,7 +537,7 @@ function ReportActionsView({
537537
parentReportAction={parentReportAction}
538538
parentReportActionForTransactionThread={parentReportActionForTransactionThread}
539539
onLayout={recordTimeToMeasureItemLayout}
540-
sortedReportActions={reportActionsToDisplay}
540+
sortedReportActions={reportActions}
541541
mostRecentIOUReportActionID={mostRecentIOUReportActionID}
542542
loadOlderChats={loadOlderChats}
543543
loadNewerChats={loadNewerChats}

0 commit comments

Comments
 (0)