The best way to control Claude Code, Codex and more from any computer or phone

main
tsx 565 lines 20.1 KB
Raw
1 import { interpolate } from "remotion";
2 import type { PyramidStep } from "../../shared/intro.ts";
3 import { hand, mono, sans } from "../fonts.ts";
4 import { brand } from "../theme.ts";
5 import { drawProgress, TONE } from "./draw.tsx";
6
7 const clamp = { extrapolateLeft: "clamp", extrapolateRight: "clamp" } as const;
8
9 /** Four bands, bottom first: slot 0 is the widest. */
10 const CX = 690;
11 const TOP = 196;
12 const BOTTOM = 944;
13 const WIDE = 1080;
14 const NARROW = 460;
15 const GAP = 12;
16 const SLOTS = 4;
17 const BAND = (BOTTOM - TOP - GAP * (SLOTS - 1)) / SLOTS;
18 const widthAt = (y: number) => NARROW + ((WIDE - NARROW) * (y - TOP)) / (BOTTOM - TOP);
19 const band = (slot: number) => {
20 const bottom = BOTTOM - slot * (BAND + GAP);
21 return { top: bottom - BAND, bottom, mid: bottom - BAND / 2 };
22 };
23 /** Where the notes beside the pyramid start. */
24 const NOTE_X = CX + WIDE / 2 + 70;
25 const SLICE_W = 420;
26 /** The observability column: right of the pyramid's widest band. */
27 const PILLAR_X = CX + WIDE / 2 + 70;
28 const PILLAR_W = 300;
29 /** The code column beside the pyramid: clear of its widest band, same left edge for every row. */
30 const CODE_X = 1225;
31 const CODE_SIZE = 22;
32 const CODE_LH = 34;
33
34 const DIM = 0.3;
35
36 type Brick = NonNullable<PyramidStep["bricks"]>[number];
37
38 /**
39 * An application's layers, stacked as a pyramid. Layers rise in as they're
40 * introduced; notes beside them, the bracket over them, the line that splits
41 * them, a feature cut through them, and the modules that rebuild them come
42 * and go by step.
43 */
44 export const PyramidView = ({
45 step,
46 prev,
47 local,
48 }: {
49 step: PyramidStep;
50 prev?: PyramidStep;
51 local: number;
52 }) => {
53 const t = interpolate(local, [0, 10], [0, 1], clamp);
54 const lit = (s: PyramidStep | undefined, id: string) => !s?.lit || s.lit.includes(id);
55 const blend = (now: number, then: number) => then + (now - then) * t;
56 const had = (id: string) => !!prev?.layers.some((l) => l.id === id);
57 const fresh = step.layers.filter((l) => !had(l.id));
58 const noteKey = (n: { layer: string; text: string }) => `${n.layer}:${n.text}`;
59 const oldNotes = new Set(prev?.side?.map(noteKey));
60 const newNotes = (step.side ?? []).filter((n) => !oldNotes.has(noteKey(n)));
61 const braceNew =
62 !!step.brace && (prev?.brace?.text !== step.brace.text || prev?.brace?.sub !== step.brace.sub);
63 const braceWas = !!prev?.brace;
64
65 // Bands fade back when something is drawn over them.
66 const covered = (s: PyramidStep | undefined) => (s?.bricks || s?.slice || s?.slices ? 0 : 1);
67 const bandText = blend(covered(step), prev ? covered(prev) : covered(step));
68 const bandFill = blend(
69 step.bricks ? 0.25 : 1,
70 prev ? (prev.bricks ? 0.25 : 1) : step.bricks ? 0.25 : 1,
71 );
72
73 const sliceIn = step.slice ? (prev?.slice ? 1 : interpolate(local, [2, 12], [0, 1], clamp)) : 0;
74 const cutIn = step.cut
75 ? prev?.cut?.under === step.cut.under
76 ? 1
77 : drawProgress(local, 6, 12)
78 : 0;
79
80 const brickKey = (b: Brick) => `${b.row}:${b.title}`;
81 const oldBricks = new Set(prev?.bricks?.map(brickKey));
82 const newBricks = (step.bricks ?? []).filter((b) => !oldBricks.has(brickKey(b)));
83
84 return (
85 <svg width={1920} height={1080} style={{ position: "absolute", left: 0, top: 0 }}>
86 {step.layers.map((layer, slot) => {
87 const { top, bottom, mid } = band(slot);
88 const k = fresh.indexOf(layer);
89 const q = k < 0 ? 1 : interpolate(local, [k * 4, k * 4 + 8], [0, 1], clamp);
90 // With modules drawn over them, the bands are only a backdrop: never dimmed.
91 const bandLit = (s: PyramidStep | undefined) => !!s?.bricks || lit(s, layer.id);
92 const now = bandLit(step) ? 1 : DIM;
93 const then = had(layer.id) ? (bandLit(prev) ? 1 : DIM) : now;
94 const level = blend(now, then);
95 const wt = widthAt(top);
96 const wb = widthAt(bottom);
97 const oldDetail = prev?.layers.find((l) => l.id === layer.id)?.detail;
98 const detailIn =
99 oldDetail === undefined || oldDetail === layer.detail
100 ? 1
101 : interpolate(local, [2, 10], [0, 1], clamp);
102 return (
103 <g key={layer.id} opacity={q * level} transform={`translate(0 ${(1 - q) * 30})`}>
104 <path
105 d={`M ${CX - wb / 2} ${bottom} L ${CX - wt / 2} ${top} L ${CX + wt / 2} ${top} L ${CX + wb / 2} ${bottom} Z`}
106 fill={`${layer.color}1f`}
107 fillOpacity={bandFill}
108 stroke={layer.color}
109 strokeOpacity={0.4 + 0.6 * bandFill}
110 strokeWidth={3}
111 strokeLinejoin="round"
112 />
113 <g opacity={bandText}>
114 <text
115 x={CX}
116 y={mid - 6}
117 textAnchor="middle"
118 fontFamily={sans}
119 fontWeight={700}
120 fontSize={40}
121 fill={brand.fg}
122 >
123 {layer.title}
124 </text>
125 <text
126 x={CX}
127 y={mid + 38}
128 textAnchor="middle"
129 fontFamily={mono}
130 fontSize={24}
131 fill={brand.fgMuted}
132 opacity={detailIn}
133 >
134 {layer.detail}
135 </text>
136 </g>
137 </g>
138 );
139 })}
140
141 {step.cut
142 ? (() => {
143 const slot = step.layers.findIndex((l) => l.id === step.cut!.under);
144 const y = band(slot).bottom + GAP / 2;
145 const x1 = CX - widthAt(y) / 2 - 40;
146 const x2 = NOTE_X - 30;
147 return (
148 <g>
149 <path
150 d={`M ${x1} ${y} L ${x1 + (x2 - x1) * cutIn} ${y}`}
151 stroke={TONE.bad}
152 strokeWidth={5}
153 strokeDasharray="16 10"
154 strokeLinecap="round"
155 />
156 <g opacity={cutIn} fontFamily={hand} fontWeight={700} fontSize={46} fill={brand.fg}>
157 <text x={NOTE_X} y={y - 26}>
158 ↑ {step.cut.above}
159 </text>
160 <text x={NOTE_X} y={y + 58}>
161 ↓ {step.cut.below}
162 </text>
163 </g>
164 </g>
165 );
166 })()
167 : null}
168
169 {/* A feature cut vertically through the layers: one solid section per band. */}
170 {step.slice
171 ? (() => {
172 const top = band(step.layers.length - 1).top;
173 const x = CX - SLICE_W / 2;
174 return (
175 <g opacity={sliceIn}>
176 {step.layers.map((layer, slot) => {
177 const b = band(slot);
178 const item = step.slice!.items[layer.id];
179 const level = blend(
180 lit(step, layer.id) ? 1 : DIM,
181 prev?.slice ? (lit(prev, layer.id) ? 1 : DIM) : 1,
182 );
183 return (
184 <g key={layer.id}>
185 <rect x={x} y={b.top} width={SLICE_W} height={BAND} fill={brand.bg} />
186 <rect
187 x={x}
188 y={b.top}
189 width={SLICE_W}
190 height={BAND}
191 fill={layer.color}
192 fillOpacity={0.16 * level}
193 />
194 {item ? (
195 <g opacity={level}>
196 <text
197 x={CX}
198 y={b.mid - 10}
199 textAnchor="middle"
200 fontFamily={mono}
201 fontSize={20}
202 fill={layer.color}
203 >
204 {layer.title.toLowerCase()}
205 </text>
206 <text
207 x={CX}
208 y={b.mid + 30}
209 textAnchor="middle"
210 fontFamily={sans}
211 fontWeight={700}
212 fontSize={32}
213 fill={brand.fg}
214 >
215 {item}
216 </text>
217 </g>
218 ) : null}
219 </g>
220 );
221 })}
222 <rect
223 x={x}
224 y={top}
225 width={SLICE_W}
226 height={BOTTOM - top}
227 rx={14}
228 fill="none"
229 stroke={TONE.good}
230 strokeWidth={4}
231 />
232 <text
233 x={CX}
234 y={BOTTOM + 58}
235 textAnchor="middle"
236 fontFamily={hand}
237 fontWeight={700}
238 fontSize={48}
239 fill={TONE.good}
240 >
241 {step.slice.label}
242 </text>
243 </g>
244 );
245 })()
246 : null}
247
248 {/* Several slices side by side, widening with the pyramid: every module spans the same layers. */}
249 {step.slices
250 ? step.slices.map((sl, k) => {
251 const n = step.slices!.length;
252 const gap = 14;
253 /** The slice's left and right edge at height y. */
254 const edges = (y: number) => {
255 const w = widthAt(y);
256 const left = CX - w / 2;
257 return [left + (k * w) / n + gap / 2, left + ((k + 1) * w) / n - gap / 2] as const;
258 };
259 const topY = band(step.layers.length - 1).top;
260 const [tl, tr] = edges(topY);
261 const [bl, br] = edges(BOTTOM);
262 const q = prev?.slices?.some((p) => p.label === sl.label)
263 ? 1
264 : interpolate(local, [2 + k * 4, 10 + k * 4], [0, 1], clamp);
265 return (
266 <g key={sl.label} opacity={q} transform={`translate(0 ${(1 - q) * 18})`}>
267 {step.layers.map((layer, slot) => {
268 const b = band(slot);
269 const [a1, a2] = edges(b.top);
270 const [c1, c2] = edges(b.bottom);
271 const item = sl.items[layer.id];
272 const [m1, m2] = edges(b.mid);
273 return (
274 <g key={layer.id}>
275 <path
276 d={`M ${a1} ${b.top} L ${a2} ${b.top} L ${c2} ${b.bottom} L ${c1} ${b.bottom} Z`}
277 fill={brand.bg}
278 />
279 <path
280 d={`M ${a1} ${b.top} L ${a2} ${b.top} L ${c2} ${b.bottom} L ${c1} ${b.bottom} Z`}
281 fill={layer.color}
282 fillOpacity={item ? 0.2 : 0.05}
283 />
284 {item ? (
285 <text
286 x={(m1 + m2) / 2}
287 y={b.mid + 9}
288 textAnchor="middle"
289 fontFamily={mono}
290 fontSize={22}
291 fill={brand.fg}
292 >
293 {item}
294 </text>
295 ) : null}
296 </g>
297 );
298 })}
299 <path
300 d={`M ${tl} ${topY} L ${tr} ${topY} L ${br} ${BOTTOM} L ${bl} ${BOTTOM} Z`}
301 fill="none"
302 stroke={TONE.good}
303 strokeWidth={3}
304 strokeLinejoin="round"
305 />
306 <text
307 x={(bl + br) / 2}
308 y={BOTTOM + 50}
309 textAnchor="middle"
310 fontFamily={hand}
311 fontWeight={700}
312 fontSize={40}
313 fill={TONE.good}
314 >
315 {sl.label}
316 </text>
317 </g>
318 );
319 })
320 : null}
321
322 {(step.bricks ?? []).map((brick) => {
323 const { top, bottom } = band(brick.row);
324 const inRow = step.bricks!.filter((b) => b.row === brick.row);
325 const i = brick.col ?? inRow.indexOf(brick);
326 const n = brick.of ?? inRow.length;
327 const rowW = widthAt(top) - 60;
328 const gap = 18;
329 const w = (rowW - gap * (n - 1)) / n;
330 const x = CX - rowW / 2 + i * (w + gap);
331 const y = top + 10;
332 const h = bottom - top - 20;
333 const k = newBricks.indexOf(brick);
334 const q = k < 0 ? 1 : interpolate(local, [4 + k * 4, 12 + k * 4], [0, 1], clamp);
335 const level = blend(
336 lit(step, brick.title) ? 1 : DIM,
337 prev?.bricks?.some((b) => brickKey(b) === brickKey(brick))
338 ? lit(prev, brick.title)
339 ? 1
340 : DIM
341 : 1,
342 );
343 return (
344 <g key={brickKey(brick)} opacity={q * level} transform={`translate(0 ${(1 - q) * -40})`}>
345 <rect
346 x={x}
347 y={y}
348 width={w}
349 height={h}
350 rx={16}
351 fill={brand.bgElevated}
352 stroke={brick.color}
353 strokeWidth={3.5}
354 />
355 <text
356 x={x + w / 2}
357 y={y + h / 2 - 4}
358 textAnchor="middle"
359 fontFamily={sans}
360 fontWeight={700}
361 fontSize={36}
362 fill={brand.fg}
363 >
364 {brick.title}
365 </text>
366 <text
367 x={x + w / 2}
368 y={y + h / 2 + 34}
369 textAnchor="middle"
370 fontFamily={mono}
371 fontSize={20}
372 fill={brand.fgMuted}
373 >
374 {brick.detail}
375 </text>
376 </g>
377 );
378 })}
379
380 {/* A column beside the pyramid: it spans every layer, and each layer feeds it. */}
381 {step.pillar
382 ? (() => {
383 const pillar = step.pillar;
384 const top = band(step.layers.length - 1).top;
385 const x = PILLAR_X;
386 const was = prev?.pillar;
387 const q = was ? 1 : interpolate(local, [2, 12], [0, 1], clamp);
388 const linesIn =
389 was && was.lines.join() === pillar.lines.join()
390 ? 1
391 : interpolate(local, [6, 14], [0, 1], clamp);
392 const mid = (top + BOTTOM) / 2;
393 return (
394 <g opacity={q} transform={`translate(${(1 - q) * 24} 0)`}>
395 {step.layers.map((layer, slot) => {
396 const b = band(slot);
397 const from = CX + widthAt(b.mid) / 2 + 10;
398 return (
399 <path
400 key={layer.id}
401 d={`M ${from} ${b.mid} L ${x - 12} ${b.mid}`}
402 stroke={pillar.color}
403 strokeWidth={3}
404 strokeDasharray="8 8"
405 opacity={0.7}
406 />
407 );
408 })}
409 <rect
410 x={x}
411 y={top}
412 width={PILLAR_W}
413 height={BOTTOM - top}
414 rx={18}
415 fill={brand.bgElevated}
416 stroke={pillar.color}
417 strokeWidth={3.5}
418 />
419 <text
420 x={x + PILLAR_W / 2}
421 y={top + 64}
422 textAnchor="middle"
423 fontFamily={sans}
424 fontWeight={700}
425 fontSize={34}
426 fill={brand.fg}
427 >
428 {pillar.title}
429 </text>
430 <g opacity={linesIn}>
431 {pillar.lines.map((line, i) => (
432 <text
433 key={line}
434 x={x + PILLAR_W / 2}
435 y={mid - ((pillar.lines.length - 1) * 44) / 2 + i * 44 + 10}
436 textAnchor="middle"
437 fontFamily={mono}
438 fontSize={24}
439 fill={brand.fgMuted}
440 >
441 {line}
442 </text>
443 ))}
444 </g>
445 </g>
446 );
447 })()
448 : null}
449
450 {(step.side ?? []).map((note) => {
451 const slot = step.layers.findIndex((l) => l.id === note.layer);
452 if (slot < 0) return null;
453 const { mid } = band(slot);
454 const k = newNotes.indexOf(note);
455 const q = k < 0 ? 1 : interpolate(local, [4 + k * 4, 12 + k * 4], [0, 1], clamp);
456 const color = TONE[note.tone ?? "neutral"];
457 if (note.code) {
458 // One left-aligned column beside the pyramid; each line keeps its place, and only
459 // lines that weren't there before fade in.
460 const lines =
461 note.tokens ?? note.text.split("\n").map((text) => [{ text, color: brand.fg }]);
462 const before = new Set(
463 (prev?.side ?? [])
464 .filter((n) => n.code && n.layer === note.layer)
465 .flatMap((n) => n.text.split("\n")),
466 );
467 const y0 = mid + 8 - ((lines.length - 1) * CODE_LH) / 2;
468 return (
469 <g key={note.layer}>
470 {lines.map((line, j) => {
471 const raw = line.map((t) => t.text).join("");
472 if (!raw.trim()) return null;
473 const lq = before.has(raw) ? 1 : interpolate(local, [4, 12], [0, 1], clamp);
474 return (
475 <text
476 key={raw}
477 x={CODE_X}
478 y={y0 + j * CODE_LH}
479 fontFamily={mono}
480 fontSize={CODE_SIZE}
481 opacity={lq}
482 transform={`translate(${(1 - lq) * 14} 0)`}
483 style={{ whiteSpace: "pre" }}
484 >
485 {line.map((t, k) => (
486 <tspan key={k} fill={t.color}>
487 {t.text}
488 </tspan>
489 ))}
490 </text>
491 );
492 })}
493 </g>
494 );
495 }
496 return (
497 <g key={noteKey(note)} opacity={q} transform={`translate(${(1 - q) * 14} 0)`}>
498 <text
499 x={NOTE_X}
500 y={mid + 14}
501 fontFamily={hand}
502 fontWeight={700}
503 fontSize={44}
504 fill={color}
505 >
506 {note.text}
507 </text>
508 </g>
509 );
510 })}
511
512 {step.brace
513 ? (() => {
514 const shown = step.layers.length;
515 const top = band(shown - 1).top;
516 const bottom = BOTTOM;
517 const x = NOTE_X - 20;
518 const midY = (top + bottom) / 2;
519 const p = braceNew && !braceWas ? drawProgress(local, 4, 12) : 1;
520 const d = `M ${x} ${top} Q ${x + 28} ${top} ${x + 28} ${top + 40} L ${x + 28} ${midY - 30} Q ${x + 28} ${midY} ${x + 56} ${midY} Q ${x + 28} ${midY} ${x + 28} ${midY + 30} L ${x + 28} ${bottom - 40} Q ${x + 28} ${bottom} ${x} ${bottom}`;
521 const label = braceNew ? interpolate(local, [8, 16], [0, 1], clamp) : 1;
522 return (
523 <g>
524 <path
525 d={d}
526 fill="none"
527 stroke={TONE.good}
528 strokeWidth={4}
529 strokeLinecap="round"
530 strokeDasharray={3000}
531 strokeDashoffset={3000 * (1 - p)}
532 />
533 <g opacity={label}>
534 <text
535 x={x + 84}
536 y={midY - 4}
537 fontFamily={hand}
538 fontWeight={700}
539 fontSize={54}
540 fill={TONE.good}
541 >
542 {step.brace.text}
543 </text>
544 {step.brace.sub
545 ? step.brace.sub.split("\n").map((line, i) => (
546 <text
547 key={line}
548 x={x + 86}
549 y={midY + 44 + i * 40}
550 fontFamily={mono}
551 fontSize={26}
552 fill={brand.fgMuted}
553 >
554 {line}
555 </text>
556 ))
557 : null}
558 </g>
559 </g>
560 );
561 })()
562 : null}
563 </svg>
564 );
565 };