Skip to content

fix: Show discard changes modal on browser/device back in money-request flows - #95076

Merged
MonilBhavsar merged 26 commits into
Expensify:mainfrom
TaduJR:fix-Discard-changes-modal-doesnt-show-up-when-going-back-with-the-browser-back-button
Jul 13, 2026
Merged

fix: Show discard changes modal on browser/device back in money-request flows#95076
MonilBhavsar merged 26 commits into
Expensify:mainfrom
TaduJR:fix-Discard-changes-modal-doesnt-show-up-when-going-back-with-the-browser-back-button

Conversation

@TaduJR

@TaduJR TaduJR commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Explanation of Change

This PR makes the "Discard changes" confirmation appear when a user leaves a money-request step that has unsaved input by pressing the browser back button or the Android device back button. Before this change those two back paths left the step and dropped the input with no prompt.

How it works

  • beforeRemove propagation into nested navigators. A patch to @react-navigation/core (useOnPreventRemove) makes shouldPreventRemove also recurse into a kept route whose nested state changed, instead of only checking routes removed by key. A browser back dispatches a RESET that keeps the outer RHP route but replaces its nested tab state, so without this patch the event never reached the focused tab screen and it could not prompt.
  • Web hook (useDiscardChangesConfirmation/index.ts). It intercepts that RESET through beforeRemove, shows the discard modal (configured not to add a browser history entry of its own), and restores the URL with history.go(1), because the browser entry has already moved by the time beforeRemove fires. It tracks this restore round-trip so the follow-up popstate event is not mistaken for a fresh back press.
  • Native hook (index.native.ts). It mirrors the web behavior with usePreventRemove, plus a BackHandler listener for the tab-switch hardware back, which is an index-only tab change that never fires beforeRemove.
  • backBehavior: 'none' on OnyxTabNavigator. Previously the navigator used 'initialRoute', so a back press returned to the initial tab before leaving. 'none' keeps the tab history at a single entry, so a back press leaves the whole flow directly, and it behaves the same on web and native.
  • Tab-switch guard (TabSwitchGuardContext and OnyxTabNavigator.handleTabPress). The odometer tab holds its reading in local state that a tab switch would drop, so switching away from it is intercepted and the same discard modal is shown. Other tabs share their draft or reset by design, so they have nothing to lose and stay unguarded.
  • Committed-baseline dirty predicates (MoneyRequestUtils). getAmountHasUnsavedChanges, getStringFieldHasUnsavedChanges, and getWaypointsHasUnsavedChanges decide whether a step is dirty by comparing the current value against the last committed value, which is empty when creating and the saved value when editing. That keeps the check correct in both the create and edit flows.

Deploy blockers fixed

Fixed Issues

$ #94844
$ #94845
$ #94910
$ #94861
$ #84246
PROPOSAL:

Tests

  1. [Due for payment 2026-07-23] Expense - Discard changes modal does not appear after proceeding to next page and exiting it #94844 (no modal after Next, then back)

    1. Open FAB > Create expense > Manual.
    2. Enter an amount.
    3. Click the RHP back button, verify the "Discard changes" modal appears, then click Cancel.
    4. Click Next.
    5. Click the RHP back button twice.
    6. Verify the "Discard changes" modal appears.
  2. [Due for payment 2026-07-23] Expense - Discard changes modal does not appear when amount is zero #94845 (no modal when the amount is zero)

    1. Open FAB > Create expense > Manual.
    2. Enter a non-zero amount.
    3. Click the RHP back button, verify the "Discard changes" modal appears, then click Cancel.
    4. Change the amount to 0.
    5. Click the RHP back button.
    6. Verify the "Discard changes" modal appears.
  3. [Due for payment 2026-07-23] Expense - "Discard Changes" modal not displayed after editing waypoints in distance expense. #94910 (no modal after editing waypoints)

    1. Open FAB > Track Distance > Map.
    2. Enter start and stop waypoints.
    3. Click the tab back button or the browser back button, verify the "Discard changes" modal appears, then dismiss it.
    4. Click Next.
    5. On the confirmation page, click the Distance field.
    6. Edit any waypoint.
    7. Click the tab back button or the browser back button.
    8. Verify the "Discard changes" modal appears and the edit is not silently discarded back to the confirmation page.
  4. [Due for payment 2026-07-23] Expense - Different behavior of "Discard Changes" when using app´s or device´s back button. #94861 (device and app back behave the same after Discard, Android)

    1. Open the app on Android and tap FAB > Create Expense.
    2. On the Manual tab (if already there, switch to another tab and back), enter an amount.
    3. Press the device back button, then tap Discard changes.
    4. Verify the expense creation flow closes completely.
    5. Return to the Manual tab, enter an amount, and tap the in-app back button.
    6. Tap Discard changes and verify the flow closes.
    7. Verify both back methods behave the same, closing the flow rather than returning to a previous tab.
  • Verify that no errors appear in the JS console

Offline tests

Same as tests

QA Steps

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

  • 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
    • MacOS: Desktop
  • 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 verified there are no new alerts related to the canBeMissing param for useOnyx
  • 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
      • If any non-english text was added/modified, I used JaimeGPT to get English > Spanish translation. I then posted it in #expensify-open-source and it was approved by an internal Expensify engineer. Link to Slack message:
    • 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
Android-Native.mp4
Android: mWeb Chrome
iOS: Native
iOS: mWeb Safari
iOS-Safari.mp4
MacOS: Chrome / Safari
Mac-Chrome.mp4

@TaduJR
TaduJR requested review from a team as code owners July 1, 2026 18:04
@melvin-bot
melvin-bot Bot requested review from JmillsExpensify and jayeshmangwani and removed request for a team July 1, 2026 18:04
@melvin-bot

melvin-bot Bot commented Jul 1, 2026

Copy link
Copy Markdown

@jayeshmangwani 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 the request for review from a team July 1, 2026 18:04

@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: 4120f352ca

ℹ️ 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/iou/request/step/IOURequestStepDistanceMap.tsx Outdated
…-changes-modal-doesnt-show-up-when-going-back-with-the-browser-back-button

# Conflicts:
#	src/libs/MoneyRequestUtils.ts
@TaduJR
TaduJR marked this pull request as draft July 1, 2026 18:10
@codecov

codecov Bot commented Jul 1, 2026

Copy link
Copy Markdown

Codecov Report

✅ Changes either increased or maintained existing code coverage, great job!

Files with missing lines Coverage Δ
...ooks/useDiscardChangesConfirmation/index.native.ts 100.00% <100.00%> (ø)
src/hooks/useDiscardChangesConfirmation/index.ts 95.55% <100.00%> (+0.10%) ⬆️
src/libs/MoneyRequestUtils.ts 91.86% <100.00%> (+1.58%) ⬆️
...rc/libs/Navigation/OnyxTabNavigatorConfig/index.ts 100.00% <100.00%> (ø)
src/libs/TransactionUtils/index.ts 89.71% <ø> (ø)
src/pages/iou/request/step/IOURequestStepHours.tsx 81.08% <50.00%> (ø)
...ges/settings/Agents/Fields/EditAgentAvatarPage.tsx 0.00% <0.00%> (ø)
...rc/pages/iou/request/step/IOURequestStepAmount.tsx 87.38% <60.00%> (+0.90%) ⬆️
.../iou/request/step/IOURequestStepDistanceManual.tsx 0.00% <0.00%> (ø)
...es/settings/Profile/Avatar/useProfileAvatarForm.ts 0.00% <0.00%> (ø)
... and 3 more
... and 18 files with indirect coverage changes

@TaduJR

TaduJR commented Jul 1, 2026

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: 2c2beeadbd

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

…-changes-modal-doesnt-show-up-when-going-back-with-the-browser-back-button
@TaduJR

TaduJR commented Jul 11, 2026

Copy link
Copy Markdown
Contributor Author

@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: 5a0e627ef3

ℹ️ 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/libs/MoneyRequestUtils.ts Outdated
@TaduJR

TaduJR commented Jul 11, 2026

Copy link
Copy Markdown
Contributor Author

@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: f91d155095

ℹ️ 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/libs/Navigation/OnyxTabNavigatorConfig/index.ts
@TaduJR
TaduJR requested a review from MonilBhavsar July 11, 2026 09:21

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

Looking good otherwise

Comment thread src/libs/MoneyRequestUtils.ts Outdated
Comment thread src/libs/MoneyRequestUtils.ts Outdated
@MonilBhavsar

Copy link
Copy Markdown
Contributor

Thanks!

@MonilBhavsar
MonilBhavsar merged commit e287da0 into Expensify:main Jul 13, 2026
34 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

🚧 MonilBhavsar has triggered a test Expensify/App build. You can view the workflow run here.

@OSBotify

Copy link
Copy Markdown
Contributor

✋ This PR was not deployed to staging yet because QA is ongoing. It will be automatically deployed to staging after the next production release.

@ZhenjaHorbach

Copy link
Copy Markdown
Contributor

We have a crash when we try to open Create expense or Track distance flow
I'm pretty sure it's related to this PR

@TaduJR
Can you check this, please?

2026-07-14.13.23.56.mov

@OSBotify

Copy link
Copy Markdown
Contributor

🚀 Deployed to staging by https://github.com/MonilBhavsar in version: 9.4.34-0 🚀

platform result
🕸 web 🕸 success ✅
🤖 android 🤖 success ✅
🍎 iOS 🍎 success ✅

@MelvinBot

Copy link
Copy Markdown
Contributor

🤖 I reviewed the changes in this PR against Expensify's help site files under App/docs/articles.

No help site changes are required, and no draft docs PR was created.

This PR is a UX/navigation bug fix: it makes the existing Discard changes confirmation modal appear when a user leaves a money-request step that has unsaved input by pressing the browser back button or the Android device back button (plus the related dirty-check and backBehavior fixes for the linked deploy blockers). It does not add, rename, or remove any user-facing feature, tab, setting, or button, and it does not change any documented workflow.

Why no docs update is needed
  • No article in App/docs/articles documents back-button behavior, the "Discard changes" confirmation, or unsaved-input prompts in the expense-creation flow — I searched for discard, unsaved, back button, and confirmation modal and found no relevant references (the one "discard" hit in Distance-Expenses.md is about deleting a trip via the trash icon, unrelated to this change).
  • The expense-creation help articles describe what fields to fill in and how to create an expense, not the low-level navigation/confirmation micro-interactions this PR changes.
  • The discard-confirmation prompt is a standard, self-explanatory safety interaction; surfacing it consistently across back paths restores expected behavior rather than introducing something that needs to be explained.

If you feel a specific article should call out this behavior, let me know which one and I'll draft the update.

@TaduJR

TaduJR commented Jul 14, 2026

Copy link
Copy Markdown
Contributor Author

We have a crash when we try to open Create expense or Track distance flow
I'm pretty sure it's related to this PR
@TaduJR
Can you check this, please?

Checking

@TaduJR

TaduJR commented Jul 14, 2026

Copy link
Copy Markdown
Contributor Author

@OSBotify

Copy link
Copy Markdown
Contributor

🚀 Deployed to production by https://github.com/roryabraham in version: 9.4.34-14 🚀

platform result
🕸 web 🕸 success ✅
🤖 android 🤖 failure ❌
🍎 iOS 🍎 failure ❌

@OSBotify

Copy link
Copy Markdown
Contributor

🚀 Deployed to production by https://github.com/roryabraham in version: 9.4.34-14 🚀

platform result
🕸 web 🕸 success ✅
🤖 android 🤖 success ✅
🍎 iOS 🍎 success ✅

Bundle Size Analysis (Sentry):

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.

8 participants