Skip to content

0015. Pages, a tiny router, and one data-source module ​

  • Status: Accepted
  • Date: 2026-11-30

Context ​

Partner's page is the first real screen. It needs a path per page, and it must read and write through the one DataSource interface so the API-backed source can replace the mock without touching a page.

Decision ​

  • Router: the current path is a signal (router.ts), navigate changes it and the address bar, and the pages are one list in routes.tsx (route-match.ts is the pure matching, tested in Node). No library: there are a handful of exact paths, no nested routes. The query string is kept on navigate, so the mock switches survive.
  • / is a landing: it reads the session and replaces itself with that role's page (landingByRole). Both roles land on Partner's page (/spend); the admin reaches the dashboard (/dashboard) with Switch page, and either role can open the other's page by its address.
  • Switch page: a route with a nav label is listed in the header's "Switch page" navigation, driven by the registry (navLinks). Phone width: a compact row of plain links. From 56rem: a native <details> menu (Escape and leaving it close it). Links are real <a href> (they keep the query string); a plain click changes the page without a reload and moves focus to main. The registry is the one source of the menu: a route has either a nav label or a written navExcluded reason (the landing, Help and Emergency as footer links, the gallery), and a test holds every route to one or the other. audience: "admin" offers a page only to the admin (Reconcile; a role not yet known, or a session that can't be read, is offered nothing admin-only; the page checks the role itself too, so a typed address never opens it for a Member). desktopOnly marks a page that needs a wide screen: the phone's row of links leaves it out, and from 56rem the menu lists it. Both Dashboard and Can I spend this? are offered to both roles (the dashboard has a Member variant).
  • Desktop-only pages: DesktopOnly puts the page and a "This screen is desktop only" card in the page together and CSS shows one: below 56rem the card (what is going on in plain words and links to the pages that fit a phone), from 56rem the page. No script measures the screen, so nothing flashes. The 56rem is the same width the menu and the dashboard's desktop-only widgets use. Only the admin ever gets the card: a Member opening /reconcile is told "Not available" on any screen. Because the line is drawn in rem, a tablet held upright and a desktop browser zoomed in or set to large text both fall below it, so the card's words don't just say "use a computer": it says to turn a tablet sideways, or zoom out or make the text smaller, and then offers the pages that fit. (A page's own root font size doesn't move a rem media query, only the browser's setting and zoom do.) On paper the page is shown, not the card.
  • The shell's session read: the header and the menu need the person and their role. If that read fails the page still opens, the menu offers nothing admin-only and a quiet line says the name couldn't be loaded; it is asked again once after a couple of seconds and on the next page change.
  • Between the two layouts: Partner's page has "Open the dashboard" for the admin only (a Member has no dashboard link there), and the dashboard has "Open Can I spend this?" (LayoutLink). Addresses come from paths.ts.
  • A page = a controller + components. The controller (createSpendController) holds the page's signals and talks to the DataSource; every money rule is @home-budget/core (parse the amount, partnerHome, partnerCheck); components only show signals. Tests drive the controller in Node and the page in a browser.
  • src/data/index.ts chooses the source. The mock is the default and is loaded on demand (its own chunk). The ?mock=, ?as= and ?now= switches work only while the source is the mock (VITE_DATA_SOURCE not api), so a build on the API ignores them.
  • Focus after Check goes to the answer's heading (focusable by script only), and after Add it or a saved note to the confirmation (a role="status" region that exists before its text does). Layout effects do the moves, so there's no frame where focus is lost.

Consequences ​

  • Adding a page is a route (with a nav label if it belongs in Switch page) plus a component; /gallery and "page not found" use the same list.
  • The mock's in-memory writes live for one page load (one source per load), so they survive a change of route.
  • No undo yet: the data-source interface has no "remove entry".

Addendum: the API-backed source ​

The second DataSource implementation (src/data/api-data-source.ts) reads and writes through the Worker's /api.

  • Same interface, same contract. One list of tests (apps/web/test/contract/data-source-contract.ts) runs against the mock in Node and against the API source and the real Worker (seeded D1, inside workerd). A page that works on one works on the other.
  • Typed client, parsed answers. hc<AppType> (a type-only import of the Worker's route types; src/worker-types.d.ts stands in for the one workerd global they mention). Every answer is parsed with the wire schemas in @home-budget/core/wire (Zod, annotated z.ZodType<CoreType> so a type and its schema can't drift apart). A failure of any kind rejects with DataSourceError (words, plus status and code when the server gave them).
  • Server time. Every answer carries X-Server-Now; now() runs on from the latest one with a monotonic timer, so the device's clock is never read.
  • Who is signed in. In production Cloudflare Access adds the login; the app sends nothing. A local stub login and a pretend "now" exist only in a vite build --mode localdev build (npm run app:build:local): one dynamic import behind a build-time switch that vite.config.ts pins off in every other mode. test/bundle.test.ts builds both ways and looks for the login in the output.
  • Writes pulled forward. The two pages needed writes to work end to end, so Add it / Add a charge (POST /api/entries), a needs-a-note save (PATCH /api/entries/:id/note) and a move (POST /api/envelope-moves) exist now, thin: the Worker re-checks with core on fresh rows and stores exactly core's plan; idempotent on the request id; the owner and creator are the session person.
  • Undo and the Reconcile buttons. Undo after Add it (POST /api/entries/:id/undo, either role) and the Admin's Reconcile buttons (POST /api/reconcile/actions) are writes too. Both only archive (an entry, the move a pull made) or move a working-state day (roll_state.origin) or end a recurring item; nothing is deleted. The rules are core's (canUndo, reconcileLists, planReconcileAction); the Worker persists in one db.batch whose audit lines are written under the same condition as the change. No schema change: "who added it" is the entries.created_by column the Add it routes already write. The Reconcile page's "Not at bank" and "Didn't post" lists are worked out from the entries and the roll state the last live run kept, since the bank's lines are not stored.
  • The Reconcile page and its data source calls. The data source gets two more: reconcileSnapshot() (the page's data as core's ReconcileSnapshot, admin only: a Member's read rejects with a 403) and reconcileAction({ itemId, action, requestId }) (core's ReconcileActionResult; a refusal is an answer with words). Core's reconcilePage(snapshot, now, filter) words the last run, the four lists, the history (with its run filter) and the pulls. The API source reads GET /api/reconcile and adds the people from the session (the route has no names), and the route now also names the household's timeZone, since run times show on its clock. The mock keeps each button's effect in memory and shows it on the next read; the same contract tests run against both.
  • Recently added and the data source's undo. The data source gets one more call, undo(entryId, requestId), that answers core's UndoResult (a refusal is an answer with words, not an error). Partner's snapshot gains an optional added list (AddedFact: entry id, who added it, when, when it last changed, confirmed by reconcile, a later audit line) for the entries people added through the app, because the ledger's entries carry none of that. Core's recentlyAdded(snapshot, now) turns it into the "Recently added" rows (last 14 days, newest first, at most 10) with canUndo and the plain-words reason for the viewer; an undone entry is archived, so it drops off by itself. addEntry may name the entry it wrote (entryId) so the "Added" message can offer Undo for it; a source that doesn't say still shows the entry, with its Undo, in the list. The API source sends the call; the Worker filling added (and entryId) from its rows is the one piece of wiring left for the local-server step, so an API-backed page shows an empty list until then.
  • Move money uses the existing moveBetweenEnvelopes. The data source already had the call (and the Worker route behind it), so the form adds none. The mock now remembers a press's requestId, like the other writes, so a retry of the same move writes once. Core's moveFormCheck is the early, field-by-field copy of the rules partnerMove runs when the move is sent; a test holds the two together.

Addendum: the page menu ​

Updated: the Switch page navigation described above is now one hamburger <details> menu ("Menu") at every width, not a row of plain links on a phone and a menu from 56rem. A desktopOnly page is still offered only from 56rem; below that the Menu leaves it out. Everything else in the Decision stands (the registry is the one source, the links are real <a href>s, Escape and leaving it close the menu).