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

main
tsx 238 lines 8.33 KB
Raw
1 /** The Settings tab: API keys, storage stats, compaction, delete. */
2 import { useEffect, useState } from "react";
3 import {
4 compactRepo,
5 createApiKey,
6 deleteApiKey,
7 deleteRepo,
8 listApiKeys,
9 updateRepo,
10 type ApiKey,
11 } from "../client.ts";
12 import { Badge, Button, CopyButton, ErrorBox, Input, Spinner } from "../components.tsx";
13 import { formatBytes, timeAgo } from "../format.ts";
14 import { useRouter } from "../router.tsx";
15 import type { RepoContext } from "./Repo.tsx";
16
17 const StorageCard = ({ context }: { context: RepoContext }) => {
18 const { objects, lastPush } = context.repo;
19 const [compacting, setCompacting] = useState(false);
20 const [error, setError] = useState<unknown>(null);
21 return (
22 <section className="rounded-md border border-border-muted p-4">
23 <h2 className="mb-3 font-semibold">Storage</h2>
24 <dl className="grid grid-cols-2 gap-x-8 gap-y-1 text-sm sm:grid-cols-4">
25 <dt className="text-fg-muted">Total</dt>
26 <dd>{formatBytes(objects.bytes)}</dd>
27 <dt className="text-fg-muted">Loose objects</dt>
28 <dd>{objects.loose}</dd>
29 <dt className="text-fg-muted">Packed (R2)</dt>
30 <dd>{objects.packed}</dd>
31 <dt className="text-fg-muted">Oversize (R2)</dt>
32 <dd>{objects.r2}</dd>
33 </dl>
34 {lastPush !== null && (
35 <p className="mt-3 text-xs text-fg-muted">
36 Last push: {lastPush.objects} objects, {formatBytes(lastPush.bytes)} — server ingest{" "}
37 {lastPush.ingestMs}ms (sql {lastPush.stageMs}ms), total {lastPush.totalMs}ms
38 </p>
39 )}
40 <div className="mt-3">
41 <Button
42 disabled={compacting || objects.loose === 0}
43 onClick={() => {
44 setCompacting(true);
45 setError(null);
46 compactRepo(context.connection, context.repo.owner, context.repo.name)
47 .then(() => setCompacting(false))
48 .catch((cause) => {
49 setError(cause);
50 setCompacting(false);
51 });
52 }}
53 >
54 {compacting ? "Compaction scheduled ✓" : "Compact loose objects"}
55 </Button>
56 </div>
57 {error != null && <ErrorBox error={error} />}
58 </section>
59 );
60 };
61
62 const VisibilityCard = ({ context }: { context: RepoContext }) => {
63 const [isPublic, setIsPublic] = useState(context.repo.public);
64 const [error, setError] = useState<unknown>(null);
65 const [busy, setBusy] = useState(false);
66 return (
67 <section className="rounded-md border border-border-muted p-4">
68 <h2 className="mb-2 font-semibold">Visibility</h2>
69 <p className="mb-3 text-sm text-fg-muted">
70 {isPublic
71 ? "Public — anyone can browse and clone this repository without a token."
72 : "Private — reads and clones require a token."}
73 </p>
74 <Button
75 disabled={busy}
76 onClick={() => {
77 setBusy(true);
78 setError(null);
79 updateRepo(context.connection, context.repo.owner, context.repo.name, {
80 public: !isPublic,
81 })
82 .then((updated) => {
83 setIsPublic(updated.public);
84 setBusy(false);
85 })
86 .catch((cause) => {
87 setError(cause);
88 setBusy(false);
89 });
90 }}
91 >
92 {busy ? "Saving…" : isPublic ? "Make private" : "Make public"}
93 </Button>
94 {error != null && <ErrorBox error={error} />}
95 </section>
96 );
97 };
98
99 const ApiKeysCard = ({ context }: { context: RepoContext }) => {
100 const [keys, setKeys] = useState<ApiKey[] | null>(null);
101 const [error, setError] = useState<unknown>(null);
102 const [name, setName] = useState("");
103 const [minted, setMinted] = useState<(ApiKey & { key: string }) | null>(null);
104 const { connection, repo } = context;
105 const remote = `${connection.url}/${repo.owner}/${repo.name}.git`;
106
107 const load = () =>
108 listApiKeys(connection)
109 .then((items) => setKeys(items))
110 .catch(setError);
111
112 useEffect(() => {
113 void load();
114 // eslint-disable-next-line react-hooks/exhaustive-deps
115 }, []);
116
117 return (
118 <section className="rounded-md border border-border-muted p-4">
119 <h2 className="mb-1 font-semibold">API keys</h2>
120 <p className="mb-3 text-sm text-fg-muted">
121 A key is the password of your git remote. Keys belong to your account and work on every
122 repository you own.
123 </p>
124 {minted !== null && (
125 <div className="mb-3 space-y-2 rounded-md border border-success/40 bg-success/5 p-3 text-sm">
126 <p className="font-medium text-success">
127 Key “{minted.name}” created — shown exactly once:
128 </p>
129 <div className="flex items-center gap-2">
130 <code className="grow overflow-x-auto rounded bg-canvas-subtle px-2 py-1 font-mono text-xs">
131 git remote add origin {remote.replace("://", `://x:${minted.key}@`)}
132 </code>
133 <CopyButton
134 text={`git remote add origin ${remote.replace("://", `://x:${minted.key}@`)}`}
135 />
136 </div>
137 </div>
138 )}
139 <form
140 className="mb-4 flex flex-wrap items-center gap-2"
141 onSubmit={(event) => {
142 event.preventDefault();
143 createApiKey(connection, name.trim())
144 .then((key) => {
145 setMinted(key);
146 setName("");
147 void load();
148 })
149 .catch(setError);
150 }}
151 >
152 <div className="w-48">
153 <Input value={name} onChange={setName} placeholder="Key name" />
154 </div>
155 <Button kind="primary" type="submit" disabled={name.trim().length === 0}>
156 Generate key
157 </Button>
158 </form>
159 {error != null && <ErrorBox error={error} />}
160 {keys === null ? (
161 <Spinner />
162 ) : keys.length === 0 ? (
163 <p className="text-sm text-fg-muted">No keys.</p>
164 ) : (
165 <ul className="divide-y divide-border-muted">
166 {keys.map((key) => (
167 <li key={key.id} className="flex items-center justify-between gap-4 py-2 text-sm">
168 <div className="flex items-center gap-2">
169 <span className="font-medium">{key.name ?? "unnamed"}</span>
170 {key.start !== null && <Badge>{key.start}…</Badge>}
171 <span className="text-xs text-fg-muted">
172 created {timeAgo(new Date(key.createdAt).getTime())}
173 </span>
174 </div>
175 <Button
176 kind="danger"
177 onClick={() => {
178 deleteApiKey(connection, key.id)
179 .then(() => void load())
180 .catch(setError);
181 }}
182 >
183 Revoke
184 </Button>
185 </li>
186 ))}
187 </ul>
188 )}
189 </section>
190 );
191 };
192
193 const DangerCard = ({ context }: { context: RepoContext }) => {
194 const { navigate } = useRouter();
195 const [confirm, setConfirm] = useState("");
196 const [error, setError] = useState<unknown>(null);
197 const [busy, setBusy] = useState(false);
198 const full = `${context.repo.owner}/${context.repo.name}`;
199 return (
200 <section className="rounded-md border border-danger/40 p-4">
201 <h2 className="mb-2 font-semibold text-danger">Danger zone</h2>
202 <p className="mb-3 text-sm text-fg-muted">
203 Deleting a repository destroys its refs and objects. Type{" "}
204 <code className="font-mono">{full}</code> to confirm.
205 </p>
206 <div className="flex items-center gap-2">
207 <div className="w-64">
208 <Input value={confirm} onChange={setConfirm} placeholder={full} mono />
209 </div>
210 <Button
211 kind="danger"
212 disabled={confirm !== full || busy}
213 onClick={() => {
214 setBusy(true);
215 deleteRepo(context.connection, context.repo.owner, context.repo.name)
216 .then(() => navigate("/"))
217 .catch((cause) => {
218 setError(cause);
219 setBusy(false);
220 });
221 }}
222 >
223 {busy ? "Deleting…" : "Delete this repository"}
224 </Button>
225 </div>
226 {error != null && <ErrorBox error={error} />}
227 </section>
228 );
229 };
230
231 export const SettingsTab = ({ context }: { context: RepoContext }) => (
232 <div className="space-y-4">
233 <StorageCard context={context} />
234 <VisibilityCard context={context} />
235 <ApiKeysCard context={context} />
236 <DangerCard context={context} />
237 </div>
238 );