@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
tsx 398 lines 13 KB
Raw
1 import { useEffect, useRef, useState, type CSSProperties, type ReactNode } from "react";
2 import { Line, sleep, TermChrome, useSpinner } from "./_terminal";
3
4 /* ────────────────────────────────────────────────────────────
5 * Choreography
6 *
7 * A single state machine drives both the terminal log on the
8 * right and the rendered "page" inside the pseudo-browser on the
9 * left, so the two stay in lock-step (the user sees the cause
10 * and the effect).
11 * ──────────────────────────────────────────────────────────── */
12
13 type Phase =
14 /** initial bring-up of the stack */
15 | "boot-photos"
16 | "boot-sessions"
17 | "boot-api"
18 | "ready-v1"
19 /** edit src/Api.ts → worker reloads, page rerenders with new copy */
20 | "edit-api"
21 | "reload-api"
22 | "request-hello"
23 | "ready-v2"
24 /** edit alchemy.run.ts → new Queue resource wired to Api */
25 | "edit-stack"
26 | "diff-queue"
27 | "wire-queue"
28 | "ready-v3";
29
30 const PHASES: { phase: Phase; ms: number }[] = [
31 { phase: "boot-photos", ms: 600 },
32 { phase: "boot-sessions", ms: 500 },
33 { phase: "boot-api", ms: 700 },
34 { phase: "ready-v1", ms: 1300 },
35 { phase: "edit-api", ms: 1000 },
36 { phase: "reload-api", ms: 600 },
37 { phase: "request-hello", ms: 900 },
38 { phase: "ready-v2", ms: 1600 },
39 { phase: "edit-stack", ms: 1100 },
40 { phase: "diff-queue", ms: 700 },
41 { phase: "wire-queue", ms: 700 },
42 { phase: "ready-v3", ms: 2000 },
43 ];
44
45 /* ────────────────────────────────────────────────────────────
46 * Terminal log driven by phase
47 * ──────────────────────────────────────────────────────────── */
48
49 interface LogLine {
50 text: ReactNode;
51 key: string;
52 }
53
54 let _id = 0;
55 const mkLine = (text: ReactNode): LogLine => ({ text, key: `${++_id}` });
56
57 function bootLine(label: string, detail: string) {
58 return mkLine(
59 <>
60 <span style={{ color: "var(--alc-success)" }}>✓ </span>
61 <span style={{ color: "var(--alc-fg-invert)", fontWeight: 600 }}>{label}</span>
62 <span style={{ color: "var(--alc-code-comment)" }}>{` (${detail})`}</span>
63 <span style={{ color: "var(--alc-success)" }}> created</span>
64 </>,
65 );
66 }
67
68 const READY_LINES: LogLine[] = [
69 mkLine(
70 <>
71 <span style={{ color: "var(--alc-code-comment)" }}> → </span>
72 <span style={{ color: "var(--alc-accent-bright)" }}>http://localhost:1337</span>
73 </>,
74 ),
75 mkLine(<span style={{ color: "var(--alc-code-comment)" }}>Watching for changes…</span>),
76 ];
77
78 function editLine(file: string) {
79 return mkLine(
80 <>
81 <span style={{ color: "var(--alc-warn)" }}>↻ </span>
82 <span style={{ color: "var(--alc-code-comment)" }}>{file} changed</span>
83 </>,
84 );
85 }
86
87 const RELOAD_LINE = mkLine(
88 <>
89 <span style={{ color: "var(--alc-success)" }}>✓ </span>
90 <span style={{ color: "var(--alc-fg-invert)", fontWeight: 600 }}>Api</span>
91 <span style={{ color: "var(--alc-code-comment)" }}>{` reloaded in `}</span>
92 <span style={{ color: "var(--alc-fg-invert)", fontWeight: 600 }}>38ms</span>
93 </>,
94 );
95
96 function requestLine() {
97 return mkLine(
98 <>
99 <span
100 style={{ color: "var(--alc-code-comment)" }}
101 >{`[${new Date().toLocaleTimeString().slice(0, 8)}] `}</span>
102 <span style={{ color: "var(--alc-fg-invert)" }}>GET /object/hello.txt</span>
103 <span style={{ color: "var(--alc-success)" }}> 200</span>
104 </>,
105 );
106 }
107
108 const DIFF_LINE = mkLine(
109 <>
110 <span style={{ color: "var(--alc-success)" }}>+ </span>
111 <span style={{ color: "var(--alc-fg-invert)", fontWeight: 600 }}>Queue</span>
112 <span style={{ color: "var(--alc-code-comment)" }}>{` (Cloudflare.Queues.Queue)`}</span>
113 <span style={{ color: "var(--alc-success)" }}> created</span>
114 </>,
115 );
116
117 const WIRE_LINE = mkLine(
118 <>
119 <span style={{ color: "var(--alc-warn)" }}>~ </span>
120 <span style={{ color: "var(--alc-code-comment)" }}>wired </span>
121 <span style={{ color: "var(--alc-code-type)" }}>Api → Queue.bind</span>
122 </>,
123 );
124
125 const PROMPT_LINE = mkLine(
126 <>
127 <span style={{ color: "var(--alc-code-comment)" }}>$ </span>
128 <span>alchemy dev</span>
129 </>,
130 );
131
132 /* ────────────────────────────────────────────────────────────
133 * Browser preview (the "rendered site")
134 * ──────────────────────────────────────────────────────────── */
135
136 type AppVersion = "v1" | "v2" | "v3";
137
138 function appVersionFor(phase: Phase): AppVersion {
139 switch (phase) {
140 case "ready-v1":
141 case "edit-api":
142 return "v1";
143 case "reload-api":
144 case "request-hello":
145 case "ready-v2":
146 case "edit-stack":
147 case "diff-queue":
148 return "v2";
149 case "wire-queue":
150 case "ready-v3":
151 return "v3";
152 default:
153 return "v1";
154 }
155 }
156
157 function isReloading(phase: Phase) {
158 // Worker is rebuilding (or app hasn't booted yet).
159 return (
160 phase === "boot-photos" ||
161 phase === "boot-sessions" ||
162 phase === "boot-api" ||
163 phase === "reload-api" ||
164 phase === "wire-queue"
165 );
166 }
167
168 function isBooting(phase: Phase) {
169 return phase === "boot-photos" || phase === "boot-sessions" || phase === "boot-api";
170 }
171
172 function PhotoTile({ label }: { label: string }) {
173 return (
174 <div className="bhr-tile">
175 <div className="bhr-tile__img" aria-hidden />
176 <div className="bhr-tile__label">{label}</div>
177 </div>
178 );
179 }
180
181 function PreviewApp({ version }: { version: AppVersion }) {
182 const heading = version === "v1" ? "Photos" : "Hello, photos";
183 return (
184 <div className="bhr-app" key={version}>
185 <header className="bhr-app__header">
186 <div>
187 <div className="bhr-app__eyebrow">my-app · cloudflare workers</div>
188 <h3 className="bhr-app__title">{heading}</h3>
189 </div>
190 {version === "v3" && (
191 <button type="button" className="bhr-app__btn">
192 + Send to Queue
193 </button>
194 )}
195 </header>
196 <div className="bhr-app__grid">
197 <PhotoTile label="hello.txt" />
198 <PhotoTile label="sunset.jpg" />
199 <PhotoTile label="forest.png" />
200 <PhotoTile label="diner.heic" />
201 </div>
202 {version === "v3" && (
203 <div className="bhr-app__footer">
204 <span className="bhr-app__chip">
205 queue: <strong>uploads</strong>
206 </span>
207 <span className="bhr-app__chip">
208 bucket: <strong>Photos</strong>
209 </span>
210 <span className="bhr-app__chip">
211 kv: <strong>Sessions</strong>
212 </span>
213 </div>
214 )}
215 </div>
216 );
217 }
218
219 /* ────────────────────────────────────────────────────────────
220 * Browser chrome
221 * ──────────────────────────────────────────────────────────── */
222
223 function BrowserChrome({
224 reloading,
225 children,
226 style,
227 }: {
228 reloading: boolean;
229 children: ReactNode;
230 style?: CSSProperties;
231 }) {
232 return (
233 // Simulated browser chrome — decoration, kept out of search snippets.
234 <div className="bhr-browser" style={style} data-nosnippet="">
235 <div className="bhr-browser__header">
236 <span className="alc-code-block__dot" style={{ background: "var(--alc-danger)" }} />
237 <span className="alc-code-block__dot" style={{ background: "var(--alc-warn)" }} />
238 <span className="alc-code-block__dot" style={{ background: "var(--alc-accent-bright)" }} />
239 <div className="bhr-browser__nav" aria-hidden>
240 <span className="bhr-browser__navbtn">‹</span>
241 <span className="bhr-browser__navbtn">›</span>
242 <span
243 className={
244 "bhr-browser__navbtn bhr-browser__reload" + (reloading ? " is-spinning" : "")
245 }
246 >
247 ↻
248 </span>
249 </div>
250 <div className="bhr-browser__url">
251 <span className="bhr-browser__lock" aria-hidden>
252 ●
253 </span>
254 <span className="bhr-browser__urlhost">localhost</span>
255 <span className="bhr-browser__urlmuted">:1337/</span>
256 <span style={{ flex: 1 }} />
257 <span className="bhr-browser__hmr">
258 <span className="bhr-browser__hmrdot" /> HMR
259 </span>
260 </div>
261 </div>
262 <div className="bhr-browser__body">
263 {reloading && <div className="bhr-browser__progress" aria-hidden />}
264 {children}
265 </div>
266 </div>
267 );
268 }
269
270 /* ────────────────────────────────────────────────────────────
271 * Main component
272 * ──────────────────────────────────────────────────────────── */
273
274 export default function BrowserHotReload() {
275 const [phase, setPhase] = useState<Phase>("boot-photos");
276 const [lines, setLines] = useState<LogLine[]>([PROMPT_LINE]);
277 const [busy, setBusy] = useState(false);
278 const cancelRef = useRef(false);
279
280 useEffect(() => {
281 cancelRef.current = false;
282 const aborted = () => cancelRef.current;
283
284 const push = (...newLines: LogLine[]) => setLines((ls) => [...ls, ...newLines].slice(-14));
285
286 const reset = () => {
287 setLines([PROMPT_LINE]);
288 };
289
290 const run = async () => {
291 while (!aborted()) {
292 reset();
293 setPhase("boot-photos");
294 await sleep(300);
295
296 for (const { phase: next, ms } of PHASES) {
297 if (aborted()) return;
298 setPhase(next);
299
300 // Show the spinner during clearly "busy" steps so the
301 // browser progress bar and the terminal feel coupled.
302 const showSpinner = isBooting(next) || next === "reload-api" || next === "wire-queue";
303 setBusy(showSpinner);
304 await sleep(ms);
305 setBusy(false);
306
307 switch (next) {
308 case "boot-photos":
309 push(bootLine("Photos", "Cloudflare.R2.Bucket"));
310 break;
311 case "boot-sessions":
312 push(bootLine("Sessions", "Cloudflare.KV.Namespace"));
313 break;
314 case "boot-api":
315 push(bootLine("Api", "Cloudflare.Worker · local → workerd"));
316 break;
317 case "ready-v1":
318 case "ready-v2":
319 case "ready-v3":
320 push(...READY_LINES.map((l) => mkLine(l.text)));
321 break;
322 case "edit-api":
323 push(editLine("src/Api.ts"));
324 break;
325 case "reload-api":
326 push(RELOAD_LINE);
327 break;
328 case "request-hello":
329 push(requestLine());
330 break;
331 case "edit-stack":
332 push(editLine("alchemy.run.ts"));
333 break;
334 case "diff-queue":
335 push(DIFF_LINE);
336 break;
337 case "wire-queue":
338 push(WIRE_LINE);
339 break;
340 }
341 }
342
343 await sleep(1800);
344 }
345 };
346
347 void run();
348 return () => {
349 cancelRef.current = true;
350 };
351 }, []);
352
353 const spinner = useSpinner(busy);
354 const reloading = isReloading(phase);
355 const version = appVersionFor(phase);
356 const booting = isBooting(phase);
357
358 return (
359 <div className="bhr-stage">
360 {/* Browser preview — hidden on small screens */}
361 <BrowserChrome reloading={reloading} style={{ minWidth: 0 }}>
362 {booting ? (
363 <div className="bhr-app bhr-app--boot">
364 <div className="bhr-app__skeleton" />
365 <div className="bhr-app__skeleton bhr-app__skeleton--sm" />
366 <div className="bhr-app__grid">
367 <div className="bhr-tile bhr-tile--ghost" />
368 <div className="bhr-tile bhr-tile--ghost" />
369 <div className="bhr-tile bhr-tile--ghost" />
370 <div className="bhr-tile bhr-tile--ghost" />
371 </div>
372 </div>
373 ) : (
374 <PreviewApp version={version} />
375 )}
376 </BrowserChrome>
377
378 {/* Terminal — always visible */}
379 <div className="bhr-term">
380 <TermChrome
381 title="~/my-app"
382 badge="DEV"
383 badgeColor="var(--alc-accent-bright)"
384 maxLines={15}
385 >
386 {lines.map((l) => (
387 <Line key={l.key}>{l.text}</Line>
388 ))}
389 {busy && (
390 <Line>
391 <span style={{ color: "var(--alc-code-comment)" }}>{spinner} </span>
392 </Line>
393 )}
394 </TermChrome>
395 </div>
396 </div>
397 );
398 }