Skip to content

[$250] Spends - Selection mode does not automatically exit when selection count drops to zero #93911

Closed

Description

@applause-bot

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.4.10-2
Reproducible in staging?: Yes
Reproducible in production?: Yes
If this was caught during regression testing, add the test name, ID and link from BrowserStack: #92809
Email or phone of affected tester (no customers): applausetester+bm890@applause.expensifail.com
Issue reported by: Applause Internal Team
Bug source: Pull Request QA execution
Device used: Web, android
App Component: Chat Report View

Action Performed:

  1. Go to staging.new.expensify.com
  2. Go to Spends page.
  3. Long-press a row to enter selection mode.
  4. Deselect rows until the selection count drops to zero.

Expected Result:

Selection mode should automatically exit when selection count drops to zero.

Actual Result:

Selection mode remains active when selection count is zero.

Workaround:

Unknown

Platforms:

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

Screenshots/Videos

Bug7184107_1781737576612.1.mp4

View all open jobs on GitHub

Applause Internal Information
Exported by: Mita Rachim
Bug ID: 7184107

Upwork Automation - Do Not Edit

Activity

  1. added
    BugSomething is broken. Auto assigns a BugZero manager.
    on Jun 18, 2026
  2. applause-bot commented on Jun 18, 2026

    @applause-bot
    Author

    While Applause doesn't embed attachments for this bug, here are the links for quick view:
    https://platform.applause.com/services/links/v1/external/5fe7fda61f4931a06796e5cf3940899022b6b7660b8481a6817bb2c446075039

  3. MelvinBot commented on Jun 18, 2026

    @MelvinBot
    Contributor

    Proposal

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

    On narrow/mobile layout, after long-pressing to enter selection mode and then deselecting rows until the selected count reaches zero, selection mode stays active. It should automatically exit when the selection drops to zero.

    What is the root cause of that problem?

    The effect that exits mobile selection mode when the selection becomes empty is gated on three conditions, and the third one (shouldTurnOffSelectionMode) is never true for a manual deselect:

    src/components/Search/SearchWriteActionsProvider.tsx:282

    if (selectedKeys.length === 0 && isMobileSelectionModeEnabled && shouldTurnOffSelectionMode) {
        turnOffMobileSelectionMode();
    }

    shouldTurnOffSelectionMode is only set to true by explicit bulk-action clears (clearSelectedTransactions(..., true) — approve/pay/export, etc. at SearchSelectionProvider.tsx:136). Every per-row toggle/deselect instead goes through applySelection, which hardcodes the flag to false on every commit:

    src/components/Search/SearchSelectionProvider.tsx:95

    return {
        ...prevState,
        selectedTransactions,
        ...
        shouldTurnOffSelectionMode: false,
    };

    So when you manually deselect down to zero: selectedKeys.length === 0 ✅ and isMobileSelectionModeEnabled ✅, but shouldTurnOffSelectionMode === false ❌, so turnOffMobileSelectionMode() is never called and selection mode stays on.

    Note the wide-layout branch already does the right thing — it turns selection mode off on empty with only the two-condition check (no shouldTurnOffSelectionMode gate): SearchWriteActionsProvider.tsx:312-315.

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

    Drop the shouldTurnOffSelectionMode condition from useTurnOffSelectionModeWhenEmpty so it exits selection mode whenever the selection is empty while in mobile selection mode and focused — mirroring the wide-layout branch:

    if (selectedKeys.length === 0 && isMobileSelectionModeEnabled) {
        turnOffMobileSelectionMode();
    }

    Update the effect deps accordingly (drop shouldTurnOffSelectionMode). After this, the shouldTurnOffSelectionMode flag is still useful for the explicit bulk-action clear path, but it no longer blocks the manual deselect-to-zero case.

    Investigation details, alternatives & verification note
    • The effect lives in useTurnOffSelectionModeWhenEmpty, wired in at lines 552-553 of the same file.
    • The deselect itself correctly empties the map (prepareTransactionsList deletes the key in selectionBuilders.ts), so selectedTransactions does become {} — the only thing blocking exit is the guard.
    • Alternative considered: set shouldTurnOffSelectionMode: true inside applySelection when the resulting map is empty. This is more surgical but couples a state-setter to selection-mode UX and risks the flag lingering true into the next render; removing the guard (matching the already-correct wide-layout branch) is simpler and more consistent.
    • Verification caveat: this bug reproduces in production per the report, and the pre-/post-PR effect bodies are byte-identical, so this is most likely a pre-existing latent bug that PR Search selection: move write-side off <Search> onto provider/controller #92809's test step 6 surfaced rather than a behavior change introduced by that PR. The static code path that blocks exit is deterministic, but a quick narrow-layout manual repro (deselect last row, confirm selection mode persists) is worth doing before merging the fix.

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

  4. added
    ExternalAdded to denote the issue can be worked on by a contributor
    on Jun 18, 2026
  5. MobileMage commented on Jun 18, 2026

    @MobileMage
    Contributor

    Proposal

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

    On a small-screen / mobile search view (the "Spends" list), long-pressing a row enters mobile selection mode. When the user then deselects rows one-by-one until the selection count reaches zero, selection mode stays active instead of automatically exiting. The expectation (and the behavior of every other selectable list in the app) is that emptying the selection by hand should drop the user back out of selection mode.

    What is the root cause of that problem?

    The Spends list renders through <Search>, which mounts SearchWriteActionsProvider. That provider owns the two effects that drive mobile selection mode. The one responsible for turning the mode off when the selection empties is useTurnOffSelectionModeWhenEmpty, and it only fires turnOffMobileSelectionMode() when an extra flag, shouldTurnOffSelectionMode, is also true:

    const selectedKeys = Object.keys(selectedTransactions).filter((transactionKey) => selectedTransactions[transactionKey]);
    if (selectedKeys.length === 0 && isMobileSelectionModeEnabled && shouldTurnOffSelectionMode) {
    turnOffMobileSelectionMode();
    }

    const selectedKeys = Object.keys(selectedTransactions).filter((k) => selectedTransactions[k]);
    if (selectedKeys.length === 0 && isMobileSelectionModeEnabled && shouldTurnOffSelectionMode) {
        turnOffMobileSelectionMode();
    }

    The problem is that shouldTurnOffSelectionMode is never true on the manual-deselect path. Deselecting a row goes through toggle → applySelection, and every commit in applySelection hard-resets the flag back to false:

    return {
    ...prevState,
    selectedTransactions,
    areAllMatchingItemsSelected,
    selectedReports: options?.data ? deriveSelectedReports(selectedTransactions, options.data) : prevState.selectedReports,
    shouldTurnOffSelectionMode: false,
    };

    The only thing that sets shouldTurnOffSelectionMode: true is clearSelectedTransactions(..., true), and every caller of that is a bulk-action completion path — submitting a hold/reject reason, export, change-report, edit-multiple, etc. (useSelectedTransactionsActions, useExportActions, useSearchBulkActions, SearchHoldReasonPage...). In other words, this flag exists to wipe the selection and leave selection mode after an action runs; it was never meant to cover "the user emptied the selection themselves." So when the last checkbox is unticked, the effect re-runs (its deps include selectedTransactions), sees selectedKeys.length === 0, but shouldTurnOffSelectionMode is already false, and the mode is left on.

    The sibling effect useSyncMobileSelectionModeWithScreenSize does not save us either: on the small-screen path it only ever turns the mode on, and its only zero-count turnOffMobileSelectionMode() is gated behind !isSmallScreenWidth. Its dependency array is [isSmallScreenWidth] only, so it deliberately never re-runs on selection changes.

    const selectedKeys = Object.keys(selectedTransactions).filter((transactionKey) => selectedTransactions[transactionKey]);
    if (!isSmallScreenWidth) {
    if (selectedKeys.length === 0 && isMobileSelectionModeEnabled) {
    turnOffMobileSelectionMode();
    }
    return;
    }
    if (selectedKeys.length > 0 && !isMobileSelectionModeEnabled && !isSearchResultsEmpty) {
    turnOnMobileSelectionMode();
    }
    // We only want this effect to handle the switching of mobile selection mode state when screen size changes.
    // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [isSmallScreenWidth]);

    The net effect is that the Search write-side has no effect that mirrors the app-wide convention. Every other selectable list uses useHandleSelectionMode, which calls turnOffMobileSelectionMode() whenever selectedItems.length === 0 with no extra flag gate (only a wasSelectionOnRef guard so it doesn't fire on first entry). That hook is exactly what SelectionListWithModal wires up, and it is the behavior QA is comparing against.

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

    Make useTurnOffSelectionModeWhenEmpty exit selection mode when the selection empties on the small-screen path, regardless of shouldTurnOffSelectionMode. The shouldTurnOffSelectionMode branch should stay (it still serves the post-bulk-action "clear + exit" case where the selection is wiped programmatically), but we add the manual-deselect case alongside it so the effect matches the useHandleSelectionMode convention. Concretely, in SearchWriteActionsProvider.tsx, pass isSmallScreenWidth into the hook and broaden the condition:

    const selectedKeys = Object.keys(selectedTransactions).filter((k) => selectedTransactions[k]);
    if (selectedKeys.length === 0 && isMobileSelectionModeEnabled && (shouldTurnOffSelectionMode || isSmallScreenWidth)) {
        turnOffMobileSelectionMode();
    }

    This is safe because the body only runs after the screen is focused (the early isFocused return is kept) and only when nothing is selected and the mode is currently on — so it can't interfere with an in-flight reconcile (which leaves at least one key) or with entering selection mode (which always selects the long-pressed row first in turnOnSelectionMode, so selectedKeys is never empty at entry). It also doesn't disturb the wide-screen behavior, which is already handled by useSyncMobileSelectionModeWithScreenSize. Restricting the new branch to isSmallScreenWidth keeps the change scoped to the platform where mobile selection mode actually exists, and the effect already re-runs on selectedTransactions changes so the exit happens the moment the count hits zero.

    What alternative solutions did you explore? (Optional)

    I considered replacing the whole custom pair of Search effects with the shared useHandleSelectionMode(selectedKeys) hook for consistency, but the Search write-side intentionally keeps shouldTurnOffSelectionMode and the reconcile-aware select-all logic that useHandleSelectionMode doesn't know about, so swapping wholesale risks regressing the bulk-action and select-all flows; the targeted condition change keeps those intact while restoring the expected exit-at-zero behavior.

  6. added
    Help WantedApply this label when an issue is open to proposals by contributors
    on Jun 18, 2026
  7. ahmdshrif commented on Jun 18, 2026

    @ahmdshrif
    Contributor

    🚫 Duplicated proposal withdrawn by 🤖 ProposalPolice.

  8. lorretheboy commented on Jun 18, 2026

    @lorretheboy
    Contributor

    🚫 Duplicated proposal withdrawn by 🤖 ProposalPolice.

  9. melvin-bot commented on Jun 18, 2026

    @melvin-bot

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

  10. wildan-m commented on Jun 18, 2026

    @wildan-m
    Contributor

    Proposal

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

    On the Spends page (and other Search list views), after long-pressing a row to enter mobile selection mode and then deselecting rows until the selection count drops to zero, selection mode stays active. It should automatically exit once nothing is selected.

    What is the root cause of that problem?

    The Search selection feature has a dedicated effect whose job is exactly this — turn mobile selection mode off once nothing is selected:

    function useTurnOffSelectionModeWhenEmpty({isFocused, isMobileSelectionModeEnabled}: {isFocused: boolean; isMobileSelectionModeEnabled: boolean}) {
    const {selectedTransactions, shouldTurnOffSelectionMode} = useSearchSelectionContext();
    useEffect(() => {
    if (!isFocused) {
    return;
    }
    const selectedKeys = Object.keys(selectedTransactions).filter((transactionKey) => selectedTransactions[transactionKey]);
    if (selectedKeys.length === 0 && isMobileSelectionModeEnabled && shouldTurnOffSelectionMode) {
    turnOffMobileSelectionMode();
    }
    // `isFocused` is intentionally omitted from the deps: it is only read for the early-return guard above,
    // and we don't want the effect to re-run when focus changes.
    // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [selectedTransactions, isMobileSelectionModeEnabled, shouldTurnOffSelectionMode]);

    But it only fires when the shouldTurnOffSelectionMode flag is true:

    if (selectedKeys.length === 0 && isMobileSelectionModeEnabled && shouldTurnOffSelectionMode) {

    That flag is committed to selection state in applySelection, which is the write path every row/checkbox toggle (including deselection) goes through. applySelection hardcodes the flag to false on every commit:

    const applySelection: SearchSelectionActionsValue['applySelection'] = (updater, options) => {
    setSelectionState((prevState) => {
    const selectedTransactions = updater(prevState.selectedTransactions);
    if (selectedTransactions === prevState.selectedTransactions) {
    return prevState;
    }
    const totalSelectableItemsCount = options?.totalSelectableItemsCount;
    const areAllMatchingItemsSelected =
    totalSelectableItemsCount && totalSelectableItemsCount !== Object.keys(selectedTransactions).length ? false : prevState.areAllMatchingItemsSelected;
    return {
    ...prevState,
    selectedTransactions,
    areAllMatchingItemsSelected,
    selectedReports: options?.data ? deriveSelectedReports(selectedTransactions, options.data) : prevState.selectedReports,
    shouldTurnOffSelectionMode: false,
    };
    });
    };

    So when the user manually deselects rows down to zero, applySelection resets shouldTurnOffSelectionMode to false, and the turn-off effect never runs. The flag is only ever set to true by clearSelectedTransactions(..., true), which is called after bulk actions (reject/hold/edit-multiple) complete — i.e. only a programmatic clear can currently exit selection mode, not a manual deselect-to-zero.

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

    In applySelection (in src/components/Search/SearchSelectionProvider.tsx), instead of hardcoding shouldTurnOffSelectionMode: false in the committed state, derive it from whether the resulting selection is empty — set it to true when the new selectedTransactions map is empty and false otherwise. isEmptyObject is already imported in this file, so the value becomes isEmptyObject(selectedTransactions) (using the updated map computed a few lines above).

    const applySelection: SearchSelectionActionsValue['applySelection'] = (updater, options) => {
    setSelectionState((prevState) => {
    const selectedTransactions = updater(prevState.selectedTransactions);
    if (selectedTransactions === prevState.selectedTransactions) {
    return prevState;
    }
    const totalSelectableItemsCount = options?.totalSelectableItemsCount;
    const areAllMatchingItemsSelected =
    totalSelectableItemsCount && totalSelectableItemsCount !== Object.keys(selectedTransactions).length ? false : prevState.areAllMatchingItemsSelected;
    return {
    ...prevState,
    selectedTransactions,
    areAllMatchingItemsSelected,
    selectedReports: options?.data ? deriveSelectedReports(selectedTransactions, options.data) : prevState.selectedReports,
    shouldTurnOffSelectionMode: false,
    };
    });
    };

    With this change, when a toggle deselects the last selected row, the committed selection is empty, shouldTurnOffSelectionMode becomes true, and the existing useTurnOffSelectionModeWhenEmpty effect fires and exits mobile selection mode — matching the expected behavior.

    This also preserves the guard that protects entry into selection mode: long-pressing a row turns selection mode on and immediately toggles the long-pressed item, so the committed selection is non-empty and the flag stays false — the mode is not turned off the instant it is turned on.

    Compare branch: https://github.com/Expensify/App/compare/main...wildan-m:App:wildan/93911-exit-selection-mode-on-deselect-to-zero?expand=1

    What alternative solutions did you explore? (Optional)

    Removing the && shouldTurnOffSelectionMode condition from the turn-off effect so it fires whenever the selection is empty. This was rejected because that flag also guards the entry race: when selection mode is turned on, there can be a render where mobile selection mode is enabled but the selection map is still empty (before the long-pressed item's toggle commits). Without the flag, the effect would immediately turn selection mode back off. Driving the flag from the committed selection's emptiness in applySelection fixes the deselect-to-zero case while keeping that protection intact.

  11. changed the title [-]Spends - Selection mode does not automatically exit when selection count drops to zero[/-] [+][$250] Spends - Selection mode does not automatically exit when selection count drops to zero[/+] on Jun 18, 2026
  12. melvin-bot commented on Jun 18, 2026

    @melvin-bot
  13. 20 remaining items

  14. github-actions commented on Jun 19, 2026

    @github-actions
    Contributor

    ⚠️ @petrovichby Your proposal is a duplicate of an already existing proposal and has been automatically withdrawn to prevent spam. Please review the existing proposals before submitting a new one.

  15. github-actions commented on Jun 19, 2026

    @github-actions
    Contributor

    ⚠️ @petrovichby Your proposal is a duplicate of an already existing proposal and has been automatically withdrawn to prevent spam. Please review the existing proposals before submitting a new one.

  16. FitseTLT commented on Jun 19, 2026

    @FitseTLT
    Contributor

    I think this is expected cc @Expensify/product

  17. shawnborton commented on Jun 20, 2026

    @shawnborton
    Contributor

    cc @Expensify/design too but I do think this is expected.

  18. Pankaj-Sakariya commented on Jun 20, 2026

    @Pankaj-Sakariya

    🚫 Duplicated proposal withdrawn by 🤖 ProposalPolice.

  19. github-actions commented on Jun 20, 2026

    @github-actions
    Contributor

    ⚠️ @Pankaj-Sakariya Your proposal is a duplicate of an already existing proposal and has been automatically withdrawn to prevent spam. Please review the existing proposals before submitting a new one.

  20. Pankaj-Sakariya commented on Jun 20, 2026

    @Pankaj-Sakariya

    🚫 Duplicated proposal withdrawn by 🤖 ProposalPolice.

  21. github-actions commented on Jun 20, 2026

    @github-actions
    Contributor

    ⚠️ @Pankaj-Sakariya Your proposal is a duplicate of an already existing proposal and has been automatically withdrawn to prevent spam. Please review the existing proposals before submitting a new one.

  22. dannymcclain commented on Jun 22, 2026

    @dannymcclain
    Contributor

    Agree - this is expected/desired behavior.

  23. shawnborton commented on Jun 22, 2026

    @shawnborton
    Contributor

    Okay cool, I think we can close then.

  24. aimane-chnaif commented on Jun 22, 2026

    @aimane-chnaif
    Contributor

    Let's close this

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

Metadata

Metadata

Assignees

Labels

BugSomething is broken. Auto assigns a BugZero manager.DailyKSv2ExternalAdded to denote the issue can be worked on by a contributorHelp WantedApply this label when an issue is open to proposals by contributors

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions