Contents Lesson 7 of 19

9 min read · practitioner

Named layouts, and the URL that shares them

By the end of this lesson you will have named layouts saved as one document in your server store, a switcher that changes layout without asking the network for it, an address that carries the layout name and the selection and nothing else, and an Export button that downloads a layout as JSON.

A layout is a document, so saving several is a list rather than a feature.

Name them after questions

"Morning check", "Earnings week", "Deep dive on one name". Not "Layout 1".

This is not a naming convention, it is a design constraint: if you cannot say what question a layout answers, it is a collection of panels rather than a view, and it will drift into holding everything. A cockpit that shows everything shows nothing, and the discipline that prevents it is naming.

Switching must be instant and lossless

Two requirements that are easy to miss:

Instant. Layouts are documents you already have; switching is local state, not a round trip. If switching a layout hits the network, you built it wrong.

Lossless. Unsaved changes to the current layout must not vanish silently. Either autosave on change, or say "unsaved changes" and mean it. Autosave with an explicit "revert to saved" is usually the better answer for a personal tool — it removes a decision from every interaction.

Switching layouts changes what data is needed, so the data layer gets a new union. Symbols in both layouts are already cached; only the new ones cost anything. That falls out of unit 1 without new code, which is the payoff for having built it.

The URL carries the view

/dashboard?layout=morning&symbol=AAPL.US

Layout name and selection, nothing else. The layout body lives on the server, so the link stays short and does not leak your configuration into someone's browser history.

Same rule as ever: both parameters are input from a stranger. Unknown layout name falls back to the default rather than erroring; the symbol goes through the allowlist. A link someone sends you must not be able to make your server fetch something you did not choose.

Export, so it is yours

One button that downloads the layout as JSON. It costs almost nothing and it changes what the tool is: your configuration is portable, diffable in git if you want, and recoverable when you break something.

It is also the honest answer to "what if I stop using this" — a question worth being able to answer about your own tools.

Try it now

Build three layouts and use them for a week. The one you never open is telling you something about the panels in it, and deleting it is a better outcome than keeping it.

The prompt

Mock mode on, plan mode on, and paste:

Plan named layouts. Keep them as one document, "layouts", in the
document store (src/lib/store.ts), and add "layouts" to the keys its
route accepts: a record
of Layout by name, each read through parseLayout so every panel is
checked again. A field and a button, "New layout from this one", ask
for a name and make a new layout from the current one. Each panel's
title can be renamed in place. The switcher changes the layout in the
container's state and rewrites the address to ?layout=name&symbol=...
with the history API, with no page load; an unknown name falls back to
the default. Changes autosave with the version you read, a "Saved" note
appears beside the switcher once an autosave has landed, and "Revert to
saved" restores the copy that was stored when you opened the layout. An
Export button downloads the current layout as a JSON file. Write
tests/layouts.test.ts first, for the unknown-name fallback, creating a
layout from the current one, renaming a panel, and an autosave that
meets a newer version. Before writing the switcher, tell
me with estimateCalls what switching between two layouts that share
every symbol costs. Mock mode. Run npm test, typecheck, lint and
check:leak, and show me the diff.

A good answer adds a layouts module, a switcher component and the test, changes the container, and adds layouts to the keys the store's route accepts. Switching asks neither the store nor the proxy for anything already on screen.

Check it yourself

  1. Use "New layout from this one" to make three layouts named after the questions they answer, then switch between two that share every symbol: the Network tab stays empty.
  2. Put a made-up name after ?layout= in the address and reload: the default layout opens and nothing errors.
  3. Rename a panel and wait for the saved note, then press Revert to saved: the name from when you opened the layout returns. Rename it again, wait for the note, and reload: the new name is there, and it is now the copy Revert goes back to, because a reload opens the layout afresh.
  4. Press Export and open the file: one layout as JSON, its name, its version and its panels with their types, titles, configs and positions, and nothing else.
Compare with ours
// src/lib/layouts.ts: a stranger's link opens the default, never an error
export function resolveLayout(saved: Record<string, Layout>, name: string | null): Layout {
  return name !== null && Object.hasOwn(saved, name) ? saved[name] : DEFAULT_LAYOUT;
}

// In the container: switching is state plus an address rewrite. No request, no page load.
function switchTo(name: string) {
  setLayout(resolveLayout(saved, name));
  const q = new URLSearchParams({ layout: name });
  if (selection.symbol) q.set("symbol", selection.symbol);
  window.history.replaceState(null, "", "?" + q.toString());
}

The full state at the end of this unit is on the starter's branch ref/build-a-dashboard/u2.

Checkpoint

npm test && npm run typecheck
git status          # the files you expect, and no .env file
git add -A
git commit -m "Named layouts: one document, switched locally, the address carries name and symbol"

Then answer this lesson's questions.