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

main
tsx 101 lines 3.59 KB
Raw
1 import * as Effect from "effect/Effect";
2 import * as HttpApiClient from "effect/http-api/HttpApiClient";
3 import * as FetchHttpClient from "effect/http/FetchHttpClient";
4 import { StrictMode, useEffect, useState, type FormEvent } from "react";
5 import { createRoot } from "react-dom/client";
6 import { ShortyApi, type LinkView } from "../../src/ShortyApi.ts";
7 import "./styles.css";
8
9 const API_URL = import.meta.env.VITE_API_URL as string;
10
11 // A typed client derived from the same `ShortyApi` value the Worker serves.
12 const makeClient = HttpApiClient.make(ShortyApi, { baseUrl: API_URL });
13
14 const call = <A, E>(f: (client: Effect.Success<typeof makeClient>) => Effect.Effect<A, E>) =>
15 Effect.runPromise(makeClient.pipe(Effect.flatMap(f), Effect.provide(FetchHttpClient.layer)));
16
17 /** Live click count for one link, pushed by its Durable Object over a WebSocket. */
18 function useClicks(code: string, initial: number) {
19 const [clicks, setClicks] = useState(initial);
20 useEffect(() => {
21 const socket = new WebSocket(`${API_URL.replace(/^http/, "ws")}/links/${code}/live`);
22 socket.onmessage = (event) => setClicks(JSON.parse(event.data).clicks);
23 return () => socket.close();
24 }, [code]);
25 return clicks;
26 }
27
28 function LinkRow({ link }: { link: LinkView }) {
29 const clicks = useClicks(link.code, link.clicks);
30 const short = `${API_URL}/${link.code}`;
31 return (
32 <li className="flex items-center gap-6 rounded-2xl border border-white/10 bg-white/5 px-6 py-4">
33 <div className="min-w-0 flex-1">
34 <p className="truncate text-lg font-medium text-stone-100">
35 {link.preview?.title ?? <span className="text-stone-500">Fetching preview…</span>}
36 </p>
37 <p className="truncate text-sm text-stone-400">{link.url}</p>
38 </div>
39 <a
40 href={short}
41 target="_blank"
42 rel="noreferrer"
43 className="font-mono text-sm text-lime-300 hover:underline"
44 >
45 /{link.code}
46 </a>
47 <p className="w-20 text-right font-mono text-2xl tabular-nums text-stone-100">{clicks}</p>
48 </li>
49 );
50 }
51
52 function App() {
53 const [links, setLinks] = useState<readonly LinkView[]>([]);
54 const [url, setUrl] = useState("");
55
56 const refresh = () => call((client) => client.links.list()).then(setLinks);
57
58 useEffect(() => {
59 refresh();
60 // Previews arrive from the background job; poll until every link has one.
61 const timer = setInterval(refresh, 3000);
62 return () => clearInterval(timer);
63 }, []);
64
65 const create = async (event: FormEvent) => {
66 event.preventDefault();
67 if (!url) return;
68 await call((client) => client.links.create({ payload: { url } }));
69 setUrl("");
70 refresh();
71 };
72
73 return (
74 <main className="mx-auto max-w-3xl px-6 py-16">
75 <p className="font-mono text-sm tracking-[0.3em] text-lime-300 uppercase">Shorty</p>
76 <h1 className="mt-3 font-serif text-5xl font-semibold text-stone-50">Your links</h1>
77 <form onSubmit={create} className="mt-10 flex gap-3">
78 <input
79 value={url}
80 onChange={(event) => setUrl(event.target.value)}
81 placeholder="https://…"
82 className="flex-1 rounded-xl border border-white/10 bg-white/5 px-4 py-3 text-stone-100 outline-none focus:border-lime-300/60"
83 />
84 <button className="rounded-xl bg-lime-300 px-5 py-3 font-medium text-stone-900">
85 Shorten
86 </button>
87 </form>
88 <ul className="mt-8 space-y-3">
89 {links.map((link) => (
90 <LinkRow key={link.code} link={link} />
91 ))}
92 </ul>
93 </main>
94 );
95 }
96
97 createRoot(document.getElementById("root")!).render(
98 <StrictMode>
99 <App />
100 </StrictMode>,
101 );