flagon-io/g1t

public

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

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

260 lines9,757 bytesCodeBlame
1import { identity } from "../lib/services.server";
2import { Form, Link } from "react-router";
3
4import type { Route } from "./+types/settings";
5import { page } from "../lib/meta";
6import { AvatarField } from "../components/avatar-field";
7import { Button, ErrorText, Field, Input, TimeAgo } from "../components/ui";
8import { readAvatarUpload } from "../lib/avatar-upload";
9import { assertSameOrigin, requireUser } from "../lib/session.server";
10import { ProfileSection } from "../components/profile-form";
11
12export function meta(args: Route.MetaArgs) {
13 return page(args, { title: "Settings · g1t" });
14}
15
16export async function loader({ request, context }: Route.LoaderArgs) {
17 const user = requireUser(context, request);
18 const [keys, tokens, applications, profile] = await Promise.all([
19 identity.listSshKeys(user),
20 identity.listAccessTokens(user),
21 identity.listOAuthGrants(user),
22 identity.profile(user.username),
23 ]);
24 return { user, keys, tokens, applications, profile };
25}
26
27export async function action({ request, context }: Route.ActionArgs) {
28 assertSameOrigin(request);
29 const user = requireUser(context, request);
30 const form = await request.formData();
31 const id = String(form.get("id") ?? "");
32
33 switch (form.get("intent")) {
34 // Identity checks every field again, and the website most of all.
35 case "profile": {
36 const text = (name: string) => String(form.get(name) ?? "");
37 const result = await identity.updateProfile(user, {
38 name: text("name"),
39 bio: text("bio"),
40 location: text("location"),
41 website: text("website"),
42 pronouns: text("pronouns"),
43 });
44 return result.ok ? { profileSaved: true } : { profileError: result.error.message };
45 }
46 // The picture: identity checks the image's bytes again.
47 case "avatar": {
48 const upload = await readAvatarUpload(form);
49 if ("error" in upload) return { avatarError: upload.error };
50 const result = await identity.setUserAvatar(user, upload.image);
51 return result.ok ? null : { avatarError: result.error.message };
52 }
53 case "remove-avatar": {
54 const result = await identity.setUserAvatar(user, null);
55 return result.ok ? null : { avatarError: result.error.message };
56 }
57 case "add-key": {
58 const result = await identity.addSshKey(
59 user,
60 String(form.get("title") ?? ""),
61 String(form.get("key") ?? ""),
62 );
63 return result.ok ? null : { keyError: result.error.message };
64 }
65 case "delete-key":
66 await identity.removeSshKey(user, id);
67 return null;
68 case "add-token": {
69 const created = await identity.createAccessToken(
70 user,
71 String(form.get("label") ?? ""),
72 );
73 return { newToken: created.token };
74 }
75 case "delete-token":
76 await identity.removeAccessToken(user, id);
77 return null;
78 case "sign-out-application":
79 await identity.revokeOAuthGrant(user, id);
80 return null;
81 }
82 return null;
83}
84
85function DeleteButton({
86 intent,
87 id,
88 label = "Delete",
89}: {
90 intent: string;
91 id: string;
92 label?: string;
93}) {
94 return (
95 <Form method="post" className="ml-auto">
96 <input type="hidden" name="intent" value={intent} />
97 <input type="hidden" name="id" value={id} />
98 <Button variant="quiet" type="submit">
99 {label}
100 </Button>
101 </Form>
102 );
103}
104
105export default function Settings({
106 loaderData,
107 actionData,
108}: Route.ComponentProps) {
109 const { user, keys, tokens, applications, profile } = loaderData;
110 return (
111 <main className="mx-auto max-w-5xl px-4 py-10 sm:px-8">
112 <header className="mb-8 border-b border-line pb-6">
113 <h1 className="text-2xl font-semibold tracking-tight">Account</h1>
114 <p className="mt-1.5 text-sm text-muted">
115 How <span className="font-mono text-fg">{user.username}</span> signs in from git, tools and agents.
116 </p>
117 </header>
118 <div className="max-w-2xl space-y-12">
119 <section id="picture" className="scroll-mt-20">
120 <h2 className="font-medium">Picture</h2>
121 <div className="mt-4">
122 <AvatarField
123 name={user.username}
124 image={user.avatar}
125 error={actionData && "avatarError" in actionData ? actionData.avatarError : undefined}
126 about="Shown beside your username in the sidebar and on mission control. Without one, g1t draws your first letter."
127 />
128 </div>
129 </section>
130 <ProfileSection
131 username={user.username}
132 profile={profile}
133 error={actionData && "profileError" in actionData ? actionData.profileError : undefined}
134 saved={Boolean(actionData && "profileSaved" in actionData)}
135 />
136 <section id="ssh-keys" className="scroll-mt-20">
137 <h2 className="font-medium">SSH keys</h2>
138 <ul className="mt-4 divide-y divide-line rounded-md border border-line empty:hidden">
139 {keys.map((key) => (
140 <li key={key.id} className="flex items-center gap-4 px-4 py-3">
141 <div className="min-w-0">
142 <p className="text-sm">{key.title}</p>
143 <p className="truncate font-mono text-xs text-muted">
144 {key.fingerprint}
145 </p>
146 </div>
147 <DeleteButton intent="delete-key" id={key.id} />
148 </li>
149 ))}
150 </ul>
151 <Form method="post" className="mt-4 space-y-3">
152 <input type="hidden" name="intent" value="add-key" />
153 <Field label="Title (optional)">
154 <Input name="title" maxLength={100} />
155 </Field>
156 <Field label="Public key">
157 <Input name="key" placeholder="ssh-ed25519 AAAA…" required />
158 </Field>
159 <ErrorText>{actionData?.keyError}</ErrorText>
160 <Button type="submit">Add SSH key</Button>
161 </Form>
162 </section>
163
164 <section id="tokens" className="scroll-mt-20">
165 <h2 className="font-medium">Access tokens</h2>
166 <p className="mt-1 text-sm text-muted">
167 Use a token as the password when git asks for one over HTTPS, and to
168 authenticate agents and the API. A token here acts as you.
169 </p>
170 {(user.workspaces ?? []).length > 0 && (
171 <p className="mt-1 text-sm text-muted">
172 For CI and integrations that work for a team, use a workspace's
173 own tokens instead:{" "}
174 {(user.workspaces ?? []).map((membership, i) => (
175 <span key={membership.slug}>
176 {i > 0 && ", "}
177 <Link
178 to={`/${membership.slug}/-/tokens`}
179 className="font-mono text-fg underline underline-offset-4"
180 >
181 {membership.slug}
182 </Link>
183 </span>
184 ))}
185 .
186 </p>
187 )}
188 {actionData?.newToken && (
189 <div className="mt-4 rounded-md border border-accent/40 bg-surface p-4">
190 <p className="text-sm">Copy it now. It will not be shown again.</p>
191 <pre className="mt-2 overflow-x-auto font-mono text-sm text-accent">
192 {actionData.newToken}
193 </pre>
194 </div>
195 )}
196 <ul className="mt-4 divide-y divide-line rounded-md border border-line empty:hidden">
197 {tokens.map((token) => (
198 <li key={token.id} className="flex items-center gap-4 px-4 py-3">
199 <div className="min-w-0">
200 <p className="truncate text-sm">{token.name}</p>
201 <p className="text-xs text-faint">
202 Created <TimeAgo at={token.createdAt} /> ·{" "}
203 {token.lastUsedAt ? (
204 <>
205 last used <TimeAgo at={token.lastUsedAt} />
206 </>
207 ) : (
208 "never used"
209 )}
210 </p>
211 </div>
212 <DeleteButton intent="delete-token" id={token.id} />
213 </li>
214 ))}
215 </ul>
216 <Form method="post" className="mt-4 flex items-end gap-3">
217 <input type="hidden" name="intent" value="add-token" />
218 <div className="grow">
219 <Field label="Name">
220 <Input name="label" maxLength={100} placeholder="laptop" />
221 </Field>
222 </div>
223 <Button type="submit">Create token</Button>
224 </Form>
225 </section>
226
227 <section id="applications" className="scroll-mt-20">
228 <h2 className="font-medium">Connected applications</h2>
229 <p className="mt-1 text-sm text-muted">
230 Applications you signed in to through your browser, such as an agent
231 connected to the g1t MCP server. Signing one out ends its access at
232 once.
233 </p>
234 {applications.length === 0 ? (
235 <p className="mt-4 text-sm text-faint">None yet.</p>
236 ) : (
237 <ul className="mt-4 divide-y divide-line rounded-md border border-line">
238 {applications.map((application) => (
239 <li key={application.id} className="flex items-center gap-4 px-4 py-3">
240 <div>
241 <p className="text-sm">{application.clientName}</p>
242 <p className="text-xs text-faint">
243 Connected <TimeAgo at={application.createdAt} /> · last used{" "}
244 <TimeAgo at={application.lastUsedAt} />
245 </p>
246 </div>
247 <DeleteButton
248 intent="sign-out-application"
249 id={application.id}
250 label="Sign out"
251 />
252 </li>
253 ))}
254 </ul>
255 )}
256 </section>
257 </div>
258 </main>
259 );
260}