Open History to see the last commit
1import type { EnvironmentId } from "@t3tools/contracts";
2import { ExternalLinkIcon } from "lucide-react";
3import { useState } from "react";
4
5import { useEnvironmentSettings } from "../../hooks/useSettings";
6import { serverEnvironment } from "../../state/server";
7import { useAtomCommand } from "../../state/use-atom-command";
8import { Button, InlineButton } from "../ui/button";
9import { Input } from "../ui/input";
10import { Label } from "../ui/label";
11
12const DEFAULT_HOST = "github.com";
13
14/** Where to create a token for a host: GitHub's own page, or the same path on an Enterprise host. */
15function newTokenUrl(host: string): string {
16 return `https://${host}/settings/personal-access-tokens/new`;
17}
18
19/**
20 * A GitHub token for one host, kept in the server's secret store. It is used before
21 * `GH_TOKEN` and the `gh` login, so it also works on a server without `gh` installed.
22 * Write-only: the saved token is never shown, only whether one is set.
23 */
24export function GitHubTokenSettings({
25 environmentId,
26 onSaved,
27}: {
28 readonly environmentId: EnvironmentId;
29 readonly onSaved: () => void;
30}) {
31 const tokens = useEnvironmentSettings(environmentId, (settings) => settings.github.tokens);
32 const updateSettings = useAtomCommand(serverEnvironment.updateSettings, {
33 label: "save GitHub token",
34 });
35 const [host, setHost] = useState(DEFAULT_HOST);
36 const [draft, setDraft] = useState("");
37 const [saving, setSaving] = useState(false);
38 const normalizedHost = host.trim().toLowerCase();
39 const isSaved = (tokens[normalizedHost] ?? "").length > 0;
40 const savedHosts = Object.entries(tokens)
41 .filter(([, value]) => value.length > 0)
42 .map(([saved]) => saved);
43
44 const save = async (target: string, token: string) => {
45 setSaving(true);
46 try {
47 const result = await updateSettings({
48 environmentId,
49 input: { patch: { github: { tokens: { [target]: token } } } },
50 });
51 if (result._tag === "Success") {
52 setDraft("");
53 onSaved();
54 }
55 } finally {
56 setSaving(false);
57 }
58 };
59
60 return (
61 <form
62 className="grid gap-3"
63 onSubmit={(event) => {
64 event.preventDefault();
65 if (normalizedHost && draft.trim()) void save(normalizedHost, draft.trim());
66 }}
67 >
68 {/* Locked while saving: a successful save clears the draft, which would drop edits made mid-request. */}
69 <fieldset disabled={saving} className="contents">
70 <p className="max-w-2xl text-xs leading-relaxed text-muted-foreground">
71 A token saved here is used before{" "}
72 <code className="rounded bg-muted px-1 py-px text-2xs">GH_TOKEN</code> and the{" "}
73 <code className="rounded bg-muted px-1 py-px text-2xs">gh</code> login, so GitHub works
74 without the GitHub CLI. Give it read and write access to pull requests and contents.{" "}
75 <InlineButton
76 render={
77 <a
78 href={newTokenUrl(normalizedHost || DEFAULT_HOST)}
79 target="_blank"
80 rel="noreferrer noopener"
81 />
82 }
83 >
84 Create a token
85 <ExternalLinkIcon aria-hidden className="size-3" />
86 </InlineButton>
87 </p>
88 <div className="grid gap-3 sm:grid-cols-[12rem_1fr]">
89 <div className="grid gap-1.5">
90 <Label htmlFor={`github-token-host-${environmentId}`}>Host</Label>
91 <Input
92 id={`github-token-host-${environmentId}`}
93 autoComplete="off"
94 size="sm"
95 placeholder={DEFAULT_HOST}
96 value={host}
97 onChange={(event) => setHost(event.target.value)}
98 />
99 </div>
100 <div className="grid gap-1.5">
101 <Label htmlFor={`github-token-${environmentId}`}>Token</Label>
102 <Input
103 id={`github-token-${environmentId}`}
104 type="password"
105 autoComplete="off"
106 size="sm"
107 placeholder={isSaved ? "Stored secret, enter a new value to replace" : "Not set"}
108 value={draft}
109 onChange={(event) => setDraft(event.target.value)}
110 />
111 </div>
112 </div>
113 <div className="flex items-center justify-between gap-3">
114 <p className="text-xs text-muted-foreground">
115 {savedHosts.length === 0
116 ? "No token saved; the server uses GH_TOKEN or the gh login."
117 : `Saved for ${savedHosts.join(", ")}.`}
118 </p>
119 <div className="flex shrink-0 gap-2">
120 {isSaved ? (
121 <Button
122 size="xs"
123 variant="outline"
124 disabled={saving}
125 onClick={() => void save(normalizedHost, "")}
126 >
127 Remove
128 </Button>
129 ) : null}
130 <Button type="submit" size="xs" disabled={!normalizedHost || !draft.trim() || saving}>
131 Save
132 </Button>
133 </div>
134 </div>
135 </fieldset>
136 </form>
137 );
138}