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

main
tsx 310 lines 9.71 KB
Raw
1 import { useEffect, useRef, useState, type ReactNode } from "react";
2 import { AWS_COLOR, CF_COLOR, tint } from "../marketing/diagrams/_colors";
3
4 const tok =
5 (color: string) =>
6 ({ children }: { children: ReactNode }) => <span style={{ color }}>{children}</span>;
7 const K = tok("var(--alc-code-keyword)");
8 const S = tok("var(--alc-code-string)");
9 const F = tok("var(--alc-code-fn)");
10 const V = tok("var(--alc-code-var)");
11 const T = tok("var(--alc-code-type)");
12 const C = ({ children }: { children: ReactNode }) => (
13 <span style={{ color: "var(--alc-code-comment)", fontStyle: "italic" }}>{children}</span>
14 );
15
16 type ResKind = "kv" | "ddb" | "d1";
17
18 interface Impl {
19 layer: string; // Layer name shown in code
20 resourceLabel: string; // node label
21 resourceSub: string; // node sub-label
22 bindCall: string; // bind call shown on the arrow
23 color: string;
24 kind: ResKind;
25 }
26
27 const D1_COLOR = "#0080FF";
28
29 const IMPLS: Impl[] = [
30 {
31 layer: "SessionsKV",
32 resourceLabel: "Sessions",
33 resourceSub: "Cloudflare.KV.Namespace",
34 bindCall: "KV.ReadWriteNamespace",
35 color: CF_COLOR,
36 kind: "kv",
37 },
38 {
39 layer: "SessionsDynamoDB",
40 resourceLabel: "Sessions",
41 resourceSub: "AWS.DynamoDB.Table",
42 bindCall: "DynamoDB.PutItem",
43 color: AWS_COLOR,
44 kind: "ddb",
45 },
46 {
47 layer: "SessionsD1",
48 resourceLabel: "Sessions",
49 resourceSub: "Cloudflare.D1.Database",
50 bindCall: "D1Database.bind",
51 color: D1_COLOR,
52 kind: "d1",
53 },
54 ];
55
56 const DWELL_MS = 2800;
57
58 function ResourceIcon({ kind, color }: { kind: ResKind; color: string }) {
59 const fill = tint(color, 0.18);
60 if (kind === "kv") {
61 return (
62 <svg width="22" height="22" viewBox="0 0 32 32" fill="none" aria-hidden>
63 <circle cx="11" cy="16" r="6" fill={fill} stroke={color} strokeWidth="1.5" />
64 <circle cx="11" cy="16" r="2.2" fill={color} />
65 <path
66 d="M17 16 L27 16 M23 16 L23 21 M27 16 L27 12"
67 stroke={color}
68 strokeWidth="1.5"
69 strokeLinecap="round"
70 />
71 </svg>
72 );
73 }
74 if (kind === "ddb") {
75 return (
76 <svg width="22" height="22" viewBox="0 0 32 32" fill="none" aria-hidden>
77 <ellipse cx="16" cy="8" rx="10" ry="3" fill={fill} stroke={color} strokeWidth="1.4" />
78 <ellipse cx="16" cy="16" rx="10" ry="3" fill={fill} stroke={color} strokeWidth="1.4" />
79 <ellipse cx="16" cy="24" rx="10" ry="3" fill={fill} stroke={color} strokeWidth="1.4" />
80 </svg>
81 );
82 }
83 // d1
84 return (
85 <svg width="22" height="22" viewBox="0 0 32 32" fill="none" aria-hidden>
86 <ellipse cx="16" cy="8" rx="10" ry="3" fill={fill} stroke={color} strokeWidth="1.5" />
87 <path
88 d="M6 8 V24 C6 25.7 10.5 27 16 27 C21.5 27 26 25.7 26 24 V8"
89 fill={fill}
90 stroke={color}
91 strokeWidth="1.5"
92 />
93 <path
94 d="M9 14 L23 14 M9 18 L23 18 M9 22 L19 22"
95 stroke={color}
96 strokeWidth="1.2"
97 strokeLinecap="round"
98 />
99 </svg>
100 );
101 }
102
103 export default function ServiceLayerSwap() {
104 const wrapRef = useRef<HTMLDivElement>(null);
105 const [idx, setIdx] = useState(0);
106
107 useEffect(() => {
108 let timer: ReturnType<typeof setTimeout> | null = null;
109 let cancelled = false;
110 const reduced = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
111
112 const tick = (i: number) => {
113 if (cancelled) return;
114 setIdx(i);
115 if (reduced) return;
116 timer = setTimeout(() => tick((i + 1) % IMPLS.length), DWELL_MS);
117 };
118 const obs = new IntersectionObserver(
119 (entries) => {
120 for (const e of entries) {
121 if (e.isIntersecting) {
122 tick(0);
123 obs.disconnect();
124 }
125 }
126 },
127 { threshold: 0.25 },
128 );
129 if (wrapRef.current) obs.observe(wrapRef.current);
130 return () => {
131 cancelled = true;
132 if (timer) clearTimeout(timer);
133 obs.disconnect();
134 };
135 }, []);
136
137 const active = IMPLS[idx]!;
138
139 return (
140 <div ref={wrapRef} className="service-swap-grid" data-nosnippet="">
141 {/* LEFT — code: Worker that consumes Sessions, Layer name swaps */}
142 <div className="alc-code-block">
143 <div className="alc-code-block__header">
144 <span className="alc-code-block__dot" style={{ background: "var(--alc-danger)" }} />
145 <span className="alc-code-block__dot" style={{ background: "var(--alc-warn)" }} />
146 <span
147 className="alc-code-block__dot"
148 style={{ background: "var(--alc-accent-bright)" }}
149 />
150 <span className="alc-code-block__filename">src/Api.ts</span>
151 </div>
152 <pre className="alc-code-block__pre">
153 <C>{"// Service interface — defined once."}</C>
154 {"\n"}
155 <K>export class</K> <T>Sessions</T> <K>extends</K> <V>Context</V>.<F>Service</F>
156 {"<"}
157 <T>Sessions</T>, {"{"}
158 {"\n "}
159 <V>get</V>: (<V>id</V>: <T>string</T>) {"=>"} <V>Effect</V>.<T>Effect</T>
160 {"<"}
161 <T>Session</T>
162 {">"};{"\n "}
163 <V>put</V>: (<V>s</V>: <T>Session</T>) {"=>"} <V>Effect</V>.<T>Effect</T>
164 {"<"}
165 <T>void</T>
166 {">"};{"\n"}
167 {"}>()("}
168 <S>"Sessions"</S>
169 {") {}"}
170 {"\n\n"}
171 <C>{"// Worker code never knows which Layer is providing it."}</C>
172 {"\n"}
173 <K>export default class</K> <T>Api</T> <K>extends</K> <V>Cloudflare</V>.<F>Worker</F>
174 {"<"}
175 <T>Api</T>
176 {">()("}
177 {"\n "}
178 <S>"Api"</S>,{"\n "}
179 <V>Effect</V>.<F>gen</F>(<K>function</K>* () {"{"}
180 {"\n "}
181 <K>const</K> sessions = <K>yield</K>* <V>Sessions</V>;{"\n "}
182 <K>return</K> {"{ fetch: handler(sessions) };"}
183 {"\n "}
184 {"}).pipe("}
185 {"\n "}
186 <C>{"// Swap one Layer — resources, bindings, IAM all swap with it."}</C>
187 {"\n "}
188 <V>Effect</V>.<F>provide</F>(
189 <span key={idx} className="layer-swap" style={{ color: active.color, fontWeight: 600 }}>
190 {active.layer}
191 </span>
192 ),
193 {"\n "}
194 {"),"}
195 {"\n"}
196 {") {}"}
197 </pre>
198 </div>
199
200 {/* RIGHT — visual: Sessions → Layer → Resource */}
201 <div className="service-swap-stage">
202 <div className="service-swap-stage__node service-swap-stage__node--accent">
203 <svg width="22" height="22" viewBox="0 0 32 32" fill="none" aria-hidden>
204 <rect
205 x="4"
206 y="6"
207 width="24"
208 height="20"
209 rx="4"
210 fill="rgba(184,138,74,0.15)"
211 stroke="var(--alc-accent-deep)"
212 strokeWidth="1.5"
213 />
214 <path
215 d="M9 13 H23 M9 17 H19 M9 21 H21"
216 stroke="var(--alc-accent-deep)"
217 strokeWidth="1.4"
218 strokeLinecap="round"
219 />
220 </svg>
221 <div className="service-swap-stage__label">Sessions</div>
222 <div className="service-swap-stage__sub">Context.Service</div>
223 </div>
224
225 <div
226 className="service-swap-stage__edge service-swap-stage__edge--down"
227 style={{ borderColor: active.color }}
228 >
229 <span
230 className="service-swap-stage__edge-label"
231 style={{ color: active.color, borderColor: active.color }}
232 >
233 Layer.effect
234 </span>
235 </div>
236
237 <div
238 key={`layer-${idx}`}
239 className="service-swap-stage__node layer-swap-node"
240 style={{
241 borderColor: active.color,
242 boxShadow: `0 0 0 2px ${tint(active.color, 0.12)}`,
243 }}
244 >
245 <svg width="22" height="22" viewBox="0 0 32 32" fill="none" aria-hidden>
246 <path
247 d="M16 4 L28 10 L16 16 L4 10 Z"
248 fill={tint(active.color, 0.18)}
249 stroke={active.color}
250 strokeWidth="1.4"
251 strokeLinejoin="round"
252 />
253 <path
254 d="M4 16 L16 22 L28 16"
255 stroke={active.color}
256 strokeWidth="1.4"
257 fill="none"
258 strokeLinejoin="round"
259 opacity="0.85"
260 />
261 <path
262 d="M4 22 L16 28 L28 22"
263 stroke={active.color}
264 strokeWidth="1.4"
265 fill="none"
266 strokeLinejoin="round"
267 opacity="0.55"
268 />
269 </svg>
270 <div className="service-swap-stage__label" style={{ color: active.color }}>
271 {active.layer}
272 </div>
273 <div className="service-swap-stage__sub">Layer</div>
274 </div>
275
276 <div
277 className="service-swap-stage__edge service-swap-stage__edge--down"
278 style={{ borderColor: active.color }}
279 >
280 <span
281 className="service-swap-stage__edge-label"
282 style={{ color: active.color, borderColor: active.color }}
283 >
284 {active.bindCall}
285 </span>
286 </div>
287
288 <div
289 key={`res-${idx}`}
290 className="service-swap-stage__node layer-swap-node"
291 style={{ borderColor: active.color }}
292 >
293 <ResourceIcon kind={active.kind} color={active.color} />
294 <div className="service-swap-stage__label">{active.resourceLabel}</div>
295 <div className="service-swap-stage__sub">{active.resourceSub}</div>
296 </div>
297
298 <div className="service-swap-stage__steps" aria-hidden>
299 {IMPLS.map((_, i) => (
300 <span
301 key={i}
302 className={`service-swap-stage__dot${i === idx ? " is-active" : ""}`}
303 style={i === idx ? { background: active.color } : undefined}
304 />
305 ))}
306 </div>
307 </div>
308 </div>
309 );
310 }