pr_01m47d24b0e6n91zwymwxg0vpx/apps/web/app/routes/settings.tsx

181 lines6,227 bytesCodeBlame
1import { identity } from "../lib/services.server";
2import { Form } from "react-router";
3
4import type { Route } from "./+types/settings";
5import { Button, ErrorText, Field, Input, TimeAgo } from "../components/ui";
6import { assertSameOrigin, requireUser } from "../lib/session.server";
7
8export function meta({}: Route.MetaArgs) {
9 return [{ title: "Settings · g1t" }];
10}
11
12export async function loader({ request, context }: Route.LoaderArgs) {
13 const user = requireUser(context, request);
14 const [keys, tokens, applications] = await Promise.all([
15 identity.listSshKeys(user),
16 identity.listAccessTokens(user),
17 identity.listOAuthGrants(user),
18 ]);
19 return { user, keys, tokens, applications };
20}
21
22export async function action({ request, context }: Route.ActionArgs) {
23 assertSameOrigin(request);
24 const user = requireUser(context, request);
25 const form = await request.formData();
26 const id = String(form.get("id") ?? "");
27
28 switch (form.get("intent")) {
29 case "add-key": {
30 const result = await identity.addSshKey(
31 user,
32 String(form.get("title") ?? ""),
33 String(form.get("key") ?? ""),
34 );
35 return result.ok ? null : { keyError: result.error.message };
36 }
37 case "delete-key":
38 await identity.removeSshKey(user, id);
39 return null;
40 case "add-token": {
41 const created = await identity.createAccessToken(
42 user,
43 String(form.get("name") ?? ""),
44 );
45 return { newToken: created.token };
46 }
47 case "delete-token":
48 await identity.removeAccessToken(user, id);
49 return null;
50 case "sign-out-application":
51 await identity.revokeOAuthGrant(user, id);
52 return null;
53 }
54 return null;
55}
56
57function DeleteButton({
58 intent,
59 id,
60 label = "Delete",
61}: {
62 intent: string;
63 id: string;
64 label?: string;
65}) {
66 return (
67 <Form method="post" className="ml-auto">
68 <input type="hidden" name="intent" value={intent} />
69 <input type="hidden" name="id" value={id} />
70 <Button variant="quiet" type="submit">
71 {label}
72 </Button>
73 </Form>
74 );
75}
76
77export default function Settings({
78 loaderData,
79 actionData,
80}: Route.ComponentProps) {
81 const { user, keys, tokens, applications } = loaderData;
82 return (
83 <main className="mx-auto max-w-2xl space-y-12 px-4 py-12">
84 <h1 className="text-xl font-semibold">
85 Settings <span className="font-mono text-muted">{user.username}</span>
86 </h1>
87
88 <section>
89 <h2 className="font-medium">SSH keys</h2>
90 <ul className="mt-4 divide-y divide-line rounded-md border border-line empty:hidden">
91 {keys.map((key) => (
92 <li key={key.id} className="flex items-center gap-4 px-4 py-3">
93 <div className="min-w-0">
94 <p className="text-sm">{key.title}</p>
95 <p className="truncate font-mono text-xs text-muted">
96 {key.fingerprint}
97 </p>
98 </div>
99 <DeleteButton intent="delete-key" id={key.id} />
100 </li>
101 ))}
102 </ul>
103 <Form method="post" className="mt-4 space-y-3">
104 <input type="hidden" name="intent" value="add-key" />
105 <Field label="Title (optional)">
106 <Input name="title" maxLength={100} />
107 </Field>
108 <Field label="Public key">
109 <Input name="key" placeholder="ssh-ed25519 AAAA…" required />
110 </Field>
111 <ErrorText>{actionData?.keyError}</ErrorText>
112 <Button type="submit">Add SSH key</Button>
113 </Form>
114 </section>
115
116 <section>
117 <h2 className="font-medium">Access tokens</h2>
118 <p className="mt-1 text-sm text-muted">
119 Use a token as the password when git asks for one over HTTPS, and to
120 authenticate agents and the API.
121 </p>
122 {actionData?.newToken && (
123 <div className="mt-4 rounded-md border border-accent/40 bg-surface p-4">
124 <p className="text-sm">Copy it now. It will not be shown again.</p>
125 <pre className="mt-2 overflow-x-auto font-mono text-sm text-accent">
126 {actionData.newToken}
127 </pre>
128 </div>
129 )}
130 <ul className="mt-4 divide-y divide-line rounded-md border border-line empty:hidden">
131 {tokens.map((token) => (
132 <li key={token.id} className="flex items-center gap-4 px-4 py-3">
133 <p className="text-sm">{token.name}</p>
134 <DeleteButton intent="delete-token" id={token.id} />
135 </li>
136 ))}
137 </ul>
138 <Form method="post" className="mt-4 flex items-end gap-3">
139 <input type="hidden" name="intent" value="add-token" />
140 <div className="grow">
141 <Field label="Name">
142 <Input name="name" maxLength={100} placeholder="laptop" />
143 </Field>
144 </div>
145 <Button type="submit">Create token</Button>
146 </Form>
147 </section>
148
149 <section>
150 <h2 className="font-medium">Connected applications</h2>
151 <p className="mt-1 text-sm text-muted">
152 Applications you signed in to through your browser, such as an agent
153 connected to the g1t MCP server. Signing one out ends its access at
154 once.
155 </p>
156 {applications.length === 0 ? (
157 <p className="mt-4 text-sm text-faint">None yet.</p>
158 ) : (
159 <ul className="mt-4 divide-y divide-line rounded-md border border-line">
160 {applications.map((application) => (
161 <li key={application.id} className="flex items-center gap-4 px-4 py-3">
162 <div>
163 <p className="text-sm">{application.clientName}</p>
164 <p className="text-xs text-faint">
165 Connected <TimeAgo at={application.createdAt} /> · last used{" "}
166 <TimeAgo at={application.lastUsedAt} />
167 </p>
168 </div>
169 <DeleteButton
170 intent="sign-out-application"
171 id={application.id}
172 label="Sign out"
173 />
174 </li>
175 ))}
176 </ul>
177 )}
178 </section>
179 </main>
180 );
181}