Skip to content

Fix merge expense transaction thread navigation - #96633

Open
marufsharifi wants to merge 59 commits into
Expensify:mainfrom
marufsharifi:fix/merge-expense-transaction-thread-navigation
Open

marufsharifi wants to merge 59 commits into
Expensify:mainfrom
marufsharifi:fix/merge-expense-transaction-thread-navigation

Conversation

@marufsharifi

@marufsharifi marufsharifi commented Jul 21, 2026 •

Copy link
Copy Markdown
Contributor

Explanation of Change

Merge expense now follows the same destination behavior across individual and bulk merge flows.

Before this update, merging expenses from search/spend could open the wrong place or leave the RHP loading, especially when the target workspace expense did not already have a transaction thread and the destination was an unreported/self-DM expense.

Now, the app creates the missing optimistic transaction thread when needed, carries that thread through confirmation, and attaches offline optimistic merge data to the correct destination report. This makes post-merge navigation consistent and predictable for workspace, self-DM, split, and offline merge cases.

Fixed Issues

$ #83257
PROPOSAL:

Tests

Flow 1: Workspace expense with self-DM expense

  1. Create an expense in self DM.
  2. Create an expense in workspace chat.
  3. Go to Spend > Expenses.
  4. Select the workspace expense via the checkbox.
  5. Click the dropdown button > Merge.
  6. Select self DM expense > Continue.
  7. Select the second option in each section > Next > Merge expenses.
  8. Verify the transaction thread opens after merging expenses via the bulk option.
  9. Verify the app does not open the self-DM “Your space” page.

Flow 2: Workspace manual expense with split expense

  1. Go to a workspace chat.
  2. Create an expense split.
  3. Create a manual expense.
  4. Navigate to Spend > Expenses.
  5. Select the manual expense created in Step 3 using the checkbox, then click Merge.
  6. In the merge flow, choose the Split Expense option and complete the merge.
  7. Verify that the transaction thread opens automatically after the expenses are merged using the bulk merge option.

Flow 3: Offline workspace expense with self-DM expense

  1. Create an expense in self DM.
  2. Create an expense in workspace chat.
  3. Go to Spend > Expenses.
  4. Turn off internet connection.
  5. Select the workspace expense via the checkbox.
  6. Click the dropdown button > Merge.
  7. Select self DM expense > Continue.
  8. Select the second option in each section > Next > Merge expenses.
  9. Verify the transaction thread opens after merging expenses via the bulk option.
  10. Verify the RHP does not load infinitely.
  • Verify that no errors appear in the JS console

Offline tests

QA Steps

// TODO: These must be filled out, or the issue title must include "[No QA]."

  • Verify that no errors appear in the JS console

PR Author Checklist

  • I linked the correct issue in the ### Fixed Issues section above
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I added steps for the expected offline behavior in the Offline steps section
    • I added steps for Staging and/or Production testing in the QA steps section
    • I added steps to cover failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
    • I tested this PR with a High Traffic account against the staging or production API to ensure there are no regressions (e.g. long loading states that impact usability).
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • Android: Native
    • Android: mWeb Chrome
    • iOS: Native
    • iOS: mWeb Safari
    • MacOS: Chrome / Safari
  • I verified there are no console errors (if there's a console error not related to the PR, report it or open an issue for it to be fixed)
  • I followed proper code patterns (see Reviewing the code)
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is either coming verbatim from figma or has been approved by marketing (in order to get marketing approval, ask the Bug Zero team member to add the Waiting for copy label to the issue)
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I followed the guidelines as stated in the Review Guidelines
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(theme.componentBG))
  • If new assets were added or existing ones were modified, I verified that:
    • The assets are optimized and compressed (for SVG files, run npm run compress-svg)
    • The assets load correctly across all supported platforms.
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If the PR modifies the UI (e.g. new buttons, new UI components, changing the padding/spacing/sizing, moving components, etc) or modifies the form input styles:
    • I verified that all the inputs inside a form are aligned with each other.
    • I added Design label and/or tagged @Expensify/design so the design team can review the changes.
    • If the PR adds or modifies the UI:
  • I asked an AI agent to review the changes for accessibility issues and addressed its findings.
  • I tested with a screen reader (VoiceOver on macOS) and verified all new/changed elements are reachable with a logical focus order.
  • I verified all new/changed elements have meaningful accessible names and roles.
  • I verified state changes are announced (e.g. checked/unchecked, expanded/collapsed, selected).
  • I added unit tests for any new feature or bug fix in this PR to help automatically prevent regressions in this user flow.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.

Screenshots/Videos

Android: Native

Flow:1

flow01.mov

Flow:2

flow02.mov

Flow:3

flow03.mov
Android: mWeb Chrome

Flow:1

flow1.mp4

Flow:2

flow2.mp4

Flow:3

flow3.mp4
iOS: Native

Flow:1

flow001.mov

Flow:2

flow002.mov

Flow:3

flow003.mov
iOS: mWeb Safari

Flow:1

flow0001.mov

Flow:2

flow0002.mov

Flow:3

Screen.Recording.2026-07-21.at.8.17.39.PM.mov
MacOS: Chrome / Safari

Flow:1

flow1.mov

Flow:2

flow2.mov

Flow:3

flow3.mov

@codecov

codecov Bot commented Jul 21, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Looks like you've decreased code coverage for some files. Please write tests to increase, or at least maintain, the existing level of code coverage. See our documentation here for how to interpret this table.

Files with missing lines Coverage Δ
src/libs/MergeTransactionUtils.ts 91.45% <ø> (-0.04%) ⬇️
src/libs/ReportSecondaryActionUtils.ts 96.38% <ø> (ø)
src/libs/TransactionUtils/getUpdatedTransaction.ts 96.89% <100.00%> (ø)
src/libs/actions/Report/index.ts 74.45% <100.00%> (+0.03%) ⬆️
src/hooks/useSearchBulkActions.ts 80.80% <0.00%> (ø)
src/libs/actions/MergeTransaction.ts 91.81% <96.77%> (+1.42%) ⬆️
...pages/TransactionMerge/DynamicConfirmationPage.tsx 58.49% <19.23%> (-11.39%) ⬇️
... and 9 files with indirect coverage changes

@marufsharifi
marufsharifi marked this pull request as ready for review July 21, 2026 16:01
@marufsharifi
marufsharifi requested review from a team as code owners July 21, 2026 16:01
@melvin-bot
melvin-bot Bot requested review from joekaufmanexpensify and rojiphil and removed request for a team July 21, 2026 16:01
@melvin-bot

melvin-bot Bot commented Jul 21, 2026

Copy link
Copy Markdown

@rojiphil Please copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button]

@melvin-bot
melvin-bot Bot removed request for a team and joekaufmanexpensify July 21, 2026 16:01
@marufsharifi

Copy link
Copy Markdown
Contributor Author

@rojiphil, could you please take a look at this. thanks.

@marufsharifi

Copy link
Copy Markdown
Contributor Author

@rojiphil, Gentle bump. thanks.

@marufsharifi

Copy link
Copy Markdown
Contributor Author

@rojiphil, Friendly bump. thanks.

@rojiphil

Copy link
Copy Markdown
Contributor

@marufsharifi Picking this up today

@rojiphil

Copy link
Copy Markdown
Contributor

Thanks @marufsharifi for the update. The core approach used here looks like a good improvement over the original PR. The offline issue also seems to have been fixed. Will take a closer look at the code sometime today.

96633-web-chrome-001.mp4

@marufsharifi

Copy link
Copy Markdown
Contributor Author

@rojiphil, Quick bump. thanks.

@marufsharifi

Copy link
Copy Markdown
Contributor Author

@rojiphil, quick bump. thanks.

@marufsharifi

Copy link
Copy Markdown
Contributor Author

@rojiphil, Friendly bump. thanks.

@marufsharifi

Copy link
Copy Markdown
Contributor Author

bumped in slack.

@rojiphil

rojiphil commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

@codex review

@rojiphil

rojiphil commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

@MelvinBot review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d066c8dfb4

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/pages/TransactionMerge/DynamicConfirmationPage.tsx Outdated
Comment thread src/libs/actions/MergeTransaction.ts
@MelvinBot

Copy link
Copy Markdown
Contributor

Review — looks solid ✅

Approach is sound and matches the reported bug (#83257). The fix has three coordinated parts, all of which check out:

  1. Bulk flow now passes policies into target/source selection — useSearchBulkActions.ts:2074 forwards transactionPolicies to setupMergeTransactionDataAndNavigate, so selectTargetAndSourceTransactionsForMerge picks the same target the individual flow does. This is the root cause of "self-DM opens instead of the thread."
  2. Missing thread is seeded on confirm — DynamicConfirmationPage.tsx:100-119 creates the optimistic transaction thread when the target never had one, and navigation lands on it.
  3. Optimistic merge data attaches to the real self-DM (not report "0") via optimisticDestinationReportID, and hasOptimisticReportActions clears the initial-loading state so the RHP doesn't spin forever (Report/index.ts:1590-1591).

Verification

  • All CI checks pass (test ×8, typecheck, ESLint, Oxfmt, React Compiler, Storybook). Coverage on the two touched action files went up.
  • Manually re-ran the bulk-merge flow on web (Flow 1): the merged transaction thread opened correctly and the self-DM "Your space" page did not open; RHP loaded fully with no infinite spinner.

A couple of non-blocking things worth a look:

Observations / questions
  • hasOptimisticReportActions=false path is safe — the new Report/index.ts:1590 writes hasOnceLoadedReportActions: undefined for existing callers, which Onyx MERGE drops, so no behavior change for the many other openReport callers. Just calling out that this relies on Onyx dropping undefined on merge — fine, but a bit implicit.

  • Extra OpenReport command on confirm. When the target has no thread, createTransactionThreadReport runs inside the click handler and fires its own OpenReport (via openReport) in addition to MergeTransaction. Worth confirming this double-command is intended and that the backend reconciles the separately-seeded thread with the merged expense in all cases (the client-generated thread reportID is sent via OpenReport, but is not in the MergeTransaction params — MergeTransaction.ts:432-459). Online it worked cleanly in my test; the offline hand-off is the interesting case.

  • Two self-DM resolution paths. The page computes the destination with findSelfDMReportID() (DynamicConfirmationPage.tsx:92) while mergeTransactionRequest uses the useSelfDMReport() value (selfDMReport?.reportID). They should agree in practice, but if the self-DM report is missing/filtered the action falls back to report "0" while createTransactionThreadReport builds an optimistic self-DM instead — a minor inconsistency in the (rare) missing-self-DM edge. Probably not worth handling, just flagging.

Nice test coverage on the new branches (unreported→self-DM attach, and the "IOU action has no childReportID" path). No blocking concerns from me on the code.


This is an automated review. Reply with @MelvinBot <feedback> to refine.

@marufsharifi

Copy link
Copy Markdown
Contributor Author

@rojiphil, Friendly bump. thanks.

@marufsharifi

Copy link
Copy Markdown
Contributor Author

@rojiphil, conflicts resolved again. This PR has been open a while and I'm resolving conflicts almost daily.
Could you review so we can get this merged? Thanks!

@rojiphil

Copy link
Copy Markdown
Contributor

@marufsharifi The challenge in this issue is that there are quite a lot of edge scenarios that need to be tested.
The pending #96633 (comment) seems to be the only edge case left. Otherwise, testing looks good.
And I can still reproduce this in the latest merge. Not sure if this is due to some local issue on my system but it doesn’t look like. Any clue on what could cause this?

96633-issue-1.mp4

@rojiphil

Copy link
Copy Markdown
Contributor

Since you are not able to reproduce this, this must be something to do with some difference in workspace or expenses between our setups.

@marufsharifi

Copy link
Copy Markdown
Contributor Author

@marufsharifi The challenge in this issue is that there are quite a lot of edge scenarios that need to be tested. The pending #96633 (comment) seems to be the only edge case left. Otherwise, testing looks good. And I can still reproduce this in the latest merge. Not sure if this is due to some local issue on my system but it doesn’t look like. Any clue on what could cause this?

96633-issue-1.mp4

@rojiphil I was able to reproduce this with a unit test and found the cause. It's fixed now —
could you check once again? Thanks!

@marufsharifi

Copy link
Copy Markdown
Contributor Author

@rojiphil, Quick bump. thanks.

@rojiphil

Copy link
Copy Markdown
Contributor

Will check this today.

@rojiphil

Copy link
Copy Markdown
Contributor

@codex review

@rojiphil

Copy link
Copy Markdown
Contributor

@MelvinBot review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-21T04:17:32.376591Z
We couldn’t complete this review. Comment @codex review to try again.
98a6b29 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@MelvinBot

Copy link
Copy Markdown
Contributor

Code review

The approach looks right, and CI is green apart from the approval check. I found one change worth making before merge, plus two non-blocking notes.

1. Only create a thread when the target has an IOU action and no thread yet

In DynamicConfirmationPage.tsx:135-156, createTransactionThreadReport runs whenever getTransactionThreadReportID returns nothing. That helper only checks transaction.transactionThreadReportID and the IOU action in Onyx. This can go wrong in two ways:

  • The thread already exists but only the snapshot knows it. The new code reads targetIOUAction from the Search snapshot but ignores targetIOUAction.childReportID. A second thread would be created for an expense that already has one.
  • No IOU action is found. createTransactionThreadReport then takes openReport's legacy path and builds a second IOU action. The comment on line 136 says this path should be avoided, but nothing prevents it.

Suggested guard:

if (targetIOUAction?.childReportID) {
    targetTransactionThreadReportIDForMerge = targetIOUAction.childReportID;
} else if (targetIOUAction) {
    // createTransactionThreadReport(...)
}

2. Non-blocking notes

  • The Self-DM success data deletes the placeholder action at MergeTransaction.ts:821. The moved thread's parentReportActionID still points to that placeholder, so the thread depends on the server re-linking it. I can't verify the backend side. When you test Flow 1 online, refresh the thread after the merge and confirm it still loads.
  • Report/index.ts:1773 and the MoneyRequestReportView skeleton change affect offline loading for every report, not just merges. Please also test opening an ordinary cached report and expense report offline.

view run

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Something went wrong. Try again later by commenting “@codex review”.

Failed to sample tokens
ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@marufsharifi

Copy link
Copy Markdown
Contributor Author

@rojiphil addressed Melvin's latest feedback. Could you check once again? Thanks!

@rojiphil

Copy link
Copy Markdown
Contributor

Review in progress

@marufsharifi

Copy link
Copy Markdown
Contributor Author

Conflicts resolved

@rojiphil rojiphil left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marufsharifi Did some investigation regarding the issue mentioned at #96633 (comment).
When the merge destination is a self-DM, the target expense's IOU action can already point at a transaction thread that was never opened, so that report is missing from Onyx. The merge only wrote parentReportID and parentReportActionID onto that key, which left a report with no reportID and no reportActions_ entry, so the right-hand pane waited on OpenReport forever while offline.
Looks like we need to seed a full thread report, a placeholder CREATED action, and a loaded state when that report is missing, and still only updates the parent fields when the thread is already in Onyx.
Please look into this. Thanks.

I have left one more review comment for your attention.

Comment thread src/components/MoneyRequestReportView/MoneyRequestReportView.tsx Outdated
@marufsharifi

Copy link
Copy Markdown
Contributor Author

@marufsharifi Did some investigation regarding the issue mentioned at #96633 (comment). When the merge destination is a self-DM, the target expense's IOU action can already point at a transaction thread that was never opened, so that report is missing from Onyx. The merge only wrote parentReportID and parentReportActionID onto that key, which left a report with no reportID and no reportActions_ entry, so the right-hand pane waited on OpenReport forever while offline. Looks like we need to seed a full thread report, a placeholder CREATED action, and a loaded state when that report is missing, and still only updates the parent fields when the thread is already in Onyx. Please look into this. Thanks.

I have left one more review comment for your attention.

@rojiphil, Thanks for the investigation. I couldn't reproduce it on my side, so I've implemented the fix based on your findings. Could you check and ping me? Thanks!

@rojiphil

Copy link
Copy Markdown
Contributor

@marufsharifi Seeding the missing thread report is the right fix for the two-field report_. It still never writes reportActions_ or the loading flags, and the empty-actions skeleton is no longer skipped offline, so Flow 3 can keep showing that skeleton. Please also seed a placeholder CREATED action (removed on success) and set hasOnceLoadedReportActions / isLoadingInitialReportActions: false when the thread report is missing. Treat a stored report with no reportID as missing, and cover the no-snapshot case in the test.

@marufsharifi

Copy link
Copy Markdown
Contributor Author

@rojiphil all four addressed, including the no-snapshot test case. Could you check and ping me? Thanks!

@marufsharifi Seeding the missing thread report is the right fix for the two-field report_. It still never writes reportActions_ or the loading flags, and the empty-actions skeleton is no longer skipped offline, so Flow 3 can keep showing that skeleton. Please also seed a placeholder CREATED action (removed on success) and set hasOnceLoadedReportActions / isLoadingInitialReportActions: false when the thread report is missing. Treat a stored report with no reportID as missing, and cover the no-snapshot case in the test.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants