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

main
tsx 173 lines 5.64 KB
Raw
1 import { useEffect, useRef, useState } from "react";
2 import { Line, sleep, TermChrome, useSpinner } from "./_terminal";
3
4 interface Step {
5 kind: "boot" | "ready" | "edit" | "reload" | "diff" | "wire" | "request";
6 label?: string;
7 detail?: string;
8 ms: number;
9 }
10
11 const STEPS: Step[] = [
12 { kind: "boot", label: "Photos", detail: "Cloudflare.R2.Bucket", ms: 600 },
13 {
14 kind: "boot",
15 label: "Sessions",
16 detail: "Cloudflare.KV.Namespace",
17 ms: 500,
18 },
19 {
20 kind: "boot",
21 label: "Api",
22 detail: "Cloudflare.Worker · local → workerd",
23 ms: 700,
24 },
25 { kind: "ready", ms: 700 },
26 { kind: "edit", label: "src/Api.ts", ms: 900 },
27 { kind: "reload", detail: "38ms", ms: 600 },
28 { kind: "request", label: "GET /object/hello.txt", detail: "200", ms: 900 },
29 { kind: "edit", label: "alchemy.run.ts", ms: 1000 },
30 { kind: "diff", label: "Queue", detail: "Cloudflare.Queues.Queue", ms: 700 },
31 { kind: "wire", label: "Api → Queue.bind", ms: 700 },
32 { kind: "ready", ms: 1100 },
33 ];
34
35 interface LogLine {
36 text: React.ReactNode;
37 key: string;
38 }
39
40 let _id = 0;
41
42 export default function DevTerminal({ title = "~/my-app" }: { title?: string }) {
43 const [lines, setLines] = useState<LogLine[]>([]);
44 const [busy, setBusy] = useState(false);
45 const cancelRef = useRef(false);
46
47 useEffect(() => {
48 cancelRef.current = false;
49 const aborted = () => cancelRef.current;
50
51 const push = (text: React.ReactNode) =>
52 setLines((ls) => [...ls, { text, key: `${++_id}` }].slice(-14));
53
54 const reset = () => setLines([]);
55
56 const run = async () => {
57 while (!aborted()) {
58 reset();
59 push(
60 <>
61 <span style={{ color: "var(--alc-code-comment)" }}>$ </span>
62 <span>alchemy dev</span>
63 </>,
64 );
65 await sleep(300);
66 if (aborted()) return;
67
68 for (const step of STEPS) {
69 if (aborted()) return;
70 if (step.kind === "boot") {
71 setBusy(true);
72 await sleep(step.ms);
73 setBusy(false);
74 push(
75 <>
76 <span style={{ color: "var(--alc-success)" }}>✓ </span>
77 <span style={{ color: "var(--alc-fg-invert)", fontWeight: 600 }}>{step.label}</span>
78 <span style={{ color: "var(--alc-code-comment)" }}>{` (${step.detail})`}</span>
79 <span style={{ color: "var(--alc-success)" }}> created</span>
80 </>,
81 );
82 } else if (step.kind === "ready") {
83 await sleep(step.ms);
84 push(
85 <>
86 <span style={{ color: "var(--alc-code-comment)" }}> → </span>
87 <span style={{ color: "var(--alc-accent-bright)" }}>http://localhost:1337</span>
88 </>,
89 );
90 push(
91 <>
92 <span style={{ color: "var(--alc-code-comment)" }}>Watching for changes…</span>
93 </>,
94 );
95 } else if (step.kind === "edit") {
96 await sleep(step.ms);
97 push(
98 <>
99 <span style={{ color: "var(--alc-warn)" }}>↻ </span>
100 <span style={{ color: "var(--alc-code-comment)" }}>{step.label} changed</span>
101 </>,
102 );
103 } else if (step.kind === "reload") {
104 setBusy(true);
105 await sleep(step.ms);
106 setBusy(false);
107 push(
108 <>
109 <span style={{ color: "var(--alc-success)" }}>✓ </span>
110 <span style={{ color: "var(--alc-fg-invert)", fontWeight: 600 }}>Api</span>
111 <span style={{ color: "var(--alc-code-comment)" }}>{` reloaded in `}</span>
112 <span style={{ color: "var(--alc-fg-invert)", fontWeight: 600 }}>
113 {step.detail}
114 </span>
115 </>,
116 );
117 } else if (step.kind === "diff") {
118 await sleep(step.ms);
119 push(
120 <>
121 <span style={{ color: "var(--alc-success)" }}>+ </span>
122 <span style={{ color: "var(--alc-fg-invert)", fontWeight: 600 }}>{step.label}</span>
123 <span style={{ color: "var(--alc-code-comment)" }}>{` (${step.detail})`}</span>
124 <span style={{ color: "var(--alc-success)" }}> created</span>
125 </>,
126 );
127 } else if (step.kind === "wire") {
128 await sleep(step.ms);
129 push(
130 <>
131 <span style={{ color: "var(--alc-warn)" }}>~ </span>
132 <span style={{ color: "var(--alc-code-comment)" }}>wired </span>
133 <span style={{ color: "var(--alc-code-type)" }}>{step.label}</span>
134 </>,
135 );
136 } else if (step.kind === "request") {
137 await sleep(step.ms);
138 push(
139 <>
140 <span
141 style={{ color: "var(--alc-code-comment)" }}
142 >{`[${new Date().toLocaleTimeString().slice(0, 8)}] `}</span>
143 <span style={{ color: "var(--alc-fg-invert)" }}>{step.label}</span>
144 <span style={{ color: "var(--alc-success)" }}>{` ${step.detail}`}</span>
145 </>,
146 );
147 }
148 }
149 await sleep(2400);
150 }
151 };
152
153 void run();
154 return () => {
155 cancelRef.current = true;
156 };
157 }, []);
158
159 const spinner = useSpinner(busy);
160
161 return (
162 <TermChrome title={title} badge="DEV" badgeColor="var(--alc-accent-bright)" maxLines={15}>
163 {lines.map((l) => (
164 <Line key={l.key}>{l.text}</Line>
165 ))}
166 {busy && (
167 <Line>
168 <span style={{ color: "var(--alc-code-comment)" }}>{spinner} </span>
169 </Line>
170 )}
171 </TermChrome>
172 );
173 }