Skip to content

Distance - Discard changes modal opens after creating Odometer expense #84218

Closed

Description

@lanitochka17

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: 9.3.31-0
Reproducible in staging?: Yes
Reproducible in production?: No
If this was caught during regression testing, add the test name, ID and link from BrowserStack: #78291
Email or phone of affected tester (no customers): applausetester+sd2d2d@applause.expensifail.com (you)
Issue reported by: Applause Internal Team
Bug source: Exploratory - Significant User Experience Deterioration
Device used: Mac 26.2 / Chrome
App Component: Money Requests

Action Performed:

Precondition:

  • Log in with Expensifail account.
  1. Go to staging.new.expensify.com
  2. Go to workspace chat.
  3. Click + > Track distance > Odometer.
  4. Enter Start and End reading > Next.
  5. Click Create expense.

Expected Result:

Discard changes modal will not open.

Actual Result:

Discard changes modal opens after creating Odometer expense.
On iOS app, confirm page becomes blank. Swiping back reveals Discard changes modal.

Workaround:

Unknown

Platforms:

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

Screenshots/Videos

Bug7094075_1772658943648.17.mp4

View all open jobs on GitHub

Issue OwnerCurrent Issue Owner: @DylanDylann

Activity

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

    @melvin-bot

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

  3. melvin-bot commented on Mar 4, 2026

    @melvin-bot

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

  4. melvin-bot commented on Mar 4, 2026

    @melvin-bot

    Triggered auto assignment to @NicMendonca (Bug), see https://stackoverflow.com/c/expensify/questions/14418 for more details. Please add this bug to a GH project, as outlined in the SO.

  5. github-actions commented on Mar 4, 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.
  6. OSBotify commented on Mar 4, 2026

    @OSBotify
    Contributor

    🔍 Investigation Summary

    Classification: Frontend bug
    Causing PR: #78291 - "Migrate useBeforeRemove to usePreventRemove in DiscardChangesConfirmation" by @mkzie2 (High confidence)
    Related Issues: None

    Recommendation: REVERT

    This PR introduced a regression in the odometer distance flow. The migration from useBeforeRemove to usePreventRemove changed how the "unsaved changes" state is tracked, and the new implementation fails to reset the baseline state after successfully creating an expense.

    Labels: Keeping DeployBlockerCash - this is a frontend bug that blocks the App deploy. No label changes needed.

    📋 Detailed Analysis

    Evidence

    PR #78291 modified the IOURequestStepDistanceOdometer.tsx file and changed how "unsaved changes" are detected for the DiscardChangesConfirmation component.

    The issue is reproducible only on staging (where PR #78291 was deployed) and not on production (where it has not yet been deployed), which confirms this PR as the cause.

    Verification

    The affected file is /home/runner/_work/App/App/src/pages/iou/request/step/IOURequestStepDistanceOdometer.tsx, which was modified by PR #78291.

    Looking at the PR diff, the following changes were made:

    1. Changed DiscardChangesConfirmation prop from getHasUnsavedChanges (callback function) to hasUnsavedChanges (computed boolean)
    2. Replaced refs for tracking initial values with state: const [initialReadings, setInitialReadings] = useState<{start: string; end: string}>({start: "", end: ""})
    3. Added a useMemo to compute hasUnsavedChanges by comparing current readings with initialReadings

    Root Cause

    The bug occurs because of a state management flaw in the new implementation:

    Lines 166-184: The initialReadings state is initialized ONCE on mount and never updated again:

    useEffect(() => {
        if (hasInitializedRefs.current) {
            return;  // Early return prevents re-initialization
        }
        // ... sets initialReadings ...
        hasInitializedRefs.current = true;
    }, [...]);

    Lines 354-497: When the user creates an expense via navigateToNextPage():

    1. The transaction is updated with the new odometer readings via setMoneyRequestOdometerReading()
    2. However, the initialReadings state is NEVER updated to reflect this save
    3. The component continues to exist (navigation has not completed yet)

    Lines 500-505: The hasUnsavedChanges computed value:

    const hasUnsavedChanges = useMemo(
        () =>
            shouldEnableDiscardConfirmation &&
            (startReading !== initialReadings.start || endReading !== initialReadings.end || ...),
        [...]
    );

    This still compares against the stale initialReadings from mount time, not the newly saved values. Therefore hasUnsavedChanges remains true even after successfully saving.

    Result: The DiscardChangesConfirmation with usePreventRemove triggers the discard modal because it thinks there are still unsaved changes, even though the expense was just saved.

    The old implementation using useBeforeRemove with a callback function getHasUnsavedChanges() would have checked the refs dynamically and avoided this issue, but the new state-based approach fails to update the baseline after save.

  7. MelvinBot commented on Mar 4, 2026

    @MelvinBot
    Contributor

    Proposal

    Please re-state the problem that we are trying to solve in this issue.

    After creating an Odometer expense (Track distance > Odometer) in a workspace chat, a "Discard changes" modal incorrectly appears. On iOS, the confirm page becomes blank and swiping back reveals the Discard changes modal. This was introduced by PR #78291 which migrated DiscardChangesConfirmation from useBeforeRemove to usePreventRemove.

    What is the root cause of that problem?

    PR #78291 changed DiscardChangesConfirmation from accepting a getHasUnsavedChanges callback (evaluated lazily on navigation) to a reactive hasUnsavedChanges boolean prop. The IOURequestStepDescription and IOURequestStepMerchant components were updated to use useState for this flag and explicitly call setHasUnsavedChanges(false) at the top of their submit handlers before any navigation occurs. However, IOURequestStepDistanceOdometer computes hasUnsavedChanges as a useMemo derived value that compares current readings against initial readings — and never resets it during submission.

    When the user enters odometer readings and submits, navigateToNextPage() stores the data in Onyx and triggers navigation, but hasUnsavedChanges remains true because startReading/endReading still differ from initialReadings (which are empty strings for a new expense).

    On web, the transitionStart listener in DiscardChangesConfirmation/index.tsx fires for all navigation transitions (forward and backward). It sees the stale hasUnsavedChanges=true, calls window.history.go(1) to undo the navigation, and shows the discard modal.

    On native, usePreventRemove(hasUnsavedChanges, ...) blocks the screen removal because hasUnsavedChanges is still true when the navigation stack tries to remove the odometer screen.

    The only path that handles this is shouldSkipConfirmation, which calls setShouldEnableDiscardConfirmation(false) — but the normal creation flow does not.

    Relevant code:

    • src/pages/iou/request/step/IOURequestStepDistanceOdometer.tsx — hasUnsavedChanges computed at ~line 500, navigateToNextPage at ~line 354 never resets it
    • src/pages/iou/request/step/DiscardChangesConfirmation/index.tsx — transitionStart listener at ~line 35 triggers the modal on forward navigation
    • src/pages/iou/request/step/IOURequestStepMerchant.tsx:107 and IOURequestStepDescription.tsx:138 — both correctly call setHasUnsavedChanges(false) at the start of their submit handlers

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

    In IOURequestStepDistanceOdometer.tsx, call setShouldEnableDiscardConfirmation(false) at the top of navigateToNextPage() for all submission paths (not just the shouldSkipConfirmation branch). This mirrors the pattern used by Description and Merchant components and ensures hasUnsavedChanges evaluates to false before any navigation is triggered.

    Move the existing setShouldEnableDiscardConfirmation(false) call (currently inside if (shouldSkipConfirmation) at ~line 422) to the beginning of navigateToNextPage() so it runs unconditionally.

    What alternative solutions did you explore? (Optional)

    1. Reset initialReadings to match current readings — calling setInitialReadings({start: startReading, end: endReading}) would make the useMemo evaluate to false. However, this is semantically misleading (initial readings haven't changed) and adds unnecessary state coupling.

    2. Convert to explicit useState for hasUnsavedChanges like Description/Merchant — this would work but requires a larger refactor of the Odometer component. Using the existing shouldEnableDiscardConfirmation state is simpler and already part of the hasUnsavedChanges computation.


    Next Steps for Contributor+ team: Reply with @MelvinBot implement this to create a draft PR, or @MelvinBot <your feedback> to refine this analysis.

  8. 1 remaining item

  9. added and removed on Mar 5, 2026
  10. Julesssss commented on Mar 5, 2026

    @Julesssss
    Contributor

    Not a blocker, Odometer feature behind beta.

  11. DylanDylann commented on Mar 5, 2026

    @DylanDylann
    Contributor

    @MelvinBot Implement this

  12. MelvinBot commented on Mar 5, 2026

    @MelvinBot
    Contributor

    🤖 Draft PR created: #84255
    Please mark it as "Ready for review" when it is ready for review.

  13. DylanDylann commented on Mar 5, 2026

    @DylanDylann
    Contributor

    Discussing the next step here

  14. added
    ReviewingHas a PR in review
    and removed on Mar 5, 2026
  15. Julesssss commented on Mar 5, 2026

    @Julesssss
    Contributor

    We reverted the discard modal bug here.

  16. Julesssss commented on Mar 12, 2026

    @Julesssss
    Contributor

    Fixed here

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.EngineeringReviewingHas a PR in reviewWeeklyKSv2

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions