0017 — Portal view state in the address
Status: Accepted · Date: 2026-11-19
Context
Every portal list page kept its search, filters, sort and page in local React
state. A reload, a trip to another page and back, or a shared link all lost
them, so a coach re-typed the same History search after every detour. Two
pages had already solved this on their own: Profiles kept its athlete ×
exercise pairing in the query (use-matrix-selection.ts), and the Live Hub
keeps its screen there (use-hub-route.ts). Other pages read one-shot deep
links (?athlete=, ?tag=) and never wrote back.
The options were the URL query, sessionStorage, and an in-memory store per
page. Only the query survives a reload and travels with a copied link, and
it is what the browser's Back button already restores.
Decision
- A page's view lives in the URL query. Search text, filter facets, toggles, sort, table page, tab and shown period. Selections, open drawers, dialogs and per-browser preferences stay out.
- Written in place. Changes go through
history.replaceState(debounced), notrouter.push: filtering creates no history entries, and Back leaves the page. - Read once, on mount, through
useSearchParams. The page seeds ordinary React state from it;window.locationcan still hold the previous address during a link's transition. - One shared module and one codec per page.
apps/portal/src/app/dashboard/url-state.tsholds the hooks and tolerant readers; each page has a pure, tested<page>-view.ts. Defaults write nothing, so a page at its defaults has a clean address, and the key names are the same on every page (see Routing). - No sidebar memory. The sidebar opens a page at its defaults; whether it should reopen the last view is a separate product question.
Consequences
- Reload, Back and shared links show the same view on every list page.
- Athlete and tag names appear in the address and the browser's history, as
the existing
?athlete=deep link already did. - A link to the page that is already open does not re-seed it (read once).
DataTablegained a controlledpageStateprop; the dashboard layout wraps pages in a Suspense boundary foruseSearchParams.- No new dependency: the hooks are a thin layer over
useSearchParamsand the History API instead of a library such asnuqs.