@@ -138,7 +138,6 @@ import {
138138 canEditFieldOfMoneyRequest,
139139 canSubmitAndIsAwaitingForCurrentUser,
140140 canUserPerformWriteAction as canUserPerformWriteActionReportUtils,
141- computeOptimisticReportName,
142141 findSelfDMReportID,
143142 generateReportID,
144143 getAllHeldTransactions as getAllHeldTransactionsReportUtils,
@@ -189,6 +188,7 @@ import {
189188 isSettled,
190189 isTestTransactionReport,
191190 isTrackExpenseReport,
191+ populateOptimisticReportFormula,
192192 prepareOnboardingOnyxData,
193193 shouldCreateNewMoneyRequestReport as shouldCreateNewMoneyRequestReportReportUtils,
194194 shouldEnableNegative,
@@ -3192,45 +3192,25 @@ function getDeleteTrackExpenseInformation(
31923192 * This is needed when report totals change (e.g., adding expenses or changing reimbursable status)
31933193 * to ensure the report title reflects the updated values like {report:reimbursable}.
31943194 */
3195- function recalculateOptimisticReportName(iouReport: OnyxTypes.Report, policy: OnyxEntry<OnyxTypes.Policy>, newTransaction?: OnyxTypes.Transaction ): string | undefined {
3195+ function recalculateOptimisticReportName(iouReport: OnyxTypes.Report, policy: OnyxEntry<OnyxTypes.Policy>): string | undefined {
31963196 if (!policy?.fieldList?.[CONST.POLICY.FIELDS.FIELD_LIST_TITLE]) {
31973197 return undefined;
31983198 }
31993199 const titleFormula = policy.fieldList[CONST.POLICY.FIELDS.FIELD_LIST_TITLE]?.defaultValue ?? '';
32003200 if (!titleFormula) {
32013201 return undefined;
32023202 }
3203-
3204- // Gather existing transactions + the optimistic one not yet in Onyx.
3205- const existingTransactions = getReportTransactions(iouReport.reportID);
3206- const transactionsRecord: Record<string, OnyxTypes.Transaction> = {};
3207- for (const transaction of existingTransactions) {
3208- if (transaction?.transactionID) {
3209- transactionsRecord[transaction.transactionID] = transaction;
3210- }
3211- }
3212- if (newTransaction?.transactionID) {
3213- transactionsRecord[newTransaction.transactionID] = newTransaction;
3214- }
3215-
3216- const computedName = computeOptimisticReportName(iouReport, policy, iouReport.policyID, transactionsRecord);
3217- return computedName ?? undefined;
3203+ return populateOptimisticReportFormula(titleFormula, iouReport as Parameters<typeof populateOptimisticReportFormula>[1], policy);
32183204}
32193205
3220- function maybeUpdateReportNameForFormulaTitle(iouReport: OnyxTypes.Report, policy: OnyxEntry<OnyxTypes.Policy>, newTransaction?: OnyxTypes.Transaction ): OnyxTypes.Report {
3206+ function maybeUpdateReportNameForFormulaTitle(iouReport: OnyxTypes.Report, policy: OnyxEntry<OnyxTypes.Policy>): OnyxTypes.Report {
32213207 const reportNameValuePairs = allReportNameValuePairs?.[`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${iouReport.reportID}`];
32223208 const titleField = reportNameValuePairs?.expensify_text_title;
3223-
3224- // Fall back to policy.fieldList when reportNameValuePairs doesn't exist yet (optimistic reports).
3225- const isFormulaTitle = reportNameValuePairs
3226- ? titleField?.type === CONST.REPORT_FIELD_TYPES.FORMULA
3227- : policy?.fieldList?.[CONST.POLICY.FIELDS.FIELD_LIST_TITLE]?.type === CONST.REPORT_FIELD_TYPES.FORMULA;
3228-
3229- if (!isFormulaTitle) {
3209+ if (titleField?.type !== CONST.REPORT_FIELD_TYPES.FORMULA) {
32303210 return iouReport;
32313211 }
32323212
3233- const updatedReportName = recalculateOptimisticReportName(iouReport, policy, newTransaction );
3213+ const updatedReportName = recalculateOptimisticReportName(iouReport, policy);
32343214 if (!updatedReportName) {
32353215 return iouReport;
32363216 }
@@ -3414,6 +3394,8 @@ function getMoneyRequestInformation(moneyRequestInformation: MoneyRequestInforma
34143394 iouReport.nonReimbursableTotal = (iouReport.nonReimbursableTotal ?? 0) - amount;
34153395 }
34163396 }
3397+
3398+ iouReport = maybeUpdateReportNameForFormulaTitle(iouReport, policy);
34173399 }
34183400 if (typeof iouReport.unheldTotal === 'number') {
34193401 // Use newReportTotal in scenarios where the total is based on more than just the current transaction amount, and we need to override it manually
@@ -3515,11 +3497,6 @@ function getMoneyRequestInformation(moneyRequestInformation: MoneyRequestInforma
35153497 }
35163498 }
35173499
3518- // Recalculate report name after STEP 3 so the optimistic transaction is included in formula computation.
3519- if (!shouldCreateNewMoneyRequestReport && isPolicyExpenseChat) {
3520- iouReport = maybeUpdateReportNameForFormulaTitle(iouReport, policy, optimisticTransaction);
3521- }
3522-
35233500 // STEP 4: Build optimistic reportActions. We need:
35243501 // 1. CREATED action for the chatReport
35253502 // 2. CREATED action for the iouReport
0 commit comments