From 7acf8f9777730836ecf82820150bef27ffb9084e Mon Sep 17 00:00:00 2001 From: "Marc Glasser (via MelvinBot)" Date: Thu, 14 May 2026 18:20:58 +0000 Subject: [PATCH 1/8] Fix bulk expense edit performance hang on large reports Replace createTransactionThreadReport (which fires OpenReport API calls) in the bulk edit loop with inline optimistic thread creation using buildTransactionThread + generateReportID. This eliminates up to N redundant OpenReport network requests when editing N expenses. Add a loading spinner on the Save button with requestAnimationFrame deferral so the spinner paints before the synchronous Onyx writes run. Co-authored-by: Marc Glasser --- src/libs/actions/IOU/BulkEdit.ts | 45 +++++++------ .../SearchEditMultiplePage.tsx | 57 ++++++++-------- tests/actions/IOUTest/BulkEditTest.ts | 66 ------------------- 3 files changed, 57 insertions(+), 111 deletions(-) diff --git a/src/libs/actions/IOU/BulkEdit.ts b/src/libs/actions/IOU/BulkEdit.ts index 8a5291fce22d..2905df13f93f 100644 --- a/src/libs/actions/IOU/BulkEdit.ts +++ b/src/libs/actions/IOU/BulkEdit.ts @@ -12,8 +12,10 @@ import type {TransactionDetails} from '@libs/ReportUtils'; import { buildOptimisticCreatedReportAction, buildOptimisticModifiedExpenseReportAction, + buildTransactionThread, canEditFieldOfMoneyRequest, findSelfDMReportID, + generateReportID, getOutstandingChildRequest, getParsedComment, getTransactionDetails, @@ -24,7 +26,6 @@ import { } from '@libs/ReportUtils'; import {calculateTaxAmount, getAmount, getClearedPendingFields, getCurrency, getTaxValue, getUpdatedTransaction, isOnHold, isSplitChildTransaction} from '@libs/TransactionUtils'; import ViolationsUtils from '@libs/Violations/ViolationsUtils'; -import {createTransactionThreadReport} from '@userActions/Report'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type * as OnyxTypes from '@src/types/onyx'; @@ -79,9 +80,6 @@ type UpdateMultipleMoneyRequestsParams = { policyTags: OnyxCollection; hash?: number; allPolicies?: OnyxCollection; - introSelected: OnyxEntry; - betas: OnyxEntry; - currentUserLogin: string; currentUserAccountID: number; }; @@ -96,10 +94,7 @@ function updateMultipleMoneyRequests({ policyTags, hash, allPolicies, - introSelected, - betas, currentUserAccountID, - currentUserLogin, }: UpdateMultipleMoneyRequestsParams) { // Track running totals per report so multiple edits in the same report compound correctly. const optimisticReportsByID: Record = {}; @@ -134,19 +129,16 @@ function updateMultipleMoneyRequests({ let transactionThread = reports?.[`${ONYXKEYS.COLLECTION.REPORT}${transactionThreadReportID}`] ?? null; // Offline-created expenses can be missing a transaction thread until it's opened once. - // Ensure the thread exists before adding optimistic MODIFIED_EXPENSE actions so + // Ensure the thread exists locally before adding optimistic MODIFIED_EXPENSE actions so // bulk-edit comments are visible immediately while still offline. + // We intentionally avoid calling createTransactionThreadReport here because it fires + // an OpenReport API command per expense, which floods the queue and hangs the UI on + // large reports (40-50+ expenses). The backend already creates the transaction thread + // when processing UpdateMoneyRequest, so we only need local Onyx state. let didCreateThreadInThisIteration = false; if (!transactionThreadReportID && iouReport?.reportID) { - const optimisticTransactionThread = createTransactionThreadReport({ - introSelected, - currentUserLogin, - currentUserAccountID, - betas, - iouReport, - iouReportAction: reportAction, - transaction, - }); + const optimisticTransactionThreadReportID = generateReportID(); + const optimisticTransactionThread = buildTransactionThread(reportAction, iouReport, currentUserAccountID, undefined, optimisticTransactionThreadReportID); if (optimisticTransactionThread?.reportID) { transactionThreadReportID = optimisticTransactionThread.reportID; transactionThread = optimisticTransactionThread; @@ -294,6 +286,22 @@ function updateMultipleMoneyRequests({ const snapshotSuccessData: Array> = []; const snapshotFailureData: Array> = []; + // If we created the transaction thread optimistically above, seed it into Onyx + // so the MODIFIED_EXPENSE action has somewhere to land. On success the server's + // OpenReport data (triggered by UpdateMoneyRequest) will overwrite these values. + if (didCreateThreadInThisIteration && transactionThread && transactionThreadReportID) { + optimisticData.push({ + onyxMethod: Onyx.METHOD.SET, + key: `${ONYXKEYS.COLLECTION.REPORT}${transactionThreadReportID}`, + value: transactionThread, + }); + failureData.push({ + onyxMethod: Onyx.METHOD.SET, + key: `${ONYXKEYS.COLLECTION.REPORT}${transactionThreadReportID}`, + value: null, + }); + } + // Pending fields for the transaction const pendingFields: OnyxTypes.Transaction['pendingFields'] = Object.fromEntries(Object.keys(transactionChanges).map((field) => [field, CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE])); const clearedPendingFields = getClearedPendingFields(transactionChanges); @@ -457,9 +465,8 @@ function updateMultipleMoneyRequests({ if (transactionThreadReportID) { // Backfill a CREATED action for threads never opened locally so // MoneyRequestView renders and the skeleton doesn't loop offline. - // Skip when the thread was just created above (openReport handles it). const threadReportActions = reportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThreadReportID}`] ?? {}; - const hasCreatedAction = didCreateThreadInThisIteration || Object.values(threadReportActions).some((action) => action?.actionName === CONST.REPORT.ACTIONS.TYPE.CREATED); + const hasCreatedAction = Object.values(threadReportActions).some((action) => action?.actionName === CONST.REPORT.ACTIONS.TYPE.CREATED); const optimisticCreatedValue: Record> = {}; if (!hasCreatedAction) { const optimisticCreatedAction = buildOptimisticCreatedReportAction({emailCreatingAction: CONST.REPORT.OWNER_EMAIL_FAKE}); diff --git a/src/pages/Search/SearchEditMultiple/SearchEditMultiplePage.tsx b/src/pages/Search/SearchEditMultiple/SearchEditMultiplePage.tsx index f370f12dcd09..875c3d76a244 100644 --- a/src/pages/Search/SearchEditMultiple/SearchEditMultiplePage.tsx +++ b/src/pages/Search/SearchEditMultiple/SearchEditMultiplePage.tsx @@ -1,4 +1,4 @@ -import React, {useEffect} from 'react'; +import React, {useEffect, useState} from 'react'; import {View} from 'react-native'; import type {ValueOf} from 'type-fest'; import Button from '@components/Button'; @@ -42,7 +42,7 @@ function SearchEditMultiplePage() { const styles = useThemeStyles(); const {currentSearchHash, currentSearchResults} = useSearchStateContext(); const {clearSelectedTransactions} = useSearchActionsContext(); - const {login: currentUserLogin, accountID: currentUserAccountID} = useCurrentUserPersonalDetails(); + const {accountID: currentUserAccountID} = useCurrentUserPersonalDetails(); const [policies] = useOnyx(ONYXKEYS.COLLECTION.POLICY); const [activePolicyID] = useOnyx(ONYXKEYS.NVP_ACTIVE_POLICY_ID); const [draftTransaction] = useOnyx(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${CONST.IOU.OPTIMISTIC_BULK_EDIT_TRANSACTION_ID}`); @@ -51,8 +51,6 @@ function SearchEditMultiplePage() { const [allReportActions] = useOnyx(ONYXKEYS.COLLECTION.REPORT_ACTIONS); const [allPolicyTags] = useOnyx(ONYXKEYS.COLLECTION.POLICY_TAGS); const [allPolicyCategories] = useOnyx(ONYXKEYS.COLLECTION.POLICY_CATEGORIES); - const [betas] = useOnyx(ONYXKEYS.BETAS); - const [introSelected] = useOnyx(ONYXKEYS.NVP_INTRO_SELECTED); const snapshotData = currentSearchResults?.data; const mergedTransactions = withSnapshotTransactions(allTransactions, snapshotData); @@ -124,8 +122,10 @@ function SearchEditMultiplePage() { }; }, []); + const [isSaving, setIsSaving] = useState(false); + const save = () => { - if (!draftTransaction) { + if (!draftTransaction || isSaving) { return; } @@ -163,28 +163,31 @@ function SearchEditMultiplePage() { return; } - updateMultipleMoneyRequests({ - transactionIDs: selectedTransactionIDs, - changes, - policy, - reports: mergedReports, - transactions: mergedTransactions, - reportActions: mergedReportActions, - policyCategories: allPolicyCategories, - policyTags: allPolicyTags, - hash: currentSearchHash, - allPolicies: policies, - introSelected, - betas, - currentUserAccountID, - currentUserLogin: currentUserLogin ?? '', - }); - // Bulk edit can start from report (ID-based selection) or search (map-based selection), - // so clear both stores to keep deselection behavior consistent. - clearSelectedTransactions(true); - clearSelectedTransactions(); + setIsSaving(true); + + // Defer the bulk edit loop so the loading spinner has a chance to paint + // before the synchronous Onyx writes block the JS thread. + requestAnimationFrame(() => { + updateMultipleMoneyRequests({ + transactionIDs: selectedTransactionIDs, + changes, + policy, + reports: mergedReports, + transactions: mergedTransactions, + reportActions: mergedReportActions, + policyCategories: allPolicyCategories, + policyTags: allPolicyTags, + hash: currentSearchHash, + allPolicies: policies, + currentUserAccountID, + }); + // Bulk edit can start from report (ID-based selection) or search (map-based selection), + // so clear both stores to keep deselection behavior consistent. + clearSelectedTransactions(true); + clearSelectedTransactions(); - Navigation.dismissToPreviousRHP(); + Navigation.dismissToPreviousRHP(); + }); }; const currency = policy?.outputCurrency ?? CONST.CURRENCY.USD; @@ -320,6 +323,8 @@ function SearchEditMultiplePage() { large text={translate('common.save')} onPress={save} + isLoading={isSaving} + isDisabled={isSaving} style={[styles.m5]} /> diff --git a/tests/actions/IOUTest/BulkEditTest.ts b/tests/actions/IOUTest/BulkEditTest.ts index 589535d7d80c..24caf81548b0 100644 --- a/tests/actions/IOUTest/BulkEditTest.ts +++ b/tests/actions/IOUTest/BulkEditTest.ts @@ -68,9 +68,6 @@ describe('actions/IOU/BulkEdit', () => { policyCategories: undefined, policyTags: {}, hash: undefined, - introSelected: undefined, - betas: undefined, - currentUserLogin: RORY_EMAIL, currentUserAccountID: RORY_ACCOUNT_ID, }); @@ -141,9 +138,6 @@ describe('actions/IOU/BulkEdit', () => { policyCategories: undefined, policyTags: {}, hash: undefined, - introSelected: undefined, - betas: undefined, - currentUserLogin: RORY_EMAIL, currentUserAccountID: RORY_ACCOUNT_ID, }); @@ -203,9 +197,6 @@ describe('actions/IOU/BulkEdit', () => { policyCategories: undefined, policyTags: {}, hash: undefined, - introSelected: undefined, - betas: undefined, - currentUserLogin: RORY_EMAIL, currentUserAccountID: RORY_ACCOUNT_ID, }); @@ -274,9 +265,6 @@ describe('actions/IOU/BulkEdit', () => { policyCategories: undefined, policyTags: {}, hash: undefined, - introSelected: undefined, - betas: undefined, - currentUserLogin: RORY_EMAIL, currentUserAccountID: RORY_ACCOUNT_ID, }); @@ -349,9 +337,6 @@ describe('actions/IOU/BulkEdit', () => { policyCategories: undefined, policyTags: {}, hash: undefined, - introSelected: undefined, - betas: undefined, - currentUserLogin: RORY_EMAIL, currentUserAccountID: RORY_ACCOUNT_ID, }); @@ -415,9 +400,6 @@ describe('actions/IOU/BulkEdit', () => { policyCategories: undefined, policyTags: {}, hash: undefined, - introSelected: undefined, - betas: undefined, - currentUserLogin: RORY_EMAIL, currentUserAccountID: RORY_ACCOUNT_ID, }); @@ -479,9 +461,6 @@ describe('actions/IOU/BulkEdit', () => { policyCategories: undefined, policyTags: {}, hash: undefined, - introSelected: undefined, - betas: undefined, - currentUserLogin: RORY_EMAIL, currentUserAccountID: RORY_ACCOUNT_ID, }); @@ -550,9 +529,6 @@ describe('actions/IOU/BulkEdit', () => { }, }, hash: undefined, - introSelected: undefined, - betas: undefined, - currentUserLogin: RORY_EMAIL, currentUserAccountID: RORY_ACCOUNT_ID, }); await waitForBatchedUpdates(); @@ -606,9 +582,6 @@ describe('actions/IOU/BulkEdit', () => { policyCategories: undefined, policyTags: {}, hash: undefined, - introSelected: undefined, - betas: undefined, - currentUserLogin: RORY_EMAIL, currentUserAccountID: RORY_ACCOUNT_ID, }); await waitForBatchedUpdates(); @@ -665,9 +638,6 @@ describe('actions/IOU/BulkEdit', () => { policyCategories: undefined, policyTags: {}, hash: undefined, - introSelected: undefined, - betas: undefined, - currentUserLogin: RORY_EMAIL, currentUserAccountID: RORY_ACCOUNT_ID, }); await waitForBatchedUpdates(); @@ -731,9 +701,6 @@ describe('actions/IOU/BulkEdit', () => { }, policyTags: {}, hash: undefined, - introSelected: undefined, - betas: undefined, - currentUserLogin: RORY_EMAIL, currentUserAccountID: RORY_ACCOUNT_ID, }); await waitForBatchedUpdates(); @@ -798,9 +765,6 @@ describe('actions/IOU/BulkEdit', () => { allPolicies: { [`${ONYXKEYS.COLLECTION.POLICY}${transactionPolicyID}`]: transactionPolicy, }, - introSelected: undefined, - betas: undefined, - currentUserLogin: RORY_EMAIL, currentUserAccountID: RORY_ACCOUNT_ID, }); await waitForBatchedUpdates(); @@ -876,9 +840,6 @@ describe('actions/IOU/BulkEdit', () => { allPolicies: { [`${ONYXKEYS.COLLECTION.POLICY}${transactionPolicyID}`]: txPolicy, }, - introSelected: undefined, - betas: undefined, - currentUserLogin: RORY_EMAIL, currentUserAccountID: RORY_ACCOUNT_ID, }); await waitForBatchedUpdates(); @@ -942,9 +903,6 @@ describe('actions/IOU/BulkEdit', () => { }, policyTags: {}, hash: undefined, - introSelected: undefined, - betas: undefined, - currentUserLogin: RORY_EMAIL, currentUserAccountID: RORY_ACCOUNT_ID, }); await waitForBatchedUpdates(); @@ -1020,9 +978,6 @@ describe('actions/IOU/BulkEdit', () => { [`${ONYXKEYS.COLLECTION.POLICY_TAGS}${policy.id}`]: policyTagsForPolicy, }, hash: undefined, - introSelected: undefined, - betas: undefined, - currentUserLogin: RORY_EMAIL, currentUserAccountID: RORY_ACCOUNT_ID, }); await waitForBatchedUpdates(); @@ -1079,9 +1034,6 @@ describe('actions/IOU/BulkEdit', () => { policyCategories: undefined, policyTags: {}, hash: undefined, - introSelected: undefined, - betas: undefined, - currentUserLogin: RORY_EMAIL, currentUserAccountID: RORY_ACCOUNT_ID, }); @@ -1145,9 +1097,6 @@ describe('actions/IOU/BulkEdit', () => { policyCategories: undefined, policyTags: {}, hash: undefined, - introSelected: undefined, - betas: undefined, - currentUserLogin: 'test@example.com', currentUserAccountID: 1, }); @@ -1235,9 +1184,6 @@ describe('actions/IOU/BulkEdit', () => { policyTags: {}, hash: undefined, allPolicies, - introSelected: undefined, - betas: undefined, - currentUserLogin: RORY_EMAIL, currentUserAccountID: RORY_ACCOUNT_ID, }); @@ -1327,9 +1273,6 @@ describe('actions/IOU/BulkEdit', () => { policyTags: {}, hash: undefined, allPolicies, - introSelected: undefined, - betas: undefined, - currentUserLogin: RORY_EMAIL, currentUserAccountID: RORY_ACCOUNT_ID, }); @@ -1397,9 +1340,6 @@ describe('actions/IOU/BulkEdit', () => { policyCategories: undefined, policyTags: {}, hash: undefined, - introSelected: undefined, - betas: undefined, - currentUserLogin: RORY_EMAIL, currentUserAccountID: RORY_ACCOUNT_ID, }); @@ -1466,9 +1406,6 @@ describe('actions/IOU/BulkEdit', () => { policyCategories: undefined, policyTags: {}, hash: undefined, - introSelected: undefined, - betas: undefined, - currentUserLogin: RORY_EMAIL, currentUserAccountID: RORY_ACCOUNT_ID, }); @@ -1531,9 +1468,6 @@ describe('actions/IOU/BulkEdit', () => { policyCategories: undefined, policyTags: {}, hash: undefined, - introSelected: undefined, - betas: undefined, - currentUserLogin: RORY_EMAIL, currentUserAccountID: RORY_ACCOUNT_ID, }); From cc7f31974a2410aae7f86b9036ca5d14d74a7428 Mon Sep 17 00:00:00 2001 From: "Marc Glasser (via MelvinBot)" Date: Thu, 14 May 2026 18:35:54 +0000 Subject: [PATCH 2/8] Remove unused RORY_EMAIL variable in BulkEditTest Co-authored-by: Marc Glasser --- tests/actions/IOUTest/BulkEditTest.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/actions/IOUTest/BulkEditTest.ts b/tests/actions/IOUTest/BulkEditTest.ts index 24caf81548b0..7f636a16d9c0 100644 --- a/tests/actions/IOUTest/BulkEditTest.ts +++ b/tests/actions/IOUTest/BulkEditTest.ts @@ -12,7 +12,6 @@ import createRandomTransaction from '../../utils/collections/transaction'; import getOnyxValue from '../../utils/getOnyxValue'; import waitForBatchedUpdates from '../../utils/waitForBatchedUpdates'; -const RORY_EMAIL = 'rory@expensifail.com'; const RORY_ACCOUNT_ID = 3; describe('actions/IOU/BulkEdit', () => { From 248ea3dcf2d2889325bf94365691006961e2ee39 Mon Sep 17 00:00:00 2001 From: "Marc Glasser (via MelvinBot)" Date: Fri, 15 May 2026 17:21:05 +0000 Subject: [PATCH 3/8] Link optimistic transaction thread back to parent IOU action and transaction When creating a thread optimistically during bulk edit, set childReportID on the parent IOU report action and transactionThreadReportID on the transaction. Without this linkage, subsequent offline edits of the same expense would generate a duplicate thread each time because the lookup at line 128 would again find both fields undefined. Co-authored-by: Marc Glasser --- src/libs/actions/IOU/BulkEdit.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/libs/actions/IOU/BulkEdit.ts b/src/libs/actions/IOU/BulkEdit.ts index 2905df13f93f..151b8ac7256e 100644 --- a/src/libs/actions/IOU/BulkEdit.ts +++ b/src/libs/actions/IOU/BulkEdit.ts @@ -289,12 +289,28 @@ function updateMultipleMoneyRequests({ // If we created the transaction thread optimistically above, seed it into Onyx // so the MODIFIED_EXPENSE action has somewhere to land. On success the server's // OpenReport data (triggered by UpdateMoneyRequest) will overwrite these values. + // Also link the thread back: set childReportID on the parent IOU action and + // transactionThreadReportID on the transaction so subsequent offline edits of the + // same expense reuse this thread instead of generating a new one each time. if (didCreateThreadInThisIteration && transactionThread && transactionThreadReportID) { optimisticData.push({ onyxMethod: Onyx.METHOD.SET, key: `${ONYXKEYS.COLLECTION.REPORT}${transactionThreadReportID}`, value: transactionThread, }); + // Link childReportID on the parent IOU report action + if (reportAction?.reportActionID && iouReport?.reportID) { + optimisticData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`, + value: {[reportAction.reportActionID]: {childReportID: transactionThreadReportID}}, + }); + failureData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`, + value: {[reportAction.reportActionID]: {childReportID: null}}, + }); + } failureData.push({ onyxMethod: Onyx.METHOD.SET, key: `${ONYXKEYS.COLLECTION.REPORT}${transactionThreadReportID}`, @@ -363,6 +379,9 @@ function updateMultipleMoneyRequests({ pendingFields, isLoading: false, errorFields: null, + // Link the optimistic thread back to the transaction so subsequent + // offline edits reuse it instead of generating a new thread each time. + ...(didCreateThreadInThisIteration && transactionThreadReportID ? {transactionThreadReportID} : {}), }, }); From efc4f0230226ffefd4ca77b5ce16c2ff294e1f58 Mon Sep 17 00:00:00 2001 From: "{\"message\":\"Not Found\",\"documentation_url\":\"https://docs.github.com/rest/issues/comments#get-an-issue-comment\",\"status\":\"404\"} (via MelvinBot)" Date: Sat, 16 May 2026 15:51:37 +0000 Subject: [PATCH 4/8] Prioritize reportAction.childReportID over transaction.transactionThreadReportID reportAction.childReportID is the server-sourced truth, updated when online, while transaction.transactionThreadReportID is only locally present. Swapping the priority prevents stale local IDs from winning in Offline->BulkEdit->Online->Offline->BulkEdit flows. Co-authored-by: {"message":"Not Found","documentation_url":"https://docs.github.com/rest/issues/comments#get-an-issue-comment","status":"404"} <{"message":"Not Found","documentation_url":"https://docs.github.com/rest/issues/comments#get-an-issue-comment","status":"404"}@users.noreply.github.com> --- src/libs/actions/IOU/BulkEdit.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/IOU/BulkEdit.ts b/src/libs/actions/IOU/BulkEdit.ts index 151b8ac7256e..f69b018f59c6 100644 --- a/src/libs/actions/IOU/BulkEdit.ts +++ b/src/libs/actions/IOU/BulkEdit.ts @@ -125,7 +125,7 @@ function updateMultipleMoneyRequests({ } } - let transactionThreadReportID = transaction.transactionThreadReportID ?? reportAction?.childReportID; + let transactionThreadReportID = reportAction?.childReportID ?? transaction.transactionThreadReportID; let transactionThread = reports?.[`${ONYXKEYS.COLLECTION.REPORT}${transactionThreadReportID}`] ?? null; // Offline-created expenses can be missing a transaction thread until it's opened once. From 1e161d66f16670f1270316383cd785f8c172ead6 Mon Sep 17 00:00:00 2001 From: "Roji Philip (via MelvinBot)" Date: Tue, 26 May 2026 03:01:23 +0000 Subject: [PATCH 5/8] Remove unused betas, introSelected, currentUserLogin from updateMultipleMoneyRequests call Co-authored-by: Roji Philip --- .../Search/SearchEditMultiple/SearchEditMultiplePage.tsx | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/pages/Search/SearchEditMultiple/SearchEditMultiplePage.tsx b/src/pages/Search/SearchEditMultiple/SearchEditMultiplePage.tsx index 6a73ded595b1..92830da258eb 100644 --- a/src/pages/Search/SearchEditMultiple/SearchEditMultiplePage.tsx +++ b/src/pages/Search/SearchEditMultiple/SearchEditMultiplePage.tsx @@ -44,7 +44,7 @@ function SearchEditMultiplePage() { const {currentSearchHash} = useSearchQueryContext(); const {currentSearchResults} = useSearchResultsContext(); const {clearSelectedTransactions} = useSearchSelectionActions(); - const {login: currentUserLogin, accountID: currentUserAccountID} = useCurrentUserPersonalDetails(); + const {accountID: currentUserAccountID} = useCurrentUserPersonalDetails(); const delegateAccountID = useDelegateAccountID(); const [policies] = useOnyx(ONYXKEYS.COLLECTION.POLICY); const [activePolicyID] = useOnyx(ONYXKEYS.NVP_ACTIVE_POLICY_ID); @@ -54,8 +54,6 @@ function SearchEditMultiplePage() { const [allReportActions] = useOnyx(ONYXKEYS.COLLECTION.REPORT_ACTIONS); const [allPolicyTags] = useOnyx(ONYXKEYS.COLLECTION.POLICY_TAGS); const [allPolicyCategories] = useOnyx(ONYXKEYS.COLLECTION.POLICY_CATEGORIES); - const [betas] = useOnyx(ONYXKEYS.BETAS); - const [introSelected] = useOnyx(ONYXKEYS.NVP_INTRO_SELECTED); const snapshotData = currentSearchResults?.data; const mergedTransactions = withSnapshotTransactions(allTransactions, snapshotData); @@ -184,10 +182,7 @@ function SearchEditMultiplePage() { policyTags: allPolicyTags, hash: currentSearchHash, allPolicies: policies, - introSelected, - betas, currentUserAccountID, - currentUserLogin: currentUserLogin ?? '', delegateAccountID, }); // Bulk edit can start from report (ID-based selection) or search (map-based selection), From 03db3fc043eaac26f5026de17617fcf537f3ac06 Mon Sep 17 00:00:00 2001 From: "Roji Philip (via MelvinBot)" Date: Tue, 26 May 2026 17:14:06 +0000 Subject: [PATCH 6/8] Clear transactionThreadReportID in failure data when optimistically created Since transactionThreadReportID is optimistically added to the transaction on bulk edit, it needs to be explicitly cleared on failure so a stale thread reference doesn't persist when the server never created the thread. Co-authored-by: Roji Philip --- src/libs/actions/IOU/BulkEdit.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/libs/actions/IOU/BulkEdit.ts b/src/libs/actions/IOU/BulkEdit.ts index d3b8053d8e5f..7932d6c0e6aa 100644 --- a/src/libs/actions/IOU/BulkEdit.ts +++ b/src/libs/actions/IOU/BulkEdit.ts @@ -554,6 +554,9 @@ function updateMultipleMoneyRequests({ ...transaction, pendingFields: clearedPendingFields, errorFields, + // Clear the optimistically added transactionThreadReportID so it doesn't + // persist after a failed request — the server never created this thread. + ...(didCreateThreadInThisIteration && transactionThreadReportID ? {transactionThreadReportID: null} : {}), }, }); From 01c59815d163607660b9802bbddd22740efabefd Mon Sep 17 00:00:00 2001 From: "Roji Philip (via MelvinBot)" Date: Tue, 26 May 2026 17:50:03 +0000 Subject: [PATCH 7/8] Add tests for optimistic thread creation and childReportID priority - Test that an optimistic transaction thread is created when neither childReportID nor transactionThreadReportID exists, and the thread is linked back to the transaction via transactionThreadReportID. - Test that childReportID on the report action takes priority over transactionThreadReportID on the transaction when resolving which thread to use. Co-authored-by: Roji Philip --- tests/actions/IOUTest/BulkEditTest.ts | 182 ++++++++++++++++++++++++++ 1 file changed, 182 insertions(+) diff --git a/tests/actions/IOUTest/BulkEditTest.ts b/tests/actions/IOUTest/BulkEditTest.ts index 40de85eb1b6b..36fefa7e0c70 100644 --- a/tests/actions/IOUTest/BulkEditTest.ts +++ b/tests/actions/IOUTest/BulkEditTest.ts @@ -1505,6 +1505,188 @@ describe('actions/IOU/BulkEdit', () => { writeSpy.mockRestore(); canEditFieldSpy.mockRestore(); }); + + it('creates an optimistic transaction thread when neither childReportID nor transactionThreadReportID exists', () => { + const transactionID = 'transaction-no-thread'; + const iouReportID = 'iou-no-thread'; + const policy = createRandomPolicy(20, CONST.POLICY.TYPE.TEAM); + + const iouReport: Report = { + ...createRandomReport(20, undefined), + reportID: iouReportID, + policyID: policy.id, + type: CONST.REPORT.TYPE.EXPENSE, + }; + + const reports = { + [`${ONYXKEYS.COLLECTION.REPORT}${iouReportID}`]: iouReport, + }; + + // Transaction has no transactionThreadReportID + const transaction: Transaction = { + ...createRandomTransaction(20), + transactionID, + reportID: iouReportID, + amount: 500, + currency: CONST.CURRENCY.USD, + }; + delete (transaction as Partial).transactionThreadReportID; + const transactions = { + [`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`]: transaction, + }; + + const canEditFieldSpy = jest.spyOn(require('@libs/ReportUtils'), 'canEditFieldOfMoneyRequest').mockReturnValue(true); + // eslint-disable-next-line rulesdir/no-multiple-api-calls + const writeSpy = jest.spyOn(API, 'write').mockImplementation(jest.fn()); + + updateMultipleMoneyRequests({ + transactionIDs: [transactionID], + changes: {merchant: 'Coffee Shop'}, + policy, + reports, + transactions, + reportActions: {}, + policyCategories: undefined, + policyTags: {}, + hash: undefined, + currentUserAccountID: RORY_ACCOUNT_ID, + delegateAccountID: undefined, + }); + + expect(writeSpy).toHaveBeenCalled(); + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const onyxData = writeSpy.mock.calls.at(0)?.[2] as any; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const optimisticData = onyxData?.optimisticData as any[]; + + // An optimistic thread report should be created via SET + const optimisticReportSet = optimisticData.find( + // eslint-disable-next-line @typescript-eslint/no-explicit-any + (entry: any) => String(entry.key).startsWith(ONYXKEYS.COLLECTION.REPORT) && entry.onyxMethod === 'set' && entry.key !== `${ONYXKEYS.COLLECTION.REPORT}${iouReportID}`, + ); + expect(optimisticReportSet).toBeDefined(); + const optimisticThreadReportID = String(optimisticReportSet.key).replace(ONYXKEYS.COLLECTION.REPORT, ''); + + // The transaction optimistic data should link back to the new thread via transactionThreadReportID + const transactionMerge = optimisticData.find( + // eslint-disable-next-line @typescript-eslint/no-explicit-any + (entry: any) => entry.key === `${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, + ); + expect(transactionMerge?.value?.transactionThreadReportID).toBe(optimisticThreadReportID); + + writeSpy.mockRestore(); + canEditFieldSpy.mockRestore(); + }); + + it('prioritizes childReportID over transactionThreadReportID when resolving thread', () => { + const transactionID = 'transaction-priority'; + const childReportID = 'thread-from-action'; + const transactionThreadID = 'thread-from-transaction'; + const iouReportID = 'iou-priority'; + const reportActionID = 'action-priority'; + const policy = createRandomPolicy(21, CONST.POLICY.TYPE.TEAM); + + const childThread: Report = { + ...createRandomReport(21, undefined), + reportID: childReportID, + parentReportID: iouReportID, + policyID: policy.id, + }; + const transactionThread: Report = { + ...createRandomReport(22, undefined), + reportID: transactionThreadID, + parentReportID: iouReportID, + policyID: policy.id, + }; + const iouReport: Report = { + ...createRandomReport(23, undefined), + reportID: iouReportID, + policyID: policy.id, + type: CONST.REPORT.TYPE.EXPENSE, + }; + + const reports = { + [`${ONYXKEYS.COLLECTION.REPORT}${childReportID}`]: childThread, + [`${ONYXKEYS.COLLECTION.REPORT}${transactionThreadID}`]: transactionThread, + [`${ONYXKEYS.COLLECTION.REPORT}${iouReportID}`]: iouReport, + }; + + // Transaction has transactionThreadReportID pointing to one thread + const transaction: Transaction = { + ...createRandomTransaction(21), + transactionID, + reportID: iouReportID, + transactionThreadReportID: transactionThreadID, + amount: 500, + currency: CONST.CURRENCY.USD, + }; + const transactions = { + [`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`]: transaction, + }; + + // Report action has childReportID pointing to a different thread + const reportActions = { + [`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReportID}`]: { + [reportActionID]: { + reportActionID, + actionName: CONST.REPORT.ACTIONS.TYPE.IOU, + originalMessage: { + type: CONST.IOU.REPORT_ACTION_TYPE.CREATE, + IOUTransactionID: transactionID, + }, + childReportID, + }, + }, + }; + + const canEditFieldSpy = jest.spyOn(require('@libs/ReportUtils'), 'canEditFieldOfMoneyRequest').mockReturnValue(true); + // eslint-disable-next-line rulesdir/no-multiple-api-calls + const writeSpy = jest.spyOn(API, 'write').mockImplementation(jest.fn()); + + updateMultipleMoneyRequests({ + transactionIDs: [transactionID], + changes: {merchant: 'Priority Test'}, + policy, + reports, + transactions, + reportActions, + policyCategories: undefined, + policyTags: {}, + hash: undefined, + currentUserAccountID: RORY_ACCOUNT_ID, + delegateAccountID: undefined, + }); + + expect(writeSpy).toHaveBeenCalled(); + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const onyxData = writeSpy.mock.calls.at(0)?.[2] as any; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const optimisticData = onyxData?.optimisticData as any[]; + + // No optimistic thread report should be created — the existing thread from childReportID should be used + const optimisticReportSet = optimisticData.find( + // eslint-disable-next-line @typescript-eslint/no-explicit-any + (entry: any) => String(entry.key).startsWith(ONYXKEYS.COLLECTION.REPORT) && entry.onyxMethod === 'set' && entry.key !== `${ONYXKEYS.COLLECTION.REPORT}${iouReportID}`, + ); + expect(optimisticReportSet).toBeUndefined(); + + // The MODIFIED_EXPENSE report action should be written to the childReportID thread, not the transactionThreadReportID thread + const reportActionMerge = optimisticData.find( + // eslint-disable-next-line @typescript-eslint/no-explicit-any + (entry: any) => entry.key === `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${childReportID}`, + ); + expect(reportActionMerge).toBeDefined(); + + // No report action should be written to the transactionThreadReportID thread + const wrongThreadReportAction = optimisticData.find( + // eslint-disable-next-line @typescript-eslint/no-explicit-any + (entry: any) => entry.key === `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThreadID}`, + ); + expect(wrongThreadReportAction).toBeUndefined(); + + writeSpy.mockRestore(); + canEditFieldSpy.mockRestore(); + }); }); describe('bulk edit draft transaction', () => { From e206f40ddb705b429700c0ca18a3a4040f6dbef9 Mon Sep 17 00:00:00 2001 From: "Roji Philip (via MelvinBot)" Date: Tue, 26 May 2026 18:11:14 +0000 Subject: [PATCH 8/8] Fix ESLint and TypeScript errors in BulkEditTest - Add block-level eslint-disable for no-unsafe-assignment and no-unsafe-member-access in test assertion blocks using `as any` - Add missing `created` property to report action mock object - Add OnyxCollection and ReportActions type imports Co-authored-by: Roji Philip --- tests/actions/IOUTest/BulkEditTest.ts | 32 +++++++++------------------ 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/tests/actions/IOUTest/BulkEditTest.ts b/tests/actions/IOUTest/BulkEditTest.ts index 36fefa7e0c70..a62d05df4c55 100644 --- a/tests/actions/IOUTest/BulkEditTest.ts +++ b/tests/actions/IOUTest/BulkEditTest.ts @@ -1,10 +1,10 @@ import Onyx from 'react-native-onyx'; -import type {OnyxKey} from 'react-native-onyx'; +import type {OnyxCollection, OnyxKey} from 'react-native-onyx'; import {clearBulkEditDraftTransaction, initBulkEditDraftTransaction, updateBulkEditDraftTransaction, updateMultipleMoneyRequests} from '@libs/actions/IOU/BulkEdit'; import CONST from '@src/CONST'; import * as API from '@src/libs/API'; import ONYXKEYS from '@src/ONYXKEYS'; -import type {Policy, Report} from '@src/types/onyx'; +import type {Policy, Report, ReportActions} from '@src/types/onyx'; import type Transaction from '@src/types/onyx/Transaction'; import createRandomPolicy, {createCategoryTaxExpenseRules} from '../../utils/collections/policies'; import {createRandomReport} from '../../utils/collections/reports'; @@ -1554,25 +1554,21 @@ describe('actions/IOU/BulkEdit', () => { }); expect(writeSpy).toHaveBeenCalled(); - // eslint-disable-next-line @typescript-eslint/no-explicit-any + /* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access */ const onyxData = writeSpy.mock.calls.at(0)?.[2] as any; - // eslint-disable-next-line @typescript-eslint/no-explicit-any const optimisticData = onyxData?.optimisticData as any[]; // An optimistic thread report should be created via SET const optimisticReportSet = optimisticData.find( - // eslint-disable-next-line @typescript-eslint/no-explicit-any (entry: any) => String(entry.key).startsWith(ONYXKEYS.COLLECTION.REPORT) && entry.onyxMethod === 'set' && entry.key !== `${ONYXKEYS.COLLECTION.REPORT}${iouReportID}`, ); expect(optimisticReportSet).toBeDefined(); const optimisticThreadReportID = String(optimisticReportSet.key).replace(ONYXKEYS.COLLECTION.REPORT, ''); // The transaction optimistic data should link back to the new thread via transactionThreadReportID - const transactionMerge = optimisticData.find( - // eslint-disable-next-line @typescript-eslint/no-explicit-any - (entry: any) => entry.key === `${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, - ); + const transactionMerge = optimisticData.find((entry: any) => entry.key === `${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`); expect(transactionMerge?.value?.transactionThreadReportID).toBe(optimisticThreadReportID); + /* eslint-enable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access */ writeSpy.mockRestore(); canEditFieldSpy.mockRestore(); @@ -1635,9 +1631,10 @@ describe('actions/IOU/BulkEdit', () => { IOUTransactionID: transactionID, }, childReportID, + created: '2026-01-01 00:00:00', }, }, - }; + } as OnyxCollection; const canEditFieldSpy = jest.spyOn(require('@libs/ReportUtils'), 'canEditFieldOfMoneyRequest').mockReturnValue(true); // eslint-disable-next-line rulesdir/no-multiple-api-calls @@ -1658,31 +1655,24 @@ describe('actions/IOU/BulkEdit', () => { }); expect(writeSpy).toHaveBeenCalled(); - // eslint-disable-next-line @typescript-eslint/no-explicit-any + /* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access */ const onyxData = writeSpy.mock.calls.at(0)?.[2] as any; - // eslint-disable-next-line @typescript-eslint/no-explicit-any const optimisticData = onyxData?.optimisticData as any[]; // No optimistic thread report should be created — the existing thread from childReportID should be used const optimisticReportSet = optimisticData.find( - // eslint-disable-next-line @typescript-eslint/no-explicit-any (entry: any) => String(entry.key).startsWith(ONYXKEYS.COLLECTION.REPORT) && entry.onyxMethod === 'set' && entry.key !== `${ONYXKEYS.COLLECTION.REPORT}${iouReportID}`, ); expect(optimisticReportSet).toBeUndefined(); // The MODIFIED_EXPENSE report action should be written to the childReportID thread, not the transactionThreadReportID thread - const reportActionMerge = optimisticData.find( - // eslint-disable-next-line @typescript-eslint/no-explicit-any - (entry: any) => entry.key === `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${childReportID}`, - ); + const reportActionMerge = optimisticData.find((entry: any) => entry.key === `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${childReportID}`); expect(reportActionMerge).toBeDefined(); // No report action should be written to the transactionThreadReportID thread - const wrongThreadReportAction = optimisticData.find( - // eslint-disable-next-line @typescript-eslint/no-explicit-any - (entry: any) => entry.key === `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThreadID}`, - ); + const wrongThreadReportAction = optimisticData.find((entry: any) => entry.key === `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThreadID}`); expect(wrongThreadReportAction).toBeUndefined(); + /* eslint-enable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access */ writeSpy.mockRestore(); canEditFieldSpy.mockRestore();