Skip to content
refs
History
159 lines · 6.8 KBRaw
1import { AgentAvatar, Banner, Button, ClipboardText, Empty, Input, Table } from "@refs/design-system";
2import { LayerCard } from "@refs/design-system";
3import { KeyIcon, PlugsConnectedIcon } from "@refs/design-system/icons";
4import { Form, useNavigation } from "react-router";
5import type { Route } from "./+types/tokens";
6import { AppShell, PageHeader } from "../components/shell";
7import { formatDate } from "@refs/design-system";
8import { failed, formText, signedIn } from "../lib/session";
9
10export const meta: Route.MetaFunction = () => [{ title: "Access tokens · refs" }];
11
12export async function loader({ context }: Route.LoaderArgs) {
13 const me = await signedIn(context).me();
14 const [tokens, grants] = await Promise.all([me.tokens(), me.agentGrants()]);
15 return { tokens, grants };
16}
17
18export async function action({ request, context }: Route.ActionArgs) {
19 const me = await signedIn(context).me();
20 const form = await request.formData();
21
22 switch (formText(form, "intent")) {
23 case "revoke":
24 case "disconnect": {
25 const id = formText(form, "id");
26 const outcome = formText(form, "intent") === "revoke" ? await me.revokeToken(id) : await me.revokeAgentGrant(id);
27 return outcome.ok ? { created: null, error: null } : failed(outcome.problem, { created: null });
28 }
29 case "create": {
30 const outcome = await me.createToken(formText(form, "name"));
31 if (!outcome.ok) return failed(outcome.problem, { created: null });
32 return { created: { name: outcome.value.token.name, token: outcome.value.secret }, error: null };
33 }
34 default:
35 return failed({ code: "invalid", message: "Unknown token action." }, { created: null });
36 }
37}
38
39export default function Tokens({ loaderData, actionData }: Route.ComponentProps) {
40 const navigation = useNavigation();
41 const creating = navigation.state === "submitting" && navigation.formData?.get("intent") === "create";
42 return (
43 <AppShell>
44 <PageHeader title="Access tokens" description="When Git asks for a password, paste a token. Any username works." />
45 <div className="flex max-w-4xl flex-col gap-5">
46 {actionData?.error && <Banner variant="error" description={actionData.error} />}
47 {actionData?.created && (
48 <Banner
49 title={`Copy your new "${actionData.created.name}" token now`}
50 description={
51 <>
52 You won't be able to see it again.
53 <span className="mt-2 block">
54 <ClipboardText text={actionData.created.token} size="base" />
55 </span>
56 </>
57 }
58 />
59 )}
60 <LayerCard className="p-4">
61 <Form key={actionData?.created?.token ?? "form"} method="post" className="flex flex-wrap items-end gap-3">
62 <input type="hidden" name="intent" value="create" />
63 <div className="min-w-60 flex-1">
64 <Input label="Token name" name="name" placeholder="For example, laptop" maxLength={64} required />
65 </div>
66 <Button type="submit" variant="primary" loading={creating}>
67 Generate token
68 </Button>
69 </Form>
70 </LayerCard>
71 {loaderData.tokens.length === 0 ? (
72 <LayerCard>
73 <Empty size="sm" icon={<KeyIcon size={24} />} title="No access tokens" description="Generate one to clone and push over HTTPS." />
74 </LayerCard>
75 ) : (
76 <LayerCard>
77 <Table>
78 <Table.Header>
79 <Table.Row>
80 <Table.Head>Name</Table.Head>
81 <Table.Head>Created</Table.Head>
82 <Table.Head>Last used</Table.Head>
83 <Table.Head />
84 </Table.Row>
85 </Table.Header>
86 <Table.Body>
87 {loaderData.tokens.map((token) => (
88 <Table.Row key={token.id}>
89 <Table.Cell className="font-medium">{token.name}</Table.Cell>
90 <Table.Cell className="text-muted">{formatDate(token.createdAt)}</Table.Cell>
91 <Table.Cell className="text-muted">{formatDate(token.lastUsedAt)}</Table.Cell>
92 <Table.Cell className="text-right">
93 <Form method="post" className="inline-flex">
94 <input type="hidden" name="intent" value="revoke" />
95 <input type="hidden" name="id" value={token.id} />
96 <Button type="submit" variant="secondary-destructive" size="sm">
97 Revoke
98 </Button>
99 </Form>
100 </Table.Cell>
101 </Table.Row>
102 ))}
103 </Table.Body>
104 </Table>
105 </LayerCard>
106 )}
107 <ConnectedAgents grants={loaderData.grants} />
108 </div>
109 </AppShell>
110 );
111}
112
113function ConnectedAgents({ grants }: { grants: Route.ComponentProps["loaderData"]["grants"] }) {
114 return (
115 <section className="flex flex-col gap-3">
116 <h2 className="m-0 text-base font-semibold">Connected agents</h2>
117 <p className="m-0 text-muted">Agent clients you signed in with OAuth, such as Claude Code or Codex. Disconnecting one signs it out and takes your agents offline.</p>
118 {grants.length === 0 ? (
119 <LayerCard>
120 <Empty size="sm" icon={<PlugsConnectedIcon size={24} />} title="No agents connected" description="Sign in from Claude Code or Codex to connect one." />
121 </LayerCard>
122 ) : (
123 <LayerCard>
124 <Table>
125 <Table.Header>
126 <Table.Row>
127 <Table.Head>Client</Table.Head>
128 <Table.Head>Connected</Table.Head>
129 <Table.Head />
130 </Table.Row>
131 </Table.Header>
132 <Table.Body>
133 {grants.map((grant) => (
134 <Table.Row key={grant.id}>
135 <Table.Cell>
136 <span className="flex items-center gap-2.5 font-medium">
137 <AgentAvatar name={grant.client} size="base" />
138 {grant.client}
139 </span>
140 </Table.Cell>
141 <Table.Cell className="text-muted">{formatDate(grant.createdAt)}</Table.Cell>
142 <Table.Cell className="text-right">
143 <Form method="post" className="inline-flex">
144 <input type="hidden" name="intent" value="disconnect" />
145 <input type="hidden" name="id" value={grant.id} />
146 <Button type="submit" variant="secondary-destructive" size="sm">
147 Disconnect
148 </Button>
149 </Form>
150 </Table.Cell>
151 </Table.Row>
152 ))}
153 </Table.Body>
154 </Table>
155 </LayerCard>
156 )}
157 </section>
158 );
159}