Skip to content

WS Settings - Saving new spend rule briefly displayed then disappear #88453

Closed

Description

@jponikarchuk

If you haven’t already, check out our contributing guidelines for onboarding and email contributors@expensify.com to request to join our Slack channel!


Version Number: v9.3.61-0
Reproducible in staging?: Yes
Reproducible in production?: N/A - new feature, doesn't exist in prod
If this was caught during regression testing, add the test name, ID and link from BrowserStack: https://test-management.browserstack.com/projects/2219752/test-runs/TR-21015/folder/13176707/101177362/1356328364?p=2&sort=%2Bidentifier
#86684
Email or phone of affected tester (no customers): applausetester+piyoh@applause.expensifail.com
Issue reported by: Applause Internal Team
Bug source: Regression TC Execution
Device used: Windows 11 / Chrome, iPhone 13 / Safari
App Component: Workspace Settings

Action Performed:

Preconditions:

  • Logged in to expensifail account that has at least one control workspace
  • Workspace has rules feature enabled
  • At least one Expensify card has been issued to the workspace

Steps:

  1. Navigate to workspace settings -> Rules
  2. Scroll down to "Spend" section
  3. Click on the "Add spend rule"
  4. Click on "Choose cards"
  5. Select the card(s) to apply the rule to and click "Save"
  6. In "Spend rule" section Verify you can select Allow or Block
  7. Select at least one merchant, spend category, or max amount
  8. Save rule

Expected Result:

After saving the spend rule, it should be remains displayed and not disappear

Actual Result:

After saving the spend rule, it briefly displayed then disappear, and it only correctly displayed after refreshing the page

Workaround:

Unknown

Platforms:

  • Android: App
  • Android: mWeb Chrome
  • iOS: App
  • iOS: mWeb Safari
  • iOS: mWeb Chrome
  • Windows: Chrome
  • MacOS: Chrome / Safari

Screenshots/Videos

Video.mp4

View all open jobs on GitHub

Activity

  1. added
    DeployBlockerCashThis issue or pull request should block deployment
    BugSomething is broken. Auto assigns a BugZero manager.
    on Apr 21, 2026
  2. melvin-bot commented on Apr 21, 2026

    @melvin-bot

    You have been assigned to this deploy blocker because you recently merged this PR: #86684

  3. melvin-bot commented on Apr 21, 2026

    @melvin-bot

    💬 A slack conversation has been started in #expensify-open-source

  4. github-actions commented on Apr 21, 2026

    @github-actions
    Contributor

    👋 Friendly reminder that deploy blockers are time-sensitive ⏱ issues! Check out the open `StagingDeployCash` deploy checklist to see the list of PRs included in this release, then work quickly to do one of the following:

    1. Identify the pull request that introduced this issue and revert it.
    2. Find someone who can quickly fix the issue.
    3. Fix the issue yourself.
  5. OSBotify commented on Apr 21, 2026

    @OSBotify
    Contributor

    🔍 Investigation Summary

    Classification: Frontend bug
    Causing PR: #86684 - "Add spend rule base page" by @luacmartins (High confidence)
    Related Issues: #86521

    Recommendation: REVERT

    PR #86684 introduced the spend rule feature and contains a logic bug in the SpendRulesSection component that causes the newly created rule to disappear immediately after saving, only reappearing after a page refresh.

    Assigned: @luacmartins (PR author), @JS00001 (approving reviewer) from highest-confidence causing PR only
    Labels: DeployBlockerCash kept (frontend App PR is the cause). No label changes made.

    📋 Detailed Analysis

    Evidence

    • PR Add spend rule base page #86684 introduced the entire spend rules feature, including SpendRulesSection.tsx and SpendRulePageBase.tsx
    • The bug matches the symptom exactly: after saving a spend rule, it briefly appears then disappears until a page refresh
    • The issue is only reproducible on staging (not production), consistent with this being a new feature in the current staging deploy
    • melvin-bot automatically assigned this issue to the PR's author, confirming PR Add spend rule base page #86684 is the suspected cause

    Verification

    • Affected file: src/pages/workspace/rules/SpendRules/SpendRulesSection.tsx
    • Affected file: src/pages/workspace/rules/SpendRules/SpendRulePageBase.tsx
    • Affected file: src/libs/actions/Card.ts
    • PR Add spend rule base page #86684 introduces all three of these files

    Root Cause

    In SpendRulesSection.tsx (lines 124-161), after a rule is saved, the component filters formValues.cardIDs against the live cardsList Onyx value:

    const activeCardIDs = formValues.cardIDs.filter(
      (cardID) => !!cardsList?.[cardID],
    );
    if (activeCardIDs.length === 0) {
      return undefined; // rule is hidden if no active card IDs match
    }

    In SpendRulePageBase.tsx (lines 159-161), the save sequence is:

    1. setExpensifyCardRule(...) — sends optimistic update with the new rule into PRIVATE_EXPENSIFY_CARD_SETTINGS
    2. clearDraftSpendRule() — clears the form
    3. Navigation.goBack() — returns the user to the rules list

    The isSpendRulesListLoading guard in SpendRulesSection also checks !expensifyCardSettings?.hasOnceLoaded. The optimistic data written by setExpensifyCardRule does not set hasOnceLoaded: true, so when the user lands back on the list, if cardsList has not yet fully hydrated from Onyx, activeCardIDs resolves to an empty array for the newly created rule, causing the rule to be filtered out (returning undefined). The rule reappears only once the backend response is processed and all Onyx keys are refreshed, which matches the "only displayed correctly after refreshing the page" behavior reported.

  6. MelvinBot commented on Apr 21, 2026

    @MelvinBot
    Contributor

    Proposal

    What is the root cause of that problem?

    Race condition between a WRITE and a READ request. When the user saves a spend rule:

    1. setExpensifyCardRule (a WRITE command) optimistically merges the new rule into PRIVATE_EXPENSIFY_CARD_SETTINGS.cardRules
    2. Navigation.goBack() is called immediately after, remounting SpendRulesSection
    3. SpendRulesSection's unguarded useEffect fires openPolicyExpensifyCardsPage (a READ command)
    4. The server processes the READ before the WRITE completes, so the response doesn't include the newly created rule
    5. The server response overwrites the optimistic data in Onyx, making the rule disappear

    The rule reappears on refresh because by then the WRITE has been processed server-side.

    What changes do you think we should make in order to solve the problem?

    Guard the useEffect in SpendRulesSection.tsx to skip calling openPolicyExpensifyCardsPage when the card settings data has already been loaded. The PRIVATE_EXPENSIFY_CARD_SETTINGS object already has a hasOnceLoaded flag (set in openPolicyExpensifyCardsPage's successData). Check this flag before re-fetching:

    // In SpendRulesSection.tsx useEffect:
    if (!expensifyCardSettings?.hasOnceLoaded) {
        openPolicyExpensifyCardsPage(policyID, defaultFundID);
    }
    

    This prevents the redundant READ from firing when navigating back after save, since the data was already loaded when the user first visited the rules page. The optimistic WRITE data will remain intact until the WRITE completes on the server.

    What alternative solutions did you explore? (Optional)

    An alternative would be to have setExpensifyCardRule's successData re-merge the full rule data (not just clear pendingAction), so even if the READ clobbers it, the WRITE success handler restores it. However, this is fragile — it depends on the WRITE success arriving after the READ response, which isn't guaranteed. Guarding the fetch is simpler and more robust.


    Next Steps for Contributor+ team: Reply with @MelvinBot implement this to create a draft PR, @MelvinBot <your feedback> to refine this analysis, or explain why you are rejecting Melvin's proposal.

  7. JS00001 commented on Apr 21, 2026

    @JS00001
    Contributor
  8. JS00001 commented on Apr 21, 2026

    @JS00001
    Contributor

    This is also disabled on prod, so demoting

  9. added and removed
    DeployBlockerCashThis issue or pull request should block deployment
    on Apr 21, 2026
  10. IuliiaHerets commented on Apr 22, 2026

    @IuliiaHerets

    Chrome / v9.3.61-2
    Still reproduced

    bandicam.2026-04-22.16-18-33-287.mp4
  11. IuliiaHerets commented on Apr 22, 2026

    @IuliiaHerets

    Tester can't repro this issue anymore

    bandicam.2026-04-22.20-25-28-444.mp4
  12. JS00001 commented on Apr 22, 2026

    @JS00001
    Contributor

    This was fixed in the be

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

Metadata

Metadata

Labels

BugSomething is broken. Auto assigns a BugZero manager.DailyKSv2Engineering

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions