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 try {
43 const result = await updateSettings({
44 environmentId,
45 // The map travels whole: every other saved token as its redaction marker, which keeps it.
46 input: {
47 patch: { sourceControlHosts: { github: { tokens: { ...tokens, [target]: token } } } },
48 },
49 });
50 if (result._tag === "Success") {
51 setDraft("");
52 onSaved();
53 }
54 } finally {
55 setSaving(false);
56 }
57 };
58
59 return (
60 <form
61 className="grid gap-3"
62 onSubmit={(event) => {
63 event.preventDefault();
64 if (normalizedHost && draft.trim()) void save(normalizedHost, draft.trim());
65 }}
66 >
67 {/* Locked while saving: a successful save clears the draft, which would drop edits made mid-request. */}
68 <fieldset disabled={saving} className="contents">
69 <p className="max-w-2xl text-xs leading-relaxed text-muted-foreground">
70 A token saved here is used before{" "}
71 <code className="rounded bg-muted px-1 py-px text-2xs">GH_TOKEN</code> and the{" "}
72 <code className="rounded bg-muted px-1 py-px text-2xs">gh</code> login, so GitHub works
73 without the GitHub CLI. Give it read and write access to pull requests and contents.{" "}
74 <InlineButton
75 render={
76 <a
77 href={newTokenUrl(normalizedHost || DEFAULT_HOST)}
78 target="_blank"
79 rel="noreferrer noopener"
80 />
81 }
82 >
83 Create a token
84 <ExternalLinkIcon aria-hidden className="size-3" />
85 </InlineButton>
86 </p>
87 <div className="grid gap-3 sm:grid-cols-[12rem_1fr]">
88 <div className="grid gap-1.5">
89 <Label htmlFor={`github-token-host-${environmentId}`}>Host</Label>
90 <Input
91 id={`github-token-host-${environmentId}`}
92 autoComplete="off"
93 size="sm"
94 placeholder={DEFAULT_HOST}
95 value={host}
96 onChange={(event) => setHost(event.target.value)}
97 />
98 </div>
99 <div className="grid gap-1.5">
100 <Label htmlFor={`github-token-${environmentId}`}>Token</Label>
101 <SecretInput
102 id={`github-token-${environmentId}`}
103 size="sm"
104 configured={isSaved}
105 value={draft}
106 onChange={(event) => setDraft(event.target.value)}
107 />
108 </div>
109 </div>
110 <div className="flex items-center justify-between gap-3">
111 <p className="text-xs text-muted-foreground">
112 {savedHosts.length === 0
113 ? "No token saved; the server uses GH_TOKEN or the gh login."
114 : `Saved for ${savedHosts.join(", ")}.`}
115 </p>
116 <div className="flex shrink-0 gap-2">
117 {isSaved ? (
118 <Button
119 size="xs"
120 variant="outline"
121 disabled={saving}
122 onClick={() => void save(normalizedHost, "")}
123 >
124 Remove
125 </Button>
126 ) : null}
127 <Button type="submit" size="xs" disabled={!normalizedHost || !draft.trim() || saving}>
128 Save
129 </Button>
130 </div>
131 </div>
132 </fieldset>
133 </form>
134 );
135}