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