Skip to content

[$175] [CFI] Show the "Discard changes?" modal when exiting the Create expense flow with unsaved edits #104579

Open
1 of 1 issue completed

Description

@melvin-bot

Problem

When you create an expense, it's easy to lose your work. For example, you add a receipt and start coding the expense on the confirmation page. Then you click outside the RHP or navigate away. The draft disappears with no warning. Classic keeps the draft open until you close it on purpose, so migrating customers notice this.

Solution

If you've made edits anywhere in the Create expense flow and try to leave, show the existing "Discard changes?" modal. Leaving includes clicking outside the RHP, pressing back or Esc, swiping back, or switching tabs.

Use the existing useDiscardChangesConfirmation hook. It already runs on these steps:

  • IOURequestStepAmount (Manual)
  • IOURequestStepHours (Time)
  • IOURequestStepDistance, IOURequestStepDistanceMap, IOURequestStepDistanceManual, IOURequestStepDistanceOdometer (Track distance)
  • DynamicIOURequestStepMerchant, DynamicIOURequestStepDescription

Extend it to the steps that don't use it yet:

  • The Scan step, once a receipt is attached
  • IOURequestStepConfirmation, once you've attached a receipt or edited any field (amount, merchant, category, tag, description, date, etc.)

Expected behavior

  1. Click the FAB > Create expense > Scan and attach a receipt.
  2. On the confirmation page, set a category or description.
  3. Click outside the RHP.
  4. The "Discard changes?" modal appears.
    • Choosing Discard closes the flow and clears the draft.
    • Choosing Cancel keeps you on the page with your edits.
  5. If you haven't made any edits, the flow closes with no modal.

Reported in Slack.

Upwork Automation - Do Not Edit
Issue OwnerCurrent Issue Owner: @FitseTLT

Activity

  1. changed the title [-]Show the "Discard changes?" modal when exiting the Create expense flow with unsaved edits[/-] [+][CFI] Show the "Discard changes?" modal when exiting the Create expense flow with unsaved edits[/+] on Oct 21, 2026
  2. trjExpensify commented on Oct 21, 2026

    @trjExpensify
    Contributor

    @MelvinBot proposal a solution to this.

  3. MelvinBot commented on Oct 21, 2026

    @MelvinBot
    Contributor

    Proposal

    What is the root cause of that problem?

    Neither IOURequestStepConfirmation nor IOURequestStepScan calls useDiscardChangesConfirmation, so nothing blocks the RHP from closing. The confirmation page also writes every edit straight to the draft transaction in Onyx. There's no local "dirty" state to compare against, and auto-fill components (category defaults, tax, distance merchant) write to the same draft.

    Two limits in the hook also matter once it's added to the confirmation page:

    1. On web it blocks every removal of the screen, so header back to an earlier step would also prompt, even though nothing is lost.
    2. It only prompts on the focused screen. If a sub-step (category, tag, date) sits on top of confirmation and you click outside the RHP, nothing prompts.

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

    1. Extend the hook (build on Fix discard modal in embedded manual, which already adds most of this):
      • Add shouldPromptForNavigationAction(action) so only actions that close the flow prompt. That covers the overlay click, Esc (DISMISS_MODAL), and a browser back or swipe that leaves the RHP. A pop to an earlier step doesn't prompt.
      • Add shouldPromptWhenUnfocused so confirmation still prompts when a sub-step is on top.
      • On Discard, close the flow with Navigation.closeRHPFlow() instead of re-dispatching the blocked action. The overlay sends a GO_BACK without a target, and re-dispatching it would likely pop just one screen and leave the RHP open.
    2. Track real edits on the draft. Add a hasUserEdits flag to the draft transaction. Set it only from user-driven handlers: the inline fields, sub-step save handlers, billable/reimbursable toggles, and receipt replace or drop. Never set it from the auto-fill setters.
    3. Confirmation page: call the hook with:
      • getHasUnsavedChanges: hasReceipt(transaction) || transaction.hasUserEdits
      • onConfirm (Discard): clear the drafts with clearMoneyRequest(OPTIMISTIC_TRANSACTION_ID, [OPTIMISTIC_TRANSACTION_ID, ...draftTransactionIDs]) after the RHP closes, so the page doesn't flash empty.
      • Call suppressDiscardPrompt() before onConfirm, onSendMoney, and "Remove expense", so submitting never prompts.
    4. Scan step: single scan goes straight to confirmation, so step 3 covers it. Add the hook to ScanGlobalCreate for multi-scan, where receipts pile up on Scan itself. Use "multi-scan is on and any draft has a receipt" as the dirty check, and pass onTabSwitchDiscard so switching tabs prompts too. Also prompt when you're back on Scan with a receipt still attached and close the RHP.
    5. Tests: unit tests for the action filter and the unfocused prompt. UI tests for the issue's steps: Discard closes the flow and clears the draft, Cancel keeps your edits, and no edits means no modal.
    Code references

    Not verified at runtime: that overlay click followed by Discard on a two-screen stack pops one screen instead of closing the RHP. This comes from reading the code (StackRouter turns an untargeted GO_BACK into a POP when the stack has more than one screen).

    Related work: Fix discard modal in embedded manual is open and adds shouldPromptForNavigationAction, shouldPromptWhenUnfocused, and the suppressDiscardPrompt wrapping of onConfirm/onSendMoney for the embedded Manual confirmation. Land it first, or coordinate with its author, to avoid conflicts. The hook was first added to IOU steps in fix: Show discard changes modal on browser/device back in money-request flows.

    What alternative solutions did you explore? (Optional)

    • One guard on the money request navigator that checks the draft. This covers every step and sub-step in one place, but it duplicates the per-step hook and makes tab switching and submit suppression harder.
    • Snapshot the draft on mount and compare. This was rejected because auto-fill setters write to the draft and would trigger false prompts.

    Next Steps for Contributor+ team:
    To accept: @MelvinBot implement [this](https://github.com/Expensify/App/issues/104579) to create a draft PR.
    To refine: @MelvinBot <your feedback>
    To reject: Explain why you are rejecting Melvin's proposal.


    view run

  4. added
    ExternalAdded to denote the issue can be worked on by a contributor
    on Oct 26, 2026
  5. melvin-bot commented on Oct 26, 2026

    @melvin-bot
    Author

    Triggered auto assignment to Contributor-plus team member for initial proposal review - @gijoe0295 (External)

  6. changed the title [-][CFI] Show the "Discard changes?" modal when exiting the Create expense flow with unsaved edits[/-] [+][$175] [CFI] Show the "Discard changes?" modal when exiting the Create expense flow with unsaved edits[/+] on Oct 26, 2026
  7. melvin-bot commented on Oct 26, 2026

    @melvin-bot
    Author
  8. trjExpensify commented on Oct 26, 2026

    @trjExpensify
    Contributor

    @gijoe0295 can you review Melvin's proposal and progress a PR today please? Thanks!

  9. 7 remaining items

  10. melvin-bot commented on Nov 4, 2026

    @melvin-bot
    Author

    @gijoe0295 Eep! 4 days overdue now. Issues have feelings too...

  11. gijoe0295 commented on Nov 4, 2026

    @gijoe0295
    Contributor

    @trjExpensify I'm trying to simplify the PR (~1.6k changes now) by re-evaluating the scope. Need your input.

    When should the modal show?

    1. Prompt whenever leaving Confirmation page . Simple but two issues:
      • Going back from Confirmation in a multi-step flow (e.g., Per-diem) shows the modal even when changes are not lost
      • Dismiss RHP in the middle of the flow (e.g., Sub-rate step in Per-diem) does NOT show the modal
    2. Prompt whenever dismissing RHP from any step or leaving the first step in the flow. This solves the above issues.

    I wonder if Option 1 works so we can reduce ~60% changes and avoid regressions.

    Which flows should this apply?

    The PR currently covers:

    • Create expense: manual, scan (incl. multi-scan), per-diem, time.
    • Track distance: map, manual, odometer. GPS and "Save for later" odometer don't count because they're persisted.
    • Split expense
    • Pay someone
    • Send invoice
    • Drag-n-drop receipt
    • Native share receipt

    Anything to omit?

  12. trjExpensify commented on Nov 4, 2026

    @trjExpensify
    Contributor

    Going back from Confirmation in a multi-step flow (e.g., Per-diem) shows the modal even when changes are not lost

    Ah, the Per diem flow is being refactored here, you could wait and follow-up once that PR lands?

  13. jeffrey-graham commented on Nov 7, 2026

    @jeffrey-graham
  14. melvin-bot commented on Nov 7, 2026

    @melvin-bot
    Author
  15. self-assigned this
    on Nov 9, 2026
  16. gijoe0295 commented on Nov 16, 2026

    @gijoe0295
    Contributor

    Split PRs plan

    Ship by guards & pages to apply.

    1. Discard from a stacked RHP closes the whole RHP: Discarding currently only close the top page, we need to close the whole RHP to fully dismiss the expense creation flow. Discard changes confirmation closes the whole RHP #106972
    2. Dirty detection: hasUserEdits draft flag that tells the user's input apart from default values we automatically fill in. Detect user edits in create expense drafts #106978
    3. Flow-root mode for useDiscardChangesConfirmation: The guard currently only applies to the focused screen, a multi-step flow won't trigger the guard if other steps are open on top. We need to keep a screen's guard active while later steps cover it. [HOLD 106972] Let the first screen of a flow guard the whole flow in useDiscardChangesConfirmation #106977. Depends on 1.
    4. Adds the guard to the Start steps (Manual, Scan, Per diem, Time; Split Distance, Pay, Split). Depends on 1, 2 and 3.
    5. Add the guard to Track distance and Send invoice. Depends on 4.
    6. Add the guard to Drag & drop and Native share. Depends on 4.
  17. self-assigned this
    on Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

ExternalAdded to denote the issue can be worked on by a contributorImprovementItem broken or needs improvement.ReviewingHas a PR in reviewWeeklyKSv2

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions