Panels as data, layouts as documents
By the end of this lesson you will have a src/lib/layout.ts holding the layout type, a default layout and a parser that checks each panel's config by its type and migrates old versions on read, tested first, and a dashboard page that draws that layout as a grid of titled, empty panels.
Build a Screener turned filters into data so screens could be saved. Same move, one level up: turn panels into data so layouts can be.
The layout document
type Panel = {
id: string;
type: "watchlist" | "screen" | "backtest" | "quote";
title: string;
config: unknown; // validated per type
position: { x: number; y: number; w: number; h: number };
};
type Layout = { name: string; panels: Panel[]; version: number };
If a layout is a document, then everything you want follows without new mechanisms: save it, name it, restore it, share it, diff two of them, and revert a change you regret.
Hard-code the panels in JSX instead and every one of those needs its own feature.
type decides everything, so validate it
config is deliberately unknown in the type and must be validated per panel type on read, not trusted.
The document came from a database row that came from a browser, and Build a Screener already established the rule: input that reaches a privileged path is validated against an allowlist. A layout is exactly that — it decides which requests your server makes with your key. An unvalidated config on a quote panel is a request builder for a stranger.
Unknown type gets a placeholder panel saying so. Never crash the whole cockpit because one panel is from a newer version of your own code.
Version the document, migrate on read
You will change the panel shape. Write the migration when you change it, not later:
const MIGRATIONS = {
1: (d) => ({ ...d, panels: d.panels.map(p => ({ ...p, title: p.title ?? p.type })) }),
};
Run migrations on read, save at the current version. It costs ten minutes at the time and it is the difference between changing your own format freely and being frightened of your own saved data.
Sensible defaults, and one escape hatch
A new user gets a default layout that works: a watchlist, a screen and a backtest. Nobody should meet an empty grid and a "+" button.
And one button that resets to that default; the saved-layouts lesson in unit 2 adds it. When a layout gets into a state the person cannot fix by clicking, the alternative to a reset button is a support conversation with yourself.
Try it now
Write the layout type and a default layout before building any panel. Then hand your assistant the type and ask for the grid that renders it — a generated component against a type you designed is a very different experience from asking for "a dashboard".
The prompt
Mock mode on, plan mode on, and paste:
Plan, then build, src/lib/layout.ts from these types:
type Panel = {
id: string;
type: "watchlist" | "screen" | "backtest" | "quote";
title: string;
config: unknown; // validated per type, never trusted
position: { x: number; y: number; w: number; h: number };
};
type Layout = { name: string; panels: Panel[]; version: number };
Add DEFAULT_LAYOUT: a watchlist, a screen and a backtest. Add parseLayout(raw: unknown): Layout. It checks each panel's
config with a validator for its type, turns an unknown type into a
placeholder panel that says so, runs the migrations from the document's
version up to the current one, and returns the default when the whole
document is not a layout. Write tests/layout.test.ts first and show me
it failing: an unknown type, a quote panel whose config names something
that is not a symbol, and a version 0 document without titles. Then add
src/app/dashboard/page.tsx, which draws parseLayout(DEFAULT_LAYOUT) as a
CSS grid of titled, empty panels (a placeholder panel says its type
is unknown), and a link to the page in the site's navigation. No panel fetches anything
yet, so no new request. Run npm test, typecheck, lint and check:leak,
and show me the diff first.
A good answer adds src/lib/layout.ts, its test and the page, and changes the navigation component. The page imports no data module at all: who fetches for a panel is the next unit's contract.
Check it yourself
- The three new tests were red before
parseLayoutexisted and are green now. - The dashboard page, at
localhost:3000/dashboard, shows three framed panels with their titles and no numbers. - Ask the agent to change the second panel's type in
DEFAULT_LAYOUTto a made-up name such as "hologram", with a cast so TypeScript lets it through, and reload: that panel says it does not know the type, and the other two still draw. The page only shows this because it draws whatparseLayoutreturns. Put it back. grep -n "eodhdFetch\|market" src/app/dashboard/page.tsxprints nothing.
Compare with ours
// src/lib/layout.ts: config is unknown until its own type's check says otherwise
const CHECK: Record<PanelType, (config: unknown) => object | null> = {
watchlist: parseWatchlistConfig,
screen: parseScreenConfig, // the filter allowlist from Build a Screener
backtest: parseBacktestConfig,
quote: parseQuoteConfig, // a symbol, checked against the format
};
export function parseLayout(raw: unknown): Layout {
const doc = migrate(raw); // MIGRATIONS, from its version up to CURRENT
if (doc === null) return DEFAULT_LAYOUT;
return {
...doc,
panels: doc.panels.map((p) => {
const check = Object.hasOwn(CHECK, p.type) ? CHECK[p.type as PanelType] : null;
const config = check ? check(p.config) : null;
return config === null ? placeholder(p) : { ...p, config }; // never crash the page
}),
};
}
The full state at the end of this unit is on the starter's branch ref/build-a-dashboard/u1.

Checkpoint
npm test && npm run typecheck
git status # the files you expect, and no .env file
git add -A
git commit -m "Layouts as documents: typed panels, config checked per type, migrated on read"
Then answer this lesson's questions.