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
| 1 | import { useEffect, useRef, useState } from "react"; |
| 2 | import { Line, sleep, TermChrome, useSpinner } from "./_terminal"; |
| 3 | |
| 4 | const RESOURCES = [ |
| 5 | { id: "Bucket", type: "Cloudflare.R2.Bucket" }, |
| 6 | { id: "Worker", type: "Cloudflare.Worker" }, |
| 7 | ]; |
| 8 | |
| 9 | const OUTPUTS: { key: string; value: string }[] = [ |
| 10 | { key: "bucketName", value: '"myapp-dev_sam-bucket-a3f1"' }, |
| 11 | { |
| 12 | key: "url", |
| 13 | value: '"https://myapp-worker-dev_sam-abc123.workers.dev"', |
| 14 | }, |
| 15 | ]; |
| 16 | |
| 17 | const STAGE = "dev_sam"; |
| 18 | const COMMAND = `alchemy deploy --stage ${STAGE}`; |
| 19 | |
| 20 | type RowStatus = "pending" | "creating" | "created"; |
| 21 | |
| 22 | export default function StackOutputsTerminal({ maxLines = 12 }: { maxLines?: number }) { |
| 23 | const [cmd, setCmd] = useState(""); |
| 24 | const [caret, setCaret] = useState(false); |
| 25 | const [rows, setRows] = useState<{ id: string; type: string; status: RowStatus }[]>([]); |
| 26 | const [showOutputs, setShowOutputs] = useState(false); |
| 27 | const cancelRef = useRef(false); |
| 28 | |
| 29 | useEffect(() => { |
| 30 | cancelRef.current = false; |
| 31 | const aborted = () => cancelRef.current; |
| 32 | |
| 33 | const typeCmd = async (text: string) => { |
| 34 | setCmd(""); |
| 35 | setCaret(true); |
| 36 | for (let i = 1; i <= text.length; i++) { |
| 37 | if (aborted()) return; |
| 38 | setCmd(text.slice(0, i)); |
| 39 | await sleep(28 + Math.random() * 22); |
| 40 | } |
| 41 | await sleep(160); |
| 42 | setCaret(false); |
| 43 | }; |
| 44 | |
| 45 | const update = (id: string, status: RowStatus) => |
| 46 | setRows((rs) => rs.map((r) => (r.id === id ? { ...r, status } : r))); |
| 47 | |
| 48 | const run = async () => { |
| 49 | while (!aborted()) { |
| 50 | setCmd(""); |
| 51 | setCaret(false); |
| 52 | setRows([]); |
| 53 | setShowOutputs(false); |
| 54 | await sleep(900); |
| 55 | if (aborted()) return; |
| 56 | |
| 57 | await typeCmd(COMMAND); |
| 58 | if (aborted()) return; |
| 59 | |
| 60 | await sleep(220); |
| 61 | setRows(RESOURCES.map((r) => ({ ...r, status: "pending" as RowStatus }))); |
| 62 | await sleep(260); |
| 63 | |
| 64 | for (const r of RESOURCES) { |
| 65 | if (aborted()) return; |
| 66 | update(r.id, "creating"); |
| 67 | await sleep(620); |
| 68 | if (aborted()) return; |
| 69 | update(r.id, "created"); |
| 70 | await sleep(140); |
| 71 | } |
| 72 | |
| 73 | await sleep(260); |
| 74 | setShowOutputs(true); |
| 75 | await sleep(4200); |
| 76 | } |
| 77 | }; |
| 78 | |
| 79 | void run(); |
| 80 | return () => { |
| 81 | cancelRef.current = true; |
| 82 | }; |
| 83 | }, []); |
| 84 | |
| 85 | const anyInFlight = rows.some((r) => r.status === "creating"); |
| 86 | const spinner = useSpinner(anyInFlight); |
| 87 | const accent = "var(--alc-accent-bright)"; |
| 88 | |
| 89 | return ( |
| 90 | <TermChrome title={`alchemy · ${STAGE}`} badge="DEPLOY" badgeColor={accent} maxLines={maxLines}> |
| 91 | <Line> |
| 92 | <span style={{ color: accent }}>$ </span> |
| 93 | {cmd} |
| 94 | {caret && <span style={{ color: "var(--alc-fg-invert)" }}>▍</span>} |
| 95 | </Line> |
| 96 | {rows.length > 0 && ( |
| 97 | <> |
| 98 | <Line> </Line> |
| 99 | {rows.map((r) => { |
| 100 | const inFlight = r.status === "creating"; |
| 101 | const done = r.status === "created"; |
| 102 | const icon = inFlight ? spinner : done ? "✓" : "+"; |
| 103 | return ( |
| 104 | <Line key={r.id}> |
| 105 | <span |
| 106 | style={{ |
| 107 | color: accent, |
| 108 | width: "1.2em", |
| 109 | display: "inline-block", |
| 110 | }} |
| 111 | > |
| 112 | {icon} |
| 113 | </span> |
| 114 | <span style={{ color: "var(--alc-fg-invert)", fontWeight: 600 }}>{r.id}</span> |
| 115 | <span style={{ color: "var(--alc-code-comment)" }}>{` (${r.type})`}</span> |
| 116 | {inFlight && <span style={{ color: accent, marginLeft: 6 }}>{r.status}</span>} |
| 117 | </Line> |
| 118 | ); |
| 119 | })} |
| 120 | </> |
| 121 | )} |
| 122 | {showOutputs && ( |
| 123 | <> |
| 124 | <Line> </Line> |
| 125 | <Line> |
| 126 | <span style={{ color: accent }}>✓ </span> |
| 127 | <span>deployed in </span> |
| 128 | <span style={{ color: "var(--alc-fg-invert)", fontWeight: 600 }}>3.4s</span> |
| 129 | </Line> |
| 130 | <Line> </Line> |
| 131 | <Line> |
| 132 | <span style={{ color: "var(--alc-code-comment)" }}>outputs:</span> |
| 133 | </Line> |
| 134 | <Line> |
| 135 | <span style={{ color: "var(--alc-fg-invert)" }}>{"{"}</span> |
| 136 | </Line> |
| 137 | {OUTPUTS.map((o) => ( |
| 138 | <Line key={o.key}> |
| 139 | <span style={{ color: "var(--alc-fg-invert)" }}>{" "}</span> |
| 140 | <span style={{ color: "var(--alc-fg-invert)" }}>{o.key}</span> |
| 141 | <span style={{ color: "var(--alc-code-comment)" }}>: </span> |
| 142 | <span style={{ color: accent }}>{o.value}</span> |
| 143 | <span style={{ color: "var(--alc-fg-invert)" }}>,</span> |
| 144 | </Line> |
| 145 | ))} |
| 146 | <Line> |
| 147 | <span style={{ color: "var(--alc-fg-invert)" }}>{"}"}</span> |
| 148 | </Line> |
| 149 | </> |
| 150 | )} |
| 151 | </TermChrome> |
| 152 | ); |
| 153 | } |