Fix Back for drawers, modals, the period picker and URL rewrites

Merged
GA
2mo ago
+378-46

Stacked on #3885 (feat/bills-detail-drawer). This PR removes the two per-drawer cache flags that #3885 adds, so it depends on that branch. The diff shows only this PR's seven commits. Once #3885 lands, I'll rebase this onto main.

Turbo caches a page as it was left, and Back restores that copy. Dialogs came back with it in four ways:

  • A drawer still open when a link inside it left the page came back as a stray, non-modal <dialog open> over the list, with no backdrop and no focus trap. The transaction drawer (linked bill), the transfer drawer (account names) and the budget category drawer (View all transactions, each transaction) all have such links.
  • A drawer closed before leaving the page was still in turbo-frame#drawer, so Back restored it and auto-open showed it again. That happened after any navigation. I reproduced it with the transfer, transaction and bulk edit drawers. It follows from how every drawer is built.
  • A modal still open when a link inside it left the page came back too. A provider's Cancel does that when the modal was opened over another URL, such as the accounts list that the user menu's Settings opens (/accounts?return_to=...).
  • A modal closed with Escape had the same problem as a closed drawer. Only DS--dialog#close empties the modal frame, and Escape closes the dialog natively. The dialog's esc:DS--dialog#close hotkey is not a key name that @github/hotkey can match, so it never fires.

A drawer visited directly is the page itself, so Back should bring it back. It did, but not as a modal, because connect returned early on open.

A period pick had the opposite problem. Back reverted the URL but left the page on the new period. Both pickers, on the dashboard and on the account chart, navigate a frame with data-turbo-action, and Turbo promotes that frame visit to a page visit. The promoted visit renders nothing when the frame response's head lacks the page's data-turbo-track assets, and turbo_rails' frame layout never has them. So Turbo never records the new URL as rendered. Back to the old URL then counts as a same-page visit and does nothing. Leaving the page also caches it under the old URL, so going Back twice showed the wrong period as well. hotwired/turbo#1569, still open, fixes this in Turbo.

Five controllers also rewrote the URL behind Turbo's back with a raw history.replaceState:

  • auto-open, lazy-load and budget-filter passed {} as the state. Turbo only handles a popstate that carries its own state, so Back to that entry changed the URL and left the next page on screen. Bank sync with ?manage=1 (linked from the SnapTrade and Indexa Capital menus) and the Budgets "Over budget"/"On track" filter did this.
  • DS::Tabs and goals-filter kept Turbo's state, but Turbo still held the old URL as its location and as the key it caches the page under. Back to the new URL missed the cache and refetched, which dropped anything on screen that isn't in the URL. With the period fix, an account page showed the saved default period after a tab switch, a pick and Back.

Two of them also read the same param. "Manage Connections" in SnapTrade's item menu links to Bank sync with ?manage=1. The SnapTrade row (auto-open) and the Manage Connections disclosure inside it (lazy-load) each read manage and dropped it on connect, so only the first to connect saw it. On a Drive visit Stimulus connects in document order, so the row always won: Bank sync opened on the SnapTrade row with its connections list closed.

What changes

  • DS::Dialog marks itself data-turbo-temporary when the request fetches it into its own frame (turbo_frame_request_id == frame). That covers the drawer frame (ten views), the bulk edit drawer's frame and the modal frame. On a direct visit the dialog is the page, so it stays cached. The flags #3885 added to the bill drawer and the payment drawer are gone, because the default covers them.
  • Lunch Flow's link no longer opens the modal frame with turbo_action: "advance". In a frame visit like that, Turbo fires turbo:before-cache on the live page right after the frame renders, and that removes temporary elements, so its modal vanished as it opened. Nothing needed the advance. It came with the original integration (#259) with no reason given. lunchflow-preload only warms the account cache and never reads the URL. select_accounts renders without a layout, so reloading the advanced URL showed a blank page anyway. It was also the only other frame visit with an action. Without this change, the turbo:load fix below would bring its method selector back on Back as a stray dialog.
  • DS--dialog#connect removes an open attribute that came back from the cache, and auto-open then shows the dialog again as a modal. It removes the attribute instead of calling close(), so listeners get no close event for a dialog nobody closed. The Sankey feedback dialog, for example, sends "survey dismissed" to analytics on close.
  • The dialog's close event also empties the modal frame now, so Escape and every other way of closing count. close() still empties the frame right away, because a reload-on-close visit can cache the page before the close event fires.
  • application.js records the rendered URL on turbo:load, but only when a frame on the page shows the visit's URL. By then the promoted visit has cached the previous page under its own URL. The frame check skips a frame that finished loading after its page was gone. Turbo 8.0.13 doesn't cancel that frame's request, so it still pushes its URL and promotes a visit. Recording that URL would make Back show the page you left. The listener can go once Turbo ships #1569.
  • DS::Menu closes itself on connect. Back now restores the copy Turbo took for the pick, and the period menu is open in that copy. Turbo copies the page at the click and the frame just before its new content renders, while turbo:before-cache only cleans the live page. Every menu renders closed and disconnect already closes, so this changes only a menu restored from the cache.
  • utils/replace_url.js does to history what a Turbo replace visit does, without the request. It replaces the entry through Turbo's history, keeping its restoration identifier, and records the URL as the rendered one. The four controllers that still rewrite the URL use it. It needs the turbo:load fix: without it, a pick after a tab switch would leave the tab's URL as the rendered one, and Back to it would do nothing.
  • The SnapTrade panel renders its Manage Connections disclosure open when params[:manage] is "1". lazy-load already loads when its element connects open, so its param handling is gone (this panel was its only user), and auto-open is the one reader that drops the param. Indexa Capital's "Update credentials" linked to ?manage=1 too, although only SnapTrade reads it. With the disclosure opened by the server, that link would have opened SnapTrade's connections list and called the SnapTrade API, so it links to plain Bank sync now.

Before making this a default, I checked every DS::Dialog. All 8 disable_frame: true dialogs are auto_open: false, no lazy frame loads a dialog, and a full-page render never matches the request's frame id. So the default can't drop page content.

Screenshots

Each pair shows the page after Back, before this PR (left) and after (right).

The transfer drawer, then an account link, then Back. Before, the drawer comes back stray and non-modal over the list:

Back after leaving from the transfer drawer, before and after

An account page: pick 90D, then Back. Before, the URL goes back but the chart stays on 90D:

Back after a period pick, before and after

Bank sync with ?manage=1, then Preferences, then Back. Before, the URL is back on Bank sync but Preferences stays on screen:

Back to Bank sync after it dropped its manage param, before and after

Not a Back case: Accounts, SnapTrade's menu, Manage Connections. Before, the row opens with its connections list closed:

Bank sync from SnapTrade's Manage Connections, before and after

Validation

  • New test/system/dialog_back_test.rb. Each test failed before its change and passes after:
    • Transfer drawer, then an account link, then Back: no dialog. It failed 3/3 without the default.
    • Transfer drawer closed, then Reports, then Back: no dialog. It failed 3/3.
    • New transaction modal closed with Escape, then Reports, then Back: no dialog. It failed 3/3.
    • A transfer visited directly, then an account link, then Back: the drawer shows as dialog:modal.
    • The accounts list from the user menu's Settings, a provider's Link existing account modal (Wise), Cancel, then Back: no dialog. It failed 3/3 with the modal frame left out. From the bare /accounts, Cancel visits the same URL, Turbo turns that into a history replace, and Back skips it, so that path never showed the bug.
    • Lunch Flow's link opens its modal and the modal stays open. It failed 7/7 with advance put back. The test waits for turbo-frame#modal[complete] before each click: a link clicked while the frame's previous load is finishing loses its data-turbo-action, and without the waits the test passed when it should have failed.
  • New test/system/period_picker_back_test.rb. Each test failed before its part of the change and passes 3/3 after:
    • Account page: pick 90D, then Back shows 30D with the menu closed. It failed without the turbo:load fix, and on the open menu without the DS::Menu change.
    • Dashboard: pick 90D and open Transactions. Back shows 90D, and Back again shows 30D at /.
    • Dashboard: the picked frame's request is held until Transactions has loaded, then released. Back stays on Transactions. It failed with the URL recorded after every visit.
    • Account page: switch to Holdings, pick 90D, then Back shows Holdings and 30D. It failed without the URL rewrite fix below, because Back refetched and showed the saved default. It also fails on main with only that fix, where Back does nothing.
    • The tests wait for the promoted visit's turbo:load before they leave. Leaving earlier cancels that visit, which nobody clicks fast enough to do.
  • New test/system/replace_url_back_test.rb. Both tests fail on main and on this branch without the URL rewrite commit, and pass 3/3 after:
    • Account page: switch to Holdings, open Transactions, then Back. The page is Turbo's cached copy, because a mark set before leaving is still there, and it's still on Holdings. It failed without the rendered-URL line of replace_url.
    • Bank sync with ?manage=1: the SnapTrade row opens and the param goes. Open Preferences, then Back, and Bank sync is on screen. It failed with {} as the state.
  • New test in test/system/settings/providers_test.rb: Accounts, SnapTrade's menu, Manage Connections. Bank sync shows the SnapTrade row with its connections list open and loaded, at /settings/providers. list_connections is stubbed at the API boundary. It failed before the change, with the row open and the list closed.
  • New test/components/DS/dialog_test.rb:
    • The dialog is temporary in the drawer frame, in a frame of its own and in the modal frame.
    • It is not temporary on a direct visit.
  • Throwaway probes covered two more drawers. Each failed before the change and passed after. The budget category drawer was opened, then View all transactions, then Back. The transaction drawer was closed, then another page, then Back.
  • Another probe covered the Budgets filter: Over Budget, then Transactions, then Back. Without the URL rewrite commit, Transactions stayed on screen. With it, the budget comes back with the filter on.
  • #3885's Back tests in bill_drawer_test and its recurring_occurrences_controller_test pass using the default.
  • bin/rails test: 10339 runs, 0 failures, 0 errors (33 skips).
  • DISABLE_PARALLELIZATION=true bin/rails test:system: 195 runs, 1 failure.
    • loan_payoff_chart_test's dark-theme check fails the same way locally with this branch's JavaScript reverted.
    • trades_test "can create buy transaction" passed this time but is flaky here. Run alone, it failed 1 of 3 times with the URL rewrite commit and 1 of 4 without it.
  • RuboCop, erb_lint, npm run lint and Brakeman are clean. Biome doesn't cover app/components/, so the controllers were linted from copies under app/javascript/. DS/menu_controller.js reports nothing that wasn't there before.

Not done here

  • Emptying the modal frame on close is now redundant for modals fetched into it. It still covers a modal page visited directly, so it stays.
  • The dead esc:DS--dialog#close hotkey stays as it is.
  • The same Turbo bug leaves view.forceReloaded set after a pick (hotwired/turbo#1526), so later visits skip Turbo's scrolling. Here the window never scrolls (#main does), so only #anchor links are affected.
  • Indexa's "Update credentials" now lands on Bank sync with nothing open. Opening its own row would need an auto_open key on its FAMILY_PANELS entry and a matching param on the link.
  • The Manage Connections chevron uses group-open:rotate-90, which also matches the open SnapTrade row, so it points down while the list is closed. A named group would fix it. That predates this PR.

Summary by CodeRabbit

  • Bug Fixes
    • Improved browser Back and Forward behavior around dialogs, drawers, period selections, tabs, filters, and provider settings.
    • Dialogs and menus now restore in a more predictable state when navigating between pages or returning to cached pages.
    • SnapTrade connection details open when selected from “Manage Connections.”
    • URL updates for tabs and filters better preserve the current page and browser history.
    • Lazy-loaded details no longer open automatically based on a named URL parameter.