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