The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { |
| 2 | isSecretReference, |
| 3 | type EnvironmentId, |
| 4 | type SecretReference, |
| 5 | type SettingsSecretValue, |
| 6 | } from "@t3tools/contracts"; |
| 7 | import { T, useGT, Var } from "gt-react"; |
| 8 | import { ExternalLinkIcon } from "lucide-react"; |
| 9 | import { useState } from "react"; |
| 10 | |
| 11 | import { useEnvironmentSettings } from "../../hooks/useSettings"; |
| 12 | import { |
| 13 | readHostCredentialSettings, |
| 14 | type SourceControlDefinition, |
| 15 | type SourceControlHostCredentials, |
| 16 | } from "@t3tools/client-runtime/source-control-clients"; |
| 17 | import { serverEnvironment } from "../../state/server"; |
| 18 | import { useAtomCommand } from "../../state/use-atom-command"; |
| 19 | import { Button, InlineButton } from "../ui/button"; |
| 20 | import { Input } from "../ui/input"; |
| 21 | import { Label } from "../ui/label"; |
| 22 | import { SecretSourceInput, useSecretSources } from "./SecretSourceInput"; |
| 23 | |
| 24 | const hasToken = (token: SettingsSecretValue | undefined) => |
| 25 | typeof token === "string" ? token.length > 0 : token !== undefined; |
| 26 | |
| 27 | export function SourceControlTokenSettings({ |
| 28 | environmentId, |
| 29 | definition, |
| 30 | credentials, |
| 31 | onSaved, |
| 32 | }: { |
| 33 | readonly environmentId: EnvironmentId; |
| 34 | readonly definition: SourceControlDefinition; |
| 35 | readonly credentials: SourceControlHostCredentials; |
| 36 | readonly onSaved: () => void; |
| 37 | }) { |
| 38 | const gt = useGT(); |
| 39 | // Translates the GitHub definition's English `tokenAccess`. |
| 40 | const tokenAccess = |
| 41 | definition.kind === "github" |
| 42 | ? gt("read and write access to pull requests and contents") |
| 43 | : credentials.tokenAccess; |
| 44 | const { tokens } = useEnvironmentSettings(environmentId, (settings) => |
| 45 | readHostCredentialSettings(settings, definition.kind), |
| 46 | ); |
| 47 | const updateSettings = useAtomCommand(serverEnvironment.updateSettings, { |
| 48 | label: `save ${definition.label} token`, |
| 49 | }); |
| 50 | const defaultHost = definition.publicHost ?? ""; |
| 51 | const [host, setHost] = useState(defaultHost); |
| 52 | const secretSources = useSecretSources(environmentId); |
| 53 | const [pending, setPending] = useState<{ |
| 54 | readonly key: string; |
| 55 | readonly draft: string | SecretReference; |
| 56 | } | null>(null); |
| 57 | const [saving, setSaving] = useState(false); |
| 58 | const normalizedHost = host.trim().toLowerCase(); |
| 59 | const savedToken = tokens[normalizedHost]; |
| 60 | const isSaved = hasToken(savedToken); |
| 61 | const savedHosts = Object.entries(tokens) |
| 62 | .filter(([, value]) => hasToken(value)) |
| 63 | .map(([saved]) => saved); |
| 64 | const sourceKey = JSON.stringify([normalizedHost, savedToken ?? ""]); |
| 65 | const draft = |
| 66 | pending?.key === sourceKey ? pending.draft : isSecretReference(savedToken) ? savedToken : ""; |
| 67 | const submission = typeof draft === "string" ? draft.trim() : draft; |
| 68 | |
| 69 | const save = async (target: string, token: SettingsSecretValue) => { |
| 70 | setSaving(true); |
| 71 | const result = await updateSettings({ |
| 72 | environmentId, |
| 73 | // Send the whole map; other tokens' redaction markers preserve their values. |
| 74 | input: { |
| 75 | patch: { |
| 76 | sourceControlHosts: { [definition.kind]: { tokens: { ...tokens, [target]: token } } }, |
| 77 | }, |
| 78 | }, |
| 79 | }); |
| 80 | setSaving(false); |
| 81 | if (result._tag === "Success") { |
| 82 | setPending(null); |
| 83 | onSaved(); |
| 84 | } |
| 85 | }; |
| 86 | |
| 87 | return ( |
| 88 | <form |
| 89 | className="grid gap-3" |
| 90 | onSubmit={(event) => { |
| 91 | event.preventDefault(); |
| 92 | if (normalizedHost && submission !== "") void save(normalizedHost, submission); |
| 93 | }} |
| 94 | > |
| 95 | {/* Locked while saving: a successful save clears the draft, which would drop edits made mid-request. */} |
| 96 | <fieldset disabled={saving} className="contents"> |
| 97 | <p className="max-w-2xl text-xs leading-relaxed text-muted-foreground"> |
| 98 | <T> |
| 99 | A token saved here is used before{" "} |
| 100 | <code className="rounded bg-muted px-1 py-px text-2xs"> |
| 101 | <Var>{credentials.tokenEnvVar}</Var> |
| 102 | </code>{" "} |
| 103 | and the{" "} |
| 104 | <code className="rounded bg-muted px-1 py-px text-2xs"> |
| 105 | <Var>{credentials.cliName}</Var> |
| 106 | </code>{" "} |
| 107 | login, so <Var>{definition.label}</Var> works without the{" "} |
| 108 | <Var>{credentials.cliLabel}</Var>. Give it <Var>{tokenAccess}</Var>. |
| 109 | </T>{" "} |
| 110 | {normalizedHost || defaultHost ? ( |
| 111 | <InlineButton |
| 112 | render={ |
| 113 | <a |
| 114 | href={credentials.newTokenUrl(normalizedHost || defaultHost)} |
| 115 | target="_blank" |
| 116 | rel="noreferrer noopener" |
| 117 | /> |
| 118 | } |
| 119 | > |
| 120 | <T>Create a token</T> |
| 121 | <ExternalLinkIcon aria-hidden className="size-3" /> |
| 122 | </InlineButton> |
| 123 | ) : null} |
| 124 | </p> |
| 125 | <div className="grid gap-3 sm:grid-cols-[12rem_1fr]"> |
| 126 | <div className="grid gap-1.5"> |
| 127 | <Label htmlFor={`${definition.kind}-token-host-${environmentId}`}> |
| 128 | <T>Host</T> |
| 129 | </Label> |
| 130 | <Input |
| 131 | id={`${definition.kind}-token-host-${environmentId}`} |
| 132 | autoComplete="off" |
| 133 | size="sm" |
| 134 | placeholder={defaultHost} |
| 135 | value={host} |
| 136 | onChange={(event) => setHost(event.target.value)} |
| 137 | /> |
| 138 | </div> |
| 139 | <div className="grid gap-1.5"> |
| 140 | <Label htmlFor={`${definition.kind}-token-${environmentId}`}> |
| 141 | <T>Token</T> |
| 142 | </Label> |
| 143 | <SecretSourceInput |
| 144 | id={`${definition.kind}-token-${environmentId}`} |
| 145 | size="sm" |
| 146 | aria-label={gt("{host} token", { host: definition.label })} |
| 147 | environmentId={environmentId} |
| 148 | configured={isSaved && !isSecretReference(savedToken)} |
| 149 | value={draft} |
| 150 | sources={secretSources} |
| 151 | onChange={(event) => setPending({ key: sourceKey, draft: event.target.value })} |
| 152 | onSecretChange={(secret) => setPending({ key: sourceKey, draft: secret ?? "" })} |
| 153 | /> |
| 154 | </div> |
| 155 | </div> |
| 156 | <div className="flex items-center justify-between gap-3"> |
| 157 | <p className="text-xs text-muted-foreground"> |
| 158 | {savedHosts.length === 0 |
| 159 | ? gt("No token saved; the server uses {envVar} or the {cliName} login.", { |
| 160 | envVar: credentials.tokenEnvVar, |
| 161 | cliName: credentials.cliName, |
| 162 | }) |
| 163 | : gt("Saved for {hosts}.", { hosts: savedHosts.join(", ") })} |
| 164 | </p> |
| 165 | <div className="flex shrink-0 gap-2"> |
| 166 | {isSaved ? ( |
| 167 | <Button |
| 168 | size="xs" |
| 169 | variant="outline" |
| 170 | disabled={saving} |
| 171 | onClick={() => void save(normalizedHost, "")} |
| 172 | > |
| 173 | <T>Remove</T> |
| 174 | </Button> |
| 175 | ) : null} |
| 176 | <Button |
| 177 | type="submit" |
| 178 | size="xs" |
| 179 | disabled={!normalizedHost || submission === "" || saving} |
| 180 | > |
| 181 | <T>Save</T> |
| 182 | </Button> |
| 183 | </div> |
| 184 | </div> |
| 185 | </fieldset> |
| 186 | </form> |
| 187 | ); |
| 188 | } |