One table, two products
By the end of this lesson you will have one table component that renders both the watchlist and the screen from column definitions, a yield column that shows 0.0002 as 0.02% and a missing yield as a dash, and proof that a new screener column touches one file.
Your watchlist table renders ticker, last, change, volume. Your screener needs code, name, market cap, sector, yield — different columns, different rows, and exactly the same behaviour underneath.
This is the second refactor, and it is a harder judgement call than the first.
The trap: generalising too early and too far
The tempting move is a table that can do anything: pass it any data, any column definitions, sorting, grouping, pinning, virtualisation. Two hours later you have written a worse version of a library, and both products depend on it.
The discipline is to generalise exactly as far as the second caller requires, and no further. You have two concrete uses in front of you. Build for those two. The watchlist's sort by change is a feature of one caller, so it stays with the watchlist, which orders its rows before handing them over; the screen's order comes from the screener's own sort. The third caller, if it ever exists, can push it further with real requirements instead of imagined ones.
What the two actually share
Write it down before writing code:
- a header row, a body, and one row per record
- right-aligned figures with tabular numerals so columns line up
- a value that can be missing, rendered as a dash rather than blank
- an empty state that says why it is empty
- horizontal scrolling on a narrow screen instead of pushing the page sideways
Nothing there mentions prices. That list is your component's contract.
type Column<T> = {
key: string;
label: string;
align?: "left" | "right";
render: (row: T) => string | null; // null renders as a dash
tone?: (row: T) => "up" | "down" | null; // colour, beside the sign
input?: (row: T) => ReactNode; // a control, like the note
};
The last two are not imagined requirements. Build Your First Tool's watchlist already colours its change cell and puts a note input in a row, and neither fits in a string. tone gives the colour while the text keeps the sign; input is a cell holding a control the table only places.
A column knows how to turn a row into a cell. The table knows nothing else about either. The watchlist passes quote columns; the screener passes screen columns.
Why render returns a string
Because formatting is where the numbers get lied about, and putting it in one place per column means there is one place to fix.
The screener's yield column is the example that matters. The API sends 0.0002, which is a fraction. The column that renders it is the only code that should know that:
{ key: "dividend_yield", label: "Yield", align: "right",
render: (r) => r.dividend_yield == null
? null
: `${(r.dividend_yield * 100).toFixed(2)}%` }
If instead the table received pre-formatted rows, or worse formatted numbers itself, that * 100 would end up scattered or forgotten. Reviewing for numbers that lie is a whole lesson on the damage that does.
The generalisation that is worth it
Notice what you get for free once the table is generic: the empty state, the dash-for-missing, the alignment and the scroll behaviour are now written once and correct in both products. When you fix a rendering bug you fix it everywhere.
That is the actual return on a rebuild, and it is why "start a new project" was the expensive option.
Try it now
Generalise your table and render both products through it. Then apply the real test of the abstraction: add one column to the screener — sector, say — and count how many files you had to touch. If the answer is more than one, the contract is leaking and it is cheaper to fix now than after the third product.
The prompt
Mock mode on, plan mode on, and paste:
Make one table component, src/components/DataTable.tsx, generic over
the row type. It takes rows, a row key and a list of columns
{ key, label, align?, render(row) => string | null }, plus two
optional fields the watchlist needs: tone(row) => "up" | "down" | null
for the change colour, and input(row) for a cell that holds a control,
the note. It renders null as a dash, keeps tabular numerals and
right-aligned figures, shows an empty state with a reason, and
scrolls sideways on a narrow screen.
Render the watchlist and the screen through it, the screen's columns
in src/lib/screen-columns.ts. Build only what these two need: no
sorting, grouping or pinning inside the table. The yield column turns
the API's fraction into a percentage, and nothing else does. No new
request. Done means npm test, typecheck, lint and check:leak green;
show me the diff.
A good answer adds src/components/DataTable.tsx and src/lib/screen-columns.ts, shrinks the watchlist component to its column list, and changes the screen page to pass columns in. The watchlist keeps its rows, numbers and order, but not every pixel: a column renders a string, so a cell that mixed two colours or merged two cells cannot be reproduced exactly. Expect small differences: heading case, a muted "(+0.34%)" beside the change, or the explanation on a row for a ticker that is not available. Tell your agent which of those you accept. Build Your First Tool may also have hidden some columns on a phone; the new table scrolls sideways instead, so if your PLAN.md says otherwise, update that line in the same commit.
Check it yourself
- In mock mode the watchlist shows the same rows, numbers and order as the screenshot from the last lesson. Differences of look from the list above are expected; a different number or order is a bug.
- Give one fixture row a
dividend_yieldof0.0002: the screen shows 0.02%. - Remove that row's yield from the fixture: the cell shows a dash, not 0.00%.
- After the checkpoint commit, add a column for a field the screen rows already carry but the table does not show yet (industry, say):
git diff --statnames one file,src/lib/screen-columns.ts. A field the row type does not carry yet is two files, and that is fine. The count was the test: keep the column and commit it, or drop it withgit checkout -- src/lib/screen-columns.ts. - Narrow the browser window: the screen's table scrolls sideways and the page does not.
Compare with ours
// src/components/DataTable.tsx: rows and columns, nothing about prices
export function DataTable<T>({ rows, columns, rowKey, empty }: Props<T>) {
if (rows.length === 0) return <p>{empty}</p>; // says why it is empty
return (
<div className="overflow-x-auto">
<table className="w-full tabular-nums">
{/* thead from each column's label */}
<tbody>
{rows.map((row) => (
<tr key={rowKey(row)}>
{columns.map((c) => (
<td key={c.key} className={c.align === "right" ? "text-right" : ""}>
{c.input ? c.input(row) : (c.render(row) ?? "—")}
</td>
))}
</tr>
))}
</tbody>
</table>
</div>
);
}
The full state at the end of this unit is on the starter's branch ref/build-a-screener/u2.
Checkpoint
npm test && npm run typecheck
git status # only the files this step touched, no .env*
git add -A
git commit -m "One table for both products: columns render cells, the table renders columns"
Then answer this lesson's questions.