The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { useRef, useState, type ReactNode } from "react"; |
| 2 | import { Line, sleep, TermChrome, useInViewLoop, useSpinner } from "./_terminal"; |
| 3 | |
| 4 | /** |
| 5 | * Faithful emulation of the real `alchemy dev` TUI |
| 6 | * (packages/alchemy/src/Cli/components/view/SigilCli.tsx + PlanView.tsx): |
| 7 | * |
| 8 | * - static scrollback on top (`$ alchemy dev`, Vite HMR and Worker reloads) |
| 9 | * - the live plan widget below: a `Plan · <counts>` summary rule, TaskRows |
| 10 | * with braille spinners, per-status colors/glyphs from Util/Theme.ts, and |
| 11 | * the KeyBar footer (`Starting dev stack (n/m) │ p hide widget • Ctrl+C exit`) |
| 12 | * - on success in dev mode the widget flips to the `Output` view |
| 13 | * (`{ url: '…' }` via node inspect), exactly like `startApplySession` |
| 14 | * does when the stack output is ready. |
| 15 | */ |
| 16 | |
| 17 | // Palette lifted verbatim from packages/alchemy/src/Util/Theme.ts, for a |
| 18 | // dark terminal. Marketing pages override the variables in light mode. |
| 19 | const C = { |
| 20 | brand: "var(--alc-tui-brand, #e28a5b)", |
| 21 | success: "var(--alc-tui-success, #9acb69)", |
| 22 | warning: "var(--alc-tui-warning, #efb85a)", |
| 23 | info: "var(--alc-tui-info, #b6c77a)", |
| 24 | muted: "var(--alc-tui-muted, #8f887c)", |
| 25 | accentBright: "var(--alc-tui-accent-bright, #c5df8c)", |
| 26 | emphasis: "var(--alc-tui-emphasis, #f5f0e6)", |
| 27 | }; |
| 28 | |
| 29 | type Status = "pending" | "creating" | "created" | "updating" | "updated" | "no change"; |
| 30 | |
| 31 | interface RowState { |
| 32 | id: string; |
| 33 | type: string; |
| 34 | status: Status; |
| 35 | elapsed?: string; |
| 36 | } |
| 37 | |
| 38 | const statusColor = (s: Status): string => |
| 39 | s === "creating" || s === "created" |
| 40 | ? C.success |
| 41 | : s === "updating" || s === "updated" |
| 42 | ? C.warning |
| 43 | : C.muted; |
| 44 | |
| 45 | const inProgress = (s: Status) => s === "creating" || s === "updating"; |
| 46 | |
| 47 | interface TuiState { |
| 48 | logs: ReactNode[]; |
| 49 | rows: RowState[]; |
| 50 | view: "plan" | "output"; |
| 51 | label: string; // "Starting dev stack" | "Dev stack ready" |
| 52 | busy: boolean; |
| 53 | widget: boolean; |
| 54 | } |
| 55 | |
| 56 | const BOOT: TuiState = { |
| 57 | logs: [], |
| 58 | rows: [], |
| 59 | view: "plan", |
| 60 | label: "Starting dev stack", |
| 61 | busy: true, |
| 62 | widget: false, |
| 63 | }; |
| 64 | |
| 65 | let _key = 0; |
| 66 | |
| 67 | export default function DevTuiTerminal({ title = "~/my-app" }: { title?: string }) { |
| 68 | const ref = useRef<HTMLDivElement>(null); |
| 69 | const [state, setState] = useState<TuiState>(BOOT); |
| 70 | const spinner = useSpinner(true); |
| 71 | |
| 72 | useInViewLoop(ref, async (signal) => { |
| 73 | const set = (patch: Partial<TuiState>) => setState((s) => ({ ...s, ...patch })); |
| 74 | const log = (node: ReactNode) => |
| 75 | setState((s) => ({ |
| 76 | ...s, |
| 77 | logs: [...s.logs, <span key={`l${++_key}`}>{node}</span>].slice(-4), |
| 78 | })); |
| 79 | const row = (id: string, patch: Partial<RowState>) => |
| 80 | setState((s) => ({ |
| 81 | ...s, |
| 82 | rows: s.rows.map((r) => (r.id === id ? { ...r, ...patch } : r)), |
| 83 | })); |
| 84 | |
| 85 | while (!signal.aborted) { |
| 86 | // ── boot: one command starts the Worker and the Vite app ──────── |
| 87 | setState({ ...BOOT, logs: [] }); |
| 88 | log( |
| 89 | <> |
| 90 | <span style={{ color: C.muted }}>$ </span> |
| 91 | <span style={{ color: C.emphasis }}>alchemy dev</span> |
| 92 | </>, |
| 93 | ); |
| 94 | await sleep(500); |
| 95 | if (signal.aborted) return; |
| 96 | set({ |
| 97 | widget: true, |
| 98 | rows: [ |
| 99 | { id: "Api", type: "Cloudflare.Worker", status: "creating" }, |
| 100 | { id: "Web", type: "Cloudflare.Website.Vite", status: "creating" }, |
| 101 | ], |
| 102 | }); |
| 103 | await sleep(900); |
| 104 | if (signal.aborted) return; |
| 105 | row("Api", { status: "created", elapsed: "0.6s" }); |
| 106 | await sleep(700); |
| 107 | if (signal.aborted) return; |
| 108 | row("Web", { status: "created", elapsed: "1.3s" }); |
| 109 | await sleep(500); |
| 110 | if (signal.aborted) return; |
| 111 | set({ view: "output", label: "Dev stack ready", busy: false }); |
| 112 | await sleep(2200); |
| 113 | if (signal.aborted) return; |
| 114 | |
| 115 | // ── edit the frontend: Vite HMR ───────────────────────────────── |
| 116 | log( |
| 117 | <> |
| 118 | <span style={{ color: C.muted }}>[web] </span> |
| 119 | <span style={{ color: C.info }}>hmr update </span> |
| 120 | <span style={{ color: C.emphasis }}>/src/App.tsx</span> |
| 121 | <span style={{ color: C.muted }}> (96ms)</span> |
| 122 | </>, |
| 123 | ); |
| 124 | await sleep(1800); |
| 125 | if (signal.aborted) return; |
| 126 | |
| 127 | // ── edit the backend: the Worker reloads in place ─────────────── |
| 128 | log( |
| 129 | <> |
| 130 | <span style={{ color: C.muted }}>[api] </span> |
| 131 | <span style={{ color: C.info }}>reloaded </span> |
| 132 | <span style={{ color: C.emphasis }}>src/api.ts</span> |
| 133 | <span style={{ color: C.muted }}> (41ms)</span> |
| 134 | </>, |
| 135 | ); |
| 136 | await sleep(1800); |
| 137 | if (signal.aborted) return; |
| 138 | |
| 139 | // ── edit alchemy.run.ts: a new bucket, bound to the Worker ────── |
| 140 | log( |
| 141 | <> |
| 142 | <span style={{ color: C.warning }}>↻ </span> |
| 143 | <span style={{ color: C.muted }}>alchemy.run.ts changed — reloading stack</span> |
| 144 | </>, |
| 145 | ); |
| 146 | await sleep(600); |
| 147 | if (signal.aborted) return; |
| 148 | set({ |
| 149 | view: "plan", |
| 150 | label: "Starting dev stack", |
| 151 | busy: true, |
| 152 | rows: [ |
| 153 | { id: "Uploads", type: "Cloudflare.R2.Bucket", status: "creating" }, |
| 154 | { id: "Api", type: "Cloudflare.Worker", status: "pending" }, |
| 155 | { id: "Web", type: "Cloudflare.Website.Vite", status: "no change" }, |
| 156 | ], |
| 157 | }); |
| 158 | await sleep(900); |
| 159 | if (signal.aborted) return; |
| 160 | row("Uploads", { status: "created", elapsed: "0.3s" }); |
| 161 | row("Api", { status: "updating" }); |
| 162 | await sleep(800); |
| 163 | if (signal.aborted) return; |
| 164 | row("Api", { status: "updated", elapsed: "0.4s" }); |
| 165 | await sleep(500); |
| 166 | if (signal.aborted) return; |
| 167 | set({ view: "output", label: "Dev stack ready", busy: false }); |
| 168 | await sleep(3500); |
| 169 | } |
| 170 | }); |
| 171 | |
| 172 | const done = state.rows.filter((r) => !inProgress(r.status)).length; |
| 173 | const summaryCounts = summarize(state.rows); |
| 174 | |
| 175 | return ( |
| 176 | <div ref={ref}> |
| 177 | <TermChrome title={title} badge="DEV" badgeColor="var(--alc-accent-bright)" maxLines={12}> |
| 178 | {state.logs.map((l, i) => ( |
| 179 | <Line key={i}>{l}</Line> |
| 180 | ))} |
| 181 | {state.widget && ( |
| 182 | <> |
| 183 | {/* summary rule — PlanView renders `Plan · counts` (or `Output`) */} |
| 184 | <Rule> |
| 185 | {state.view === "output" ? ( |
| 186 | <b style={{ color: C.brand }}>Output</b> |
| 187 | ) : ( |
| 188 | <> |
| 189 | <b style={{ color: C.brand }}>Plan</b> |
| 190 | <span style={{ color: C.muted }}> · </span> |
| 191 | {summaryCounts.map((part, i) => ( |
| 192 | <span key={part.label}> |
| 193 | {i > 0 && <span style={{ color: C.muted }}> · </span>} |
| 194 | <span style={{ color: part.color }}>{part.label}</span> |
| 195 | </span> |
| 196 | ))} |
| 197 | </> |
| 198 | )} |
| 199 | </Rule> |
| 200 | {state.view === "output" ? ( |
| 201 | <> |
| 202 | <Line> |
| 203 | <span style={{ color: C.muted }}>{"{"}</span> |
| 204 | </Line> |
| 205 | <Line> |
| 206 | <span style={{ color: C.muted }}>{" "}web: </span> |
| 207 | <span style={{ color: C.accentBright }}>'http://localhost:5173'</span> |
| 208 | <span style={{ color: C.muted }}>,</span> |
| 209 | </Line> |
| 210 | <Line> |
| 211 | <span style={{ color: C.muted }}>{" "}api: </span> |
| 212 | <span style={{ color: C.accentBright }}>'http://localhost:1337'</span> |
| 213 | </Line> |
| 214 | <Line> |
| 215 | <span style={{ color: C.muted }}>{"}"}</span> |
| 216 | </Line> |
| 217 | </> |
| 218 | ) : ( |
| 219 | state.rows.map((r) => ( |
| 220 | <Line key={r.id}> |
| 221 | <span style={{ color: statusColor(r.status) }}> |
| 222 | {inProgress(r.status) ? spinner : r.status === "pending" ? "·" : "✓"} |
| 223 | </span>{" "} |
| 224 | <b style={{ color: C.emphasis }}>{r.id}</b> |
| 225 | <span style={{ color: C.muted }}> ({r.type}) </span> |
| 226 | <span style={{ color: statusColor(r.status) }}>{r.status}</span> |
| 227 | {r.elapsed && <span style={{ color: C.muted }}> ({r.elapsed})</span>} |
| 228 | </Line> |
| 229 | )) |
| 230 | )} |
| 231 | {/* keybar footer — spinner + label (+ progress) │ keys */} |
| 232 | <Rule> |
| 233 | {state.busy && <span style={{ color: C.brand }}>{spinner} </span>} |
| 234 | <b style={{ color: C.brand }}>{state.label}</b> |
| 235 | {state.busy && state.rows.length > 0 && ( |
| 236 | <span style={{ color: C.muted }}> |
| 237 | {" "} |
| 238 | ({done}/{state.rows.length}) |
| 239 | </span> |
| 240 | )} |
| 241 | <span style={{ color: C.muted }}> │ </span> |
| 242 | <Key k="p" label="hide widget" /> |
| 243 | <span style={{ color: C.muted }}> • </span> |
| 244 | <Key k="Ctrl+C" label="exit" /> |
| 245 | </Rule> |
| 246 | </> |
| 247 | )} |
| 248 | </TermChrome> |
| 249 | </div> |
| 250 | ); |
| 251 | } |
| 252 | |
| 253 | function Rule({ children }: { children: ReactNode }) { |
| 254 | return ( |
| 255 | <Line |
| 256 | style={{ |
| 257 | borderTop: "1px solid rgba(143, 136, 124, 0.35)", |
| 258 | marginTop: "0.45em", |
| 259 | paddingTop: "0.45em", |
| 260 | minHeight: "2em", |
| 261 | }} |
| 262 | > |
| 263 | {children} |
| 264 | </Line> |
| 265 | ); |
| 266 | } |
| 267 | |
| 268 | function Key({ k, label }: { k: string; label: string }) { |
| 269 | return ( |
| 270 | <> |
| 271 | <b style={{ color: C.brand }}>{k}</b> |
| 272 | <span style={{ color: C.muted }}> {label}</span> |
| 273 | </> |
| 274 | ); |
| 275 | } |
| 276 | |
| 277 | function summarize(rows: RowState[]): { label: string; color: string }[] { |
| 278 | const counts = new Map<Status, number>(); |
| 279 | for (const r of rows) counts.set(r.status, (counts.get(r.status) ?? 0) + 1); |
| 280 | const order: Status[] = ["creating", "updating", "pending", "created", "updated", "no change"]; |
| 281 | return order |
| 282 | .filter((s) => counts.has(s)) |
| 283 | .map((s) => ({ |
| 284 | label: `${counts.get(s)} ${s}`, |
| 285 | color: statusColor(s), |
| 286 | })); |
| 287 | } |