The panel contract
By the end of this lesson you will have the panel contract written once in src/lib/panels.ts, your watchlist rebuilt as a panel that only receives data, and a dashboard container that unions every panel's needs into one request — with a test that renders the panel from a literal fixture and no mocks.
Four panel types today, more later. What makes that survivable is a contract every panel honours, written once.
The interface
type PanelProps<C> = {
config: C; // already validated by type
data: PanelData; // given, never fetched
onSelect(symbol: string): void; // the only way out
onConfigChange(next: C): void; // the only way to persist
};
Four rules, and each one exists because of a specific failure:
Data comes in. A panel that fetches is a panel that cannot be tested without a network, cannot be rendered twice cheaply, and will duplicate a request. This is the data-layer rule from unit 1, expressed as a signature.
Config comes in validated. The panel never parses its own config. One validator per type, at the boundary, so a panel body contains no defensive parsing at all.
Communication goes out through callbacks. A panel never reaches into a global store to change something. It says what happened; the container decides what that means.
Panels do not know about each other. No panel imports another. The moment one does, you have a pair rather than a system, and the third panel will need a different pair.
Declare what data you need
A panel cannot fetch, so it must be able to say what it wants:
const WatchlistPanel = {
dataNeeds: (config) => ({ quotes: config.symbols }),
render: (props) => { /* ... */ },
};
The container collects dataNeeds from every visible panel, unions the symbol sets, and hands that one list to the data layer. Two panels wanting AAPL produce one entry. This is where the deduplication from unit 1 actually happens — the store can only dedupe what it is asked for in one go.
It also gives you the refresh budget for free, which is unit 4: the union of dataNeeds across the layout, priced by the table from unit 1, is exactly what one refresh costs.
The panel body is small, and that is the test
If a panel body is more than a screenful, something that belongs in the container has leaked into it. The usual culprits: fetching, parsing, cross-panel coordination, or persistence.
A good panel is a pure function of its props. You can render it in a test with a literal fixture and no mocks, and that is the property to check.
Where generated code goes wrong here
Ask an assistant for "a watchlist panel" and you will get one that fetches, parses, holds its own loading state and writes to localStorage — because that is what a standalone component looks like in every example it has read.
So do not ask for a panel. Give it the contract and ask for an implementation of it. The difference in what comes back is the largest single return on the planning discipline from Build Your First Tool.
Try it now
Write the contract, then convert your existing watchlist into a panel that satisfies it. Whatever you have to delete from it is the code that was never about watchlists — and that deletion is the whole lesson.
The prompt
Mock mode on, plan mode on, and paste:
Plan this before writing. In src/lib/panels.ts write this panel contract:
type PanelProps<C> = {
config: C; // already parsed for its type
data: PanelData; // given, never fetched
onSelect(symbol: string): void; // the only way out
onConfigChange(next: C): void; // the only way to persist
};
type PanelData = {
quotes: { row: QuoteRow; fetchedAt: number }[]; // its own symbols
screen: { rows: ScreenRow[]; fetchedAt: number } | null;
bars: { rows: DailyBar[]; fetchedAt: number } | null;
selected: string | null; // to mark a row; a click goes out by onSelect
};
type Needs = { quotes: string[]; screen?: ScreenConfig; eod?: string };
type PanelDef<C> = {
parse(raw: unknown): C | null;
dataNeeds(config: C): Needs;
Component(props: PanelProps<C>): React.ReactNode;
};
QuoteRow, ScreenRow and DailyBar stand for the row types this repo
already has for a quote, a screen result and a daily bar; if the
watchlist shows notes, add them to PanelData too. Add unionNeeds, which
turns the quotes lists of many panels into one list of distinct
symbols. Implement the contract for three panels, one file each in
src/components/panels/ and nothing else in that folder:
WatchlistPanel.tsx for the watchlist, reusing the shared table and the
watchlist's columns; ScreenPanel.tsx, which shows the screen page's
results table for a saved screen; and BacktestPanel.tsx, which shows
the backtest page's summary for one symbol's end-of-day history. A
panel must not fetch, read storage or import another panel;
code the browser needs from a server-only file moves into a plain module
that holds no key. Each row of the table can be clicked and reports its
symbol through onSelect. Turn the dashboard into a client container,
src/components/Cockpit.tsx: it collects dataNeeds from every visible
panel, unions the symbols, asks the market store once, through the
proxy, and passes the data down; DEFAULT_LAYOUT in src/lib/layout.ts
now draws all three panels. Write tests/panels.test.ts first: two panels
needing one symbol give one entry, and the watchlist panel renders a
literal row to a string with no mock. Before wiring the fetch, tell me
with estimateCalls what one dashboard load costs. Mock mode. Run npm
test, typecheck, lint and check:leak, and show me the diff.
A good answer adds src/lib/panels.ts, the three panels, src/components/Cockpit.tsx and the test, makes table rows clickable so a panel has something to call onSelect with, and the dashboard page now renders the container. The panels hold no useEffect and no fetch; if your watchlist shows notes, they arrive as data like everything else. The later lessons of this unit and the next ones use all three panels, which is why they are built here. check:leak builds into the same .next folder the dev server uses, so restart npm run dev after it.
Check it yourself
grep -n "fetch(\|useEffect\|localStorage\." src/components/panels/*.tsxprints nothing. A comment that merely says the panel does not fetch is fine; a call is not.- The new tests failed first and pass now, beside the starter's own.
- In mock mode the dashboard's watchlist panel shows the same rows as the home page, and the screen and backtest panels show what the
localhost:3000/screenandlocalhost:3000/backtestpages show. - Put a second watchlist panel in the default layout and reload with the browser's Network tab open: still a single quotes request through the proxy per load. Under
npm run dev, React runs each effect twice on the first load, so a request may show as cancelled and sent again; count the ones that completed. Take the second panel out again.
Compare with ours
// src/lib/panels.ts: the contract every panel signs
export type PanelProps<C> = {
config: C; // already parsed for its type
data: PanelData; // given, never fetched
onSelect(symbol: string): void; // the only way out
onConfigChange(next: C): void; // the only way to persist
};
// Everything a panel can be handed; each reads the part its needs asked for.
export type PanelData = {
quotes: Fetched[]; // its own symbols, each with fetchedAt
screen: { rows: ScreenRow[]; fetchedAt: number } | null;
bars: { rows: Bar[]; fetchedAt: number } | null;
notes: Notes; // the watchlist's notes, as data like the rest
selected: string | null; // to mark a row; a click is onSelect
};
// Quotes are a list the container can union; a screen and a daily history are whole requests.
export type Needs = { quotes: string[]; screen?: ScreenConfig; eod?: string };
export type PanelDef<C> = {
parse(raw: unknown): C | null;
dataNeeds(config: C): Needs;
Component(props: PanelProps<C>): React.ReactNode;
};
// The container's one list: two panels wanting the same symbol ask once.
export const unionNeeds = (needs: { quotes: string[] }[]) =>
[...new Set(needs.flatMap((n) => n.quotes))];
Ours builds the screen and backtest panels in the same commit, which is why Needs carries more than quotes, and the table gains a row click and a selected row so a panel built on it can report through onSelect. 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 "Panel contract: data in, callbacks out; the watchlist is the first panel"
Then answer this lesson's questions.