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

main
tsx 208 lines 6.79 KB
Raw
1 /** The home page: all repositories, GitHub-dashboard style. */
2 import { useEffect, useState } from "react";
3 import {
4 createRepo,
5 listRepos,
6 type Connection,
7 type Repo,
8 type RepoCreated,
9 type User,
10 } from "../client.ts";
11 import {
12 Badge,
13 Button,
14 CopyButton,
15 ErrorBox,
16 ForkIcon,
17 Input,
18 RepoIcon,
19 Spinner,
20 } from "../components.tsx";
21 import { timeAgo } from "../format.ts";
22 import { href, Link } from "../router.tsx";
23
24 const RepoRow = ({ repo }: { repo: Repo }) => (
25 <li className="flex items-start justify-between gap-4 border-b border-border-muted py-4 last:border-b-0">
26 <div className="min-w-0">
27 <div className="flex flex-wrap items-center gap-2">
28 <RepoIcon className="shrink-0 text-fg-muted" />
29 <Link
30 to={href(repo.owner, repo.name)}
31 className="truncate text-base font-semibold text-accent hover:underline"
32 >
33 {repo.owner}/{repo.name}
34 </Link>
35 {repo.forkOf !== null && (
36 <Badge>
37 <span className="inline-flex items-center gap-1">
38 <ForkIcon className="size-3" /> fork of {repo.forkOf}
39 </span>
40 </Badge>
41 )}
42 <Badge>{repo.public ? "Public" : "Private"}</Badge>
43 {repo.readOnly && <Badge tone="attention">read-only</Badge>}
44 {repo.status !== "ready" && <Badge tone="danger">{repo.status}</Badge>}
45 </div>
46 {repo.description !== null && repo.description.length > 0 && (
47 <p className="mt-1 text-sm text-fg-muted">{repo.description}</p>
48 )}
49 {/* No size here: the list renders from the registry's denormalized
50 columns, which don't carry object stats (that would re-create the
51 one-DO-wake-per-row N+1 the list was built to avoid). */}
52 <p className="mt-1.5 text-xs text-fg-muted">created {timeAgo(repo.createdAt)}</p>
53 </div>
54 </li>
55 );
56
57 /** Post-create panel: the remote to add. The password is one of your API keys. */
58 const CreatedPanel = ({ created }: { created: RepoCreated }) => (
59 <div className="mb-4 space-y-3 rounded-md border border-success/40 bg-success/5 p-4 text-sm">
60 <p className="font-medium text-success">
61 Repository {created.repo.owner}/{created.repo.name} created. Push with one of your API keys as
62 the password (Settings → API keys).
63 </p>
64 <div className="flex items-center gap-2">
65 <code className="grow overflow-x-auto rounded bg-canvas-subtle px-2 py-1 font-mono text-xs">
66 git remote add origin {created.remote.replace("://", "://x:YOUR_API_KEY@")}
67 </code>
68 <CopyButton
69 text={`git remote add origin ${created.remote.replace("://", "://x:YOUR_API_KEY@")}`}
70 />
71 </div>
72 </div>
73 );
74
75 const NewRepoForm = ({
76 connection,
77 user,
78 onCreated,
79 }: {
80 connection: Connection;
81 user: User;
82 onCreated: (created: RepoCreated) => void;
83 }) => {
84 // Repositories are owned by a principal's id, so the owner is you.
85 const [owner, setOwner] = useState(user.id);
86 const [name, setName] = useState("");
87 const [description, setDescription] = useState("");
88 const [isPublic, setIsPublic] = useState(true);
89 const [error, setError] = useState<unknown>(null);
90 const [busy, setBusy] = useState(false);
91
92 const submit = async () => {
93 setBusy(true);
94 setError(null);
95 try {
96 const created = await createRepo(connection, {
97 owner: owner.trim(),
98 name: name.trim(),
99 public: isPublic,
100 ...(description.trim() ? { description: description.trim() } : {}),
101 });
102 onCreated(created);
103 } catch (cause) {
104 setError(cause);
105 } finally {
106 setBusy(false);
107 }
108 };
109
110 return (
111 <form
112 className="mb-4 space-y-3 rounded-md border border-border-muted bg-canvas-subtle p-4"
113 onSubmit={(event) => {
114 event.preventDefault();
115 void submit();
116 }}
117 >
118 <div className="flex gap-2">
119 <Input value={owner} onChange={setOwner} placeholder="owner" mono />
120 <span className="self-center text-fg-muted">/</span>
121 <Input value={name} onChange={setName} placeholder="name" mono />
122 </div>
123 <Input value={description} onChange={setDescription} placeholder="Description (optional)" />
124 <label className="flex items-center gap-2 text-sm">
125 <input
126 type="checkbox"
127 checked={isPublic}
128 onChange={(event) => setIsPublic(event.target.checked)}
129 />
130 Public — anyone can read and clone without signing in
131 </label>
132 {error != null && <ErrorBox error={error} />}
133 <Button kind="primary" type="submit" disabled={busy || !owner || !name}>
134 {busy ? "Creating…" : "Create repository"}
135 </Button>
136 </form>
137 );
138 };
139
140 export const ReposPage = ({ connection, user }: { connection: Connection; user: User | null }) => {
141 const [repos, setRepos] = useState<Repo[] | null>(null);
142 const [cursor, setCursor] = useState<string | null>(null);
143 const [error, setError] = useState<unknown>(null);
144 const [showNew, setShowNew] = useState(false);
145 const [created, setCreated] = useState<RepoCreated | null>(null);
146
147 const load = async (nextCursor?: string) => {
148 try {
149 const page = await listRepos(connection, {
150 limit: 50,
151 ...(nextCursor ? { cursor: nextCursor } : {}),
152 });
153 setRepos((existing) => (nextCursor ? [...(existing ?? []), ...page.items] : page.items));
154 setCursor(page.hasMore ? page.nextCursor : null);
155 } catch (cause) {
156 setError(cause);
157 }
158 };
159
160 useEffect(() => {
161 void load();
162 // eslint-disable-next-line react-hooks/exhaustive-deps
163 }, [connection.url, user?.id]);
164
165 if (error != null) return <ErrorBox error={error} />;
166 if (repos === null) return <Spinner />;
167
168 return (
169 <div>
170 <div className="mb-4 flex items-center justify-between">
171 <h1 className="text-xl font-semibold">Repositories</h1>
172 {user !== null && (
173 <Button kind="primary" onClick={() => setShowNew((value) => !value)}>
174 New repository
175 </Button>
176 )}
177 </div>
178 {created !== null && <CreatedPanel created={created} />}
179 {showNew && user !== null && (
180 <NewRepoForm
181 connection={connection}
182 user={user}
183 onCreated={(result) => {
184 setCreated(result);
185 setShowNew(false);
186 void load();
187 }}
188 />
189 )}
190 {repos.length === 0 ? (
191 <div className="rounded-md border border-border-muted py-16 text-center text-fg-muted">
192 No repositories yet — create one to get started.
193 </div>
194 ) : (
195 <ul>
196 {repos.map((repo) => (
197 <RepoRow key={repo.repoId} repo={repo} />
198 ))}
199 </ul>
200 )}
201 {cursor !== null && (
202 <div className="mt-4 flex justify-center">
203 <Button onClick={() => void load(cursor)}>Load more</Button>
204 </div>
205 )}
206 </div>
207 );
208 };