Skip to content

fix: prevent empty expense report when category is disabled from another tab#92276

Open
nabi-ebrahimi wants to merge 24 commits into
Expensify:mainfrom
nabi-ebrahimi:fix/expense-empty-report-disabled-category
Open

fix: prevent empty expense report when category is disabled from another tab#92276
nabi-ebrahimi wants to merge 24 commits into
Expensify:mainfrom
nabi-ebrahimi:fix/expense-empty-report-disabled-category

Conversation

@nabi-ebrahimi

@nabi-ebrahimi nabi-ebrahimi commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

Explanation of Change

This change prevents an expense report from appearing empty when only the transaction’s violations are refreshed, such as when a category is disabled from another tab. In that case, the transaction data may not be present in the update payload, but the existing expense should still remain visible in the report while its violation state is updated. The fix preserves the last known transaction for violation-only updates, while still allowing real transaction deletes or moves to remove the expense from its previous report as expected.

Fixed Issues

$ #91016
PROPOSAL: #91016 (comment)

Tests

  1. Open a second browser tab and navigate to Workspace → Categories.
  2. On the first tab, open the workspace chat.
  3. Create an expense using a category.
  4. On the second tab, disable the category that was selected for the expense.
  5. Return to the first tab and observe the expense/report view.
  6. Verify that the empty report is not displayed when the category is disabled from another tab
  • Verify that no errors appear in the JS console

Offline tests

Same as Tests.

QA Steps

Same as Tests.

// 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 any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick)
    • 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 shown in the product is localized by adding it to src/languages/* files and using the translation method
    • I verified all numbers, amounts, dates and phone numbers shown in the product are using the localization methods
    • 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)
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named "index.js". All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • 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)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.ts or at the top of the file that uses the constant) are defined as such
  • I verified that if a function's arguments changed that all usages have also been updated correctly
  • If any new file was added I verified that:
    • The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
  • 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 a new page is added, I verified it's using the ScrollView component to make it scrollable when more elements are added to the page.
  • 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
Screen.Recording.2026-06-02.at.6.16.08.AM.mov
Android: mWeb Chrome
Recording_20260602_061902.mp4
iOS: Native
Screen.Recording.2026-06-02.at.6.42.11.AM.mov
iOS: mWeb Safari
Screen.Recording.2026-06-02.at.7.23.06.AM.mov
MacOS: Chrome / Safari
Screen.Recording.2026-06-02.at.12.35.12.AM.mov

@nabi-ebrahimi
nabi-ebrahimi marked this pull request as ready for review June 2, 2026 14:17
@nabi-ebrahimi
nabi-ebrahimi requested review from a team as code owners June 2, 2026 14:17
@melvin-bot
melvin-bot Bot requested review from JmillsExpensify and Krishna2323 and removed request for a team June 2, 2026 14:17
@melvin-bot

melvin-bot Bot commented Jun 2, 2026

Copy link
Copy Markdown

@Krishna2323 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]

@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: 18c279a4d9

ℹ️ 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".

const reportTransactionsAndViolations = currentValue ? {...currentValue} : {};
if (!transactions) {
return {};
return reportTransactionsAndViolations;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Clear derived transactions when the source collection is gone

When the transaction collection becomes undefined (for example during Onyx.clear() on logout/account reset, or any full collection reset), this now returns the previous derived value instead of {}. Because the derived-value manager passes currentValue from its in-memory cache and then writes the compute result back to Onyx, stale report transactions can be re-persisted after the source collection has been cleared, leaving old expenses visible or even leaking data into the next session. Violation-only updates are already handled later by falling back to previousTransaction, so the no-transactions case should still clear the derived value.

Useful? React with 👍 / 👎.

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.

@nabi-ebrahimi can please resolve this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@Krishna2323 I fixed this by returning {} only when there is no violation update. For violation-only updates, we preserve the current derived value so the report transaction is not dropped. Could you please check it ?

JmillsExpensify
JmillsExpensify previously approved these changes Jun 3, 2026

@JmillsExpensify JmillsExpensify 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.

LGTM

@nabi-ebrahimi

Copy link
Copy Markdown
Contributor Author

@Krishna2323 The PR is ready for review. could you please a take a look when you get a chance ? thanks

OnyxDerived keeps the last computed value in a module-local closure so
future source updates can be merged against the previous derived state.
When another tab updates the same derived key, the local closure can stay
stale even though Onyx has the newer value.

Subscribe to the derived key itself so external updates refresh the local
snapshot. This prevents later partial source updates from recomputing from
stale derived data and accidentally dropping existing report transaction
state.
@nabi-ebrahimi

Copy link
Copy Markdown
Contributor Author

@Krishna2323 I also tested tags and taxes. The empty report bug reproduces for categories, tags, and taxes, and this fix resolves it for all three.
For tags, I noticed a separate preview refresh issue: the violation only updates after opening expense details. Should I handle that here too, or keep this PR scoped to the empty report fix? thanks

JmillsExpensify
JmillsExpensify previously approved these changes Jun 5, 2026

@JmillsExpensify JmillsExpensify 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.

LGTM

@Krishna2323

Krishna2323 commented Jun 7, 2026

Copy link
Copy Markdown
Contributor

For tags, I noticed a separate preview refresh issue: the violation only updates after opening expense details. Should I handle that here too, or keep this PR scoped to the empty report fix? thanks

@nabi-ebrahimi could you please share a recording of this bug?

Comment thread src/libs/actions/OnyxDerived/index.ts Outdated
sourceValues: undefined,
};

Onyx.connectWithoutView({

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.

Why do we need this? I don't see it being mentioned in the selected proposal.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Why do we need this?

derivedValue is tab-local memory used as context.currentValue. When another tab updates the derived Onyx key, this local value can stay stale. Then a violation-only update can recompute from an incomplete currentValue and write that incomplete result back to Onyx, causing the empty report.
This subscription keeps derivedValue synced with the persisted derived key across tabs.

I don't see it being mentioned in the selected proposal.

I found it while testing the selected fix against a wider cross-tab flow. Without this, the reducer fix still works only when the current tab already has the latest derived value, but it can still fail when context.currentValue is stale.

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.

The fix without the self-subscription doesn't work for me. Could you investigate an alternative to the self-subscription in index.ts?

On violation-only updates, instead of syncing the whole derived key closure, we could rebuild only missing transactions for the affected report(s) from the live COLLECTION.TRANSACTION dependency (violation keys + any txn in transactions but not in currentValue). That should fix Tab B writing an incomplete derived value without changing derived-engine behavior for all keys.

Self-subscription downsides: applies to every derived key, adds a race where an older callback can overwrite derivedValue, and can pull a bad cross-tab derived value into currentValue — not just good ones.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@Krishna2323
I removed the self-subscription and investigated this path with logs. In the failing case, the violation-only update includes the new violation key, but that tab does not have the matching transaction in live COLLECTION.TRANSACTION or currentValue, so there is not enough data to rebuild the affected report.

Instead of publishing that incomplete recompute, I now skip the derived update when a violation-only update references an unresolved transaction. This keeps the fix scoped and avoids changing derived-engine behavior for all keys.

@Krishna2323 Krishna2323 Jun 17, 2026

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.

@nabi-ebrahimi The transaction no longer disappears, but it brings back a deleted report:

https://github.com/user-attachments/assets/0166c13c-f90b-88f-95fc-d9e7fc98acb6

Could you investigate a proper solution and perform thorough testing?

Suggestion: Look into the ramOnlyKeys approach for this derived key — evaluate tradeoffs vs. the current fix.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@Krishna2323 Thanks for catching this.

The deleted report was coming back because our previous fix preserved currentValue, but when a real transaction delete happened we only removed the transaction from the report bucket and left the empty bucket behind:

reportID: {
    transactions: {},
    violations: {},
}

That empty derived bucket kept getting carried into later recomputes, so the deleted report could appear again even though it no longer had any transactions.

I investigated this more deeply with runtime logs across the full flow: create report → disable category → delete report → create another report → disable category again. The logs showed the violation-only guard was working correctly. The regression was specifically caused by stale empty report buckets surviving after a real TRANSACTION delete update.

I also tested the ramOnlyKeys approach for REPORT_TRANSACTIONS_AND_VIOLATIONS. It avoids syncing bad derived values across tabs, but it caused a worse regression: when creating an expense in Tab A, Tab B could render the expense/report empty because this derived value was no longer persisted/shared and Tab B had not recomputed it locally yet. So the tradeoff is not good here: it can hurt cross-tab rendering and startup/perf, which matches the concern already raised in the issue.

The updated fix keeps the current derived-key behavior and makes the reducer cleanup stricter:

  • violation-only updates still cannot remove valid transaction membership
  • real transaction delete/move updates still remove/move transactions
  • after removing a transaction/violation, we now delete the report bucket if it becomes empty
  • I added a regression test for deleting the last transaction from a report

I retested the original issue and the deleted-report regression, and both are working correctly now.

Could you please re-review again when you get a chance ? thanks

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.

@nabi-ebrahimi the bug is still there 🥲:

Monosnap.screencast.2026-06-23.19-32-06.mp4

@Krishna2323 Krishna2323 Jun 25, 2026

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.

@nabi-ebrahimi have you found any better solutions or should we open the issue for new proposals? Do you have ETA?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@Krishna2323 Yes, I’ve fixed the deleted-report regression and I’m now testing the full flow to make sure no other unexpected regressions were introduced. I’ll have the final solution ready by tomorrow. Thanks for your patience.

@nabi-ebrahimi

Copy link
Copy Markdown
Contributor Author

@nabi-ebrahimi could you please share a recording of this bug?

@Krishna2323 Sure.

Screen.Recording.2026-06-03.at.5.31.43.PM.mov

@nabi-ebrahimi

This comment was marked as outdated.

@github-actions

github-actions Bot commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@@ -8,29 +8,35 @@ const transactionReportIDMapping: Record<string, string> = {};

const transactionToReportIDMap: Record<string, string> = {};

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.

transactionToReportIDMap is never read — only deleted at line 110. We should remove it.

Comment thread src/libs/actions/OnyxDerived/index.ts Outdated
sourceValues: undefined,
};

Onyx.connectWithoutView({

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.

@nabi-ebrahimi the bug is still there 🥲:

Monosnap.screencast.2026-06-23.19-32-06.mp4

@nabi-ebrahimi

Copy link
Copy Markdown
Contributor Author

@Krishna2323 I fixed the reported regressions and tested the main flows again. Everything is working correctly on my side now.

Tested flows:

  1. Created a report/expense and disabled its category. The report no longer becomes empty.
REC-20260629121510.mp4
  1. Deleted a report, created another report, then disabled its category. The deleted report does not reappear.
REC-20260629121615.mp4
  1. Created multiple expenses in one report, deleted some of them, then disabled the category. The deleted transactions do not reappear.
REC-20260629122025.mp4

Could you please re review again when you get a chance ? thanks

@Krishna2323

Copy link
Copy Markdown
Contributor

@codex 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: a334fa102b

ℹ️ 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".

@github-actions

github-actions Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

This PR adds a new Onyx.connectWithoutView call, so I've requested a review from the Onyx performance reviewers (@tgolen, @mountiny, @luacmartins) — a review from any one of them is enough. Please add a link in your PR description to the Slack discussion where the @frontend-performance team approved using connectWithoutView here.

@nabi-ebrahimi

Copy link
Copy Markdown
Contributor Author

@Krishna2323 I have resolved the codex latest comment, could you please take another look when you get a chance ? thanks

@Krishna2323

Copy link
Copy Markdown
Contributor

lease add a link in your PR description to the Slack discussion where the @frontend-performance team approved using connectWithoutView here.

@nabi-ebrahimi, could you please start a Slack discussion about using Onyx.connectWithoutView in this PR? We need approval from the @frontend-performance team before moving forward, and then we can link that discussion in the PR description.

I don’t expect pushback here since connectWithoutView is already used in this same file for dependency subscriptions

@nabi-ebrahimi

Copy link
Copy Markdown
Contributor Author

@Krishna2323, I’ve started the Slack discussion here. Thank you!

@stephanieelliott
stephanieelliott removed their request for review July 10, 2026 20:20
@tgolen

tgolen commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

@nabi-ebrahimi I think from looking at that thread, that the root problem with the tabs should be fixed now. Is that indeed the case and this PR can be closed out without merging?

@tgolen

tgolen commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

bump @nabi-ebrahimi

@nabi-ebrahimi

Copy link
Copy Markdown
Contributor Author

@tgolen I checked again, and the issue is not fixed yet on my side. The related Onyx PR that should address the root cross-tab collection sync issue is still open/draft: Expensify/react-native-onyx#803

I can still reproduce the issue, so I don’t think this PR should be closed yet.

@tgolen

tgolen commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

OK, thank you. Have you tried to reproduce it with the Onyx fix in that PR?

@nabi-ebrahimi

Copy link
Copy Markdown
Contributor Author

No yet, but I'll test it. thanks

@nabi-ebrahimi

Copy link
Copy Markdown
Contributor Author

@tgolen I tested by temporarily pinning react-native-onyx to Expensify/react-native-onyx#803 (57a1564c6bf93768a9fe7a861a4f4d4b024fea69), and the issue was no longer reproducible for me.

So it looks like the root issue will be fixed by the Onyx PR.

cc @Krishna2323

@Krishna2323

Copy link
Copy Markdown
Contributor

@nabi-ebrahimi lets close this then.

@nabi-ebrahimi

Copy link
Copy Markdown
Contributor Author

@Krishna2323
Sounds good, but I think we should wait for the Onyx PR to be merged and bumped into App, then re-test once more before fully closing this out.

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.

4 participants