@@ -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,
@@ -3202,45 +3202,25 @@ function getDeleteTrackExpenseInformation(
32023202 * This is needed when report totals change (e.g., adding expenses or changing reimbursable status)
32033203 * to ensure the report title reflects the updated values like {report:reimbursable}.
32043204 */
3205- function recalculateOptimisticReportName(iouReport: OnyxTypes.Report, policy: OnyxEntry<OnyxTypes.Policy>, newTransaction?: OnyxTypes.Transaction ): string | undefined {
3205+ function recalculateOptimisticReportName(iouReport: OnyxTypes.Report, policy: OnyxEntry<OnyxTypes.Policy>): string | undefined {
32063206 if (!policy?.fieldList?.[CONST.POLICY.FIELDS.FIELD_LIST_TITLE]) {
32073207 return undefined;
32083208 }
32093209 const titleFormula = policy.fieldList[CONST.POLICY.FIELDS.FIELD_LIST_TITLE]?.defaultValue ?? '';
32103210 if (!titleFormula) {
32113211 return undefined;
32123212 }
3213-
3214- // Gather existing transactions + the optimistic one not yet in Onyx.
3215- const existingTransactions = getReportTransactions(iouReport.reportID);
3216- const transactionsRecord: Record<string, OnyxTypes.Transaction> = {};
3217- for (const transaction of existingTransactions) {
3218- if (transaction?.transactionID) {
3219- transactionsRecord[transaction.transactionID] = transaction;
3220- }
3221- }
3222- if (newTransaction?.transactionID) {
3223- transactionsRecord[newTransaction.transactionID] = newTransaction;
3224- }
3225-
3226- const computedName = computeOptimisticReportName(iouReport, policy, iouReport.policyID, transactionsRecord);
3227- return computedName ?? undefined;
3213+ return populateOptimisticReportFormula(titleFormula, iouReport as Parameters<typeof populateOptimisticReportFormula>[1], policy);
32283214}
32293215
3230- function maybeUpdateReportNameForFormulaTitle(iouReport: OnyxTypes.Report, policy: OnyxEntry<OnyxTypes.Policy>, newTransaction?: OnyxTypes.Transaction ): OnyxTypes.Report {
3216+ function maybeUpdateReportNameForFormulaTitle(iouReport: OnyxTypes.Report, policy: OnyxEntry<OnyxTypes.Policy>): OnyxTypes.Report {
32313217 const reportNameValuePairs = allReportNameValuePairs?.[`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${iouReport.reportID}`];
32323218 const titleField = reportNameValuePairs?.expensify_text_title;
3233-
3234- // Fall back to policy.fieldList when reportNameValuePairs doesn't exist yet (optimistic reports).
3235- const isFormulaTitle = reportNameValuePairs
3236- ? titleField?.type === CONST.REPORT_FIELD_TYPES.FORMULA
3237- : policy?.fieldList?.[CONST.POLICY.FIELDS.FIELD_LIST_TITLE]?.type === CONST.REPORT_FIELD_TYPES.FORMULA;
3238-
3239- if (!isFormulaTitle) {
3219+ if (titleField?.type !== CONST.REPORT_FIELD_TYPES.FORMULA) {
32403220 return iouReport;
32413221 }
32423222
3243- const updatedReportName = recalculateOptimisticReportName(iouReport, policy, newTransaction );
3223+ const updatedReportName = recalculateOptimisticReportName(iouReport, policy);
32443224 if (!updatedReportName) {
32453225 return iouReport;
32463226 }
@@ -3424,6 +3404,8 @@ function getMoneyRequestInformation(moneyRequestInformation: MoneyRequestInforma
34243404 iouReport.nonReimbursableTotal = (iouReport.nonReimbursableTotal ?? 0) - amount;
34253405 }
34263406 }
3407+
3408+ iouReport = maybeUpdateReportNameForFormulaTitle(iouReport, policy);
34273409 }
34283410 if (typeof iouReport.unheldTotal === 'number') {
34293411 // Use newReportTotal in scenarios where the total is based on more than just the current transaction amount, and we need to override it manually
@@ -3525,11 +3507,6 @@ function getMoneyRequestInformation(moneyRequestInformation: MoneyRequestInforma
35253507 }
35263508 }
35273509
3528- // Recalculate report name after STEP 3 so the optimistic transaction is included in formula computation.
3529- if (!shouldCreateNewMoneyRequestReport && isPolicyExpenseChat) {
3530- iouReport = maybeUpdateReportNameForFormulaTitle(iouReport, policy, optimisticTransaction);
3531- }
3532-
35333510 // STEP 4: Build optimistic reportActions. We need:
35343511 // 1. CREATED action for the chatReport
35353512 // 2. CREATED action for the iouReport
0 commit comments