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 { useAtomValue } from "@effect/atom-react"; |
| 2 | import { |
| 3 | linkBackBlockLabel, |
| 4 | linkBatchFailureLabel, |
| 5 | linkWithMintedCode, |
| 6 | } from "@t3tools/client-runtime/link-batch"; |
| 7 | import { ANOTHER_ADDRESS, linkRequestFailureMessage } from "@t3tools/client-runtime/link-request"; |
| 8 | import { |
| 9 | AuthAccessWriteScope, |
| 10 | type AuthMcpClientAccess, |
| 11 | type EnvironmentId, |
| 12 | type PeerLinkSummary, |
| 13 | } from "@t3tools/contracts"; |
| 14 | import { |
| 15 | isAtomCommandInterrupted, |
| 16 | squashAtomCommandFailure, |
| 17 | } from "@t3tools/client-runtime/state/runtime"; |
| 18 | import { peerLinkHealth, type PeerLinkHealth } from "@t3tools/client-runtime/state/peerLinks"; |
| 19 | import * as DateTime from "effect/DateTime"; |
| 20 | import * as Option from "effect/Option"; |
| 21 | import * as Result from "effect/Result"; |
| 22 | import { PlusIcon } from "lucide-react"; |
| 23 | import { T, useGT, useMessages, Var } from "gt-react"; |
| 24 | import { type ReactNode, useId, useRef, useState } from "react"; |
| 25 | |
| 26 | import { requestConfirmDialog } from "~/confirmDialog"; |
| 27 | import { useEnvironment } from "~/state/environments"; |
| 28 | import { mintPairingCodeOn } from "~/state/peerPairingCode"; |
| 29 | import { peerLinkEnvironment, useLinkableMachines, useLinkBackOffers } from "~/state/peerLinks"; |
| 30 | import { useEnvironments } from "~/state/environments"; |
| 31 | import { useEnvironmentQuery } from "~/state/query"; |
| 32 | import { useEnvironmentScope, usePreparedConnection } from "~/state/session"; |
| 33 | import { useAtomCommand } from "~/state/use-atom-command"; |
| 34 | import { accessConfig } from "../auth/ConnectAgentSurface"; |
| 35 | import { ConnectionStatusDot } from "../ConnectionStatusDot"; |
| 36 | import { Button } from "../ui/button"; |
| 37 | import { Checkbox } from "../ui/checkbox"; |
| 38 | import { |
| 39 | Dialog, |
| 40 | DialogClose, |
| 41 | DialogDescription, |
| 42 | DialogFooter, |
| 43 | DialogHeader, |
| 44 | DialogPanel, |
| 45 | DialogPopup, |
| 46 | DialogTitle, |
| 47 | DialogTrigger, |
| 48 | } from "../ui/dialog"; |
| 49 | import { Input } from "../ui/input"; |
| 50 | import { stackedThreadToast, toastManager } from "../ui/toast"; |
| 51 | import { ITEM_ROW_CLASSNAME, ITEM_ROW_INNER_CLASSNAME } from "./itemRows"; |
| 52 | import { LinkAccessPicker } from "./LinkAccessPicker"; |
| 53 | import { LinkAddressWarning, LinkMachineSelect } from "./LinkMachineSelect"; |
| 54 | import { LinkSeveralEnvironmentsAction } from "./LinkSeveralEnvironments"; |
| 55 | import { SettingsRow, SettingsSection } from "./settingsLayout"; |
| 56 | import { useLiveRefresh } from "../../hooks/useLiveRefresh"; |
| 57 | import { m as resolveMessage } from "../../i18n/messages"; |
| 58 | import { useNowMinute } from "../../hooks/useNowMinute"; |
| 59 | import { useSearchableSetting } from "./settingsSearch"; |
| 60 | |
| 61 | // Console log labels for the atom commands. |
| 62 | const UNLINK_COMMAND_LABEL = "unlink environment"; |
| 63 | const LINK_COMMAND_LABEL = "link environment"; |
| 64 | |
| 65 | const failureToast = (title: string, description: string) => |
| 66 | toastManager.add(stackedThreadToast({ type: "error", title, description })); |
| 67 | |
| 68 | export function LinkedEnvironmentsSettings({ |
| 69 | environmentId, |
| 70 | environmentLabel, |
| 71 | isSearchTarget, |
| 72 | }: { |
| 73 | readonly environmentId: EnvironmentId; |
| 74 | readonly environmentLabel: string | null; |
| 75 | readonly isSearchTarget: boolean; |
| 76 | }) { |
| 77 | const searchableSetting = useSearchableSetting(); |
| 78 | const gt = useGT(); |
| 79 | const links = useEnvironmentQuery( |
| 80 | peerLinkEnvironment.list({ environmentId, input: {} }), |
| 81 | useMessages(), |
| 82 | ); |
| 83 | // Links made outside this client bump nothing the shared list reads, so showing the panel |
| 84 | // asks again. Listing probes every linked environment, too costly for a timer nobody asked for. |
| 85 | // A list still loading counts as read now, so the next return to the window asks again. |
| 86 | useLiveRefresh( |
| 87 | () => { |
| 88 | if (!links.isPending) links.refresh(); |
| 89 | }, |
| 90 | { |
| 91 | key: `linked-environments:${environmentId}`, |
| 92 | intervalMs: null, |
| 93 | refreshOnShow: true, |
| 94 | }, |
| 95 | ); |
| 96 | const canLink = useAtomValue(peerLinkEnvironment.link.permissionAtom(environmentId)); |
| 97 | const search = searchableSetting("linked-environments"); |
| 98 | // A batch needs at least two other machines this app has saved. |
| 99 | const otherMachines = useEnvironments().environments.length - 1; |
| 100 | return ( |
| 101 | <SettingsSection |
| 102 | {...(isSearchTarget ? { id: search.id } : {})} |
| 103 | title={environmentLabel === null ? search.title : `${search.title} · ${environmentLabel}`} |
| 104 | headerAction={ |
| 105 | canLink ? ( |
| 106 | <div className="flex items-center gap-2"> |
| 107 | {otherMachines > 1 ? ( |
| 108 | <LinkSeveralEnvironmentsAction environmentId={environmentId} /> |
| 109 | ) : null} |
| 110 | <LinkEnvironmentAction environmentId={environmentId} /> |
| 111 | </div> |
| 112 | ) : null |
| 113 | } |
| 114 | > |
| 115 | {links.error ? ( |
| 116 | <SettingsRow title={gt("Could not load linked environments")} description={links.error} /> |
| 117 | ) : !links.data ? ( |
| 118 | <SettingsRow title={gt("Loading linked environments…")} role="status" /> |
| 119 | ) : links.data.links.length === 0 ? ( |
| 120 | <SettingsRow |
| 121 | title={gt("No linked environments")} |
| 122 | description={gt( |
| 123 | "Link another machine running T3 Code, such as a VPS, so agents here can launch and drive threads there.", |
| 124 | )} |
| 125 | /> |
| 126 | ) : ( |
| 127 | links.data.links.map((link) => ( |
| 128 | <LinkedEnvironmentRow |
| 129 | key={link.environmentId} |
| 130 | environmentId={environmentId} |
| 131 | link={link} |
| 132 | /> |
| 133 | )) |
| 134 | )} |
| 135 | </SettingsSection> |
| 136 | ); |
| 137 | } |
| 138 | |
| 139 | function LinkedEnvironmentRow({ |
| 140 | environmentId, |
| 141 | link, |
| 142 | }: { |
| 143 | readonly environmentId: EnvironmentId; |
| 144 | readonly link: PeerLinkSummary; |
| 145 | }) { |
| 146 | const gt = useGT(); |
| 147 | const m = useMessages(); |
| 148 | const nowMinute = useNowMinute(); |
| 149 | const health = peerLinkHealth(link, DateTime.makeUnsafe(nowMinute)); |
| 150 | const canUnlink = useAtomValue(peerLinkEnvironment.unlink.permissionAtom(environmentId)); |
| 151 | const { busy, forget } = useForgetPeerLink(environmentId, link); |
| 152 | const status = peerLinkStatusText(health, gt); |
| 153 | return ( |
| 154 | <div className={ITEM_ROW_CLASSNAME}> |
| 155 | <div className={ITEM_ROW_INNER_CLASSNAME}> |
| 156 | <div className="min-w-0 flex-1 space-y-1"> |
| 157 | <div className="flex min-h-5 items-center gap-1.5"> |
| 158 | <ConnectionStatusDot |
| 159 | tooltipText={status} |
| 160 | dotClassName={ |
| 161 | health.kind === "reachable" |
| 162 | ? health.expiresInDays === null |
| 163 | ? "bg-success" |
| 164 | : "bg-warning" |
| 165 | : health.kind === "expired" || health.kind === "failing" |
| 166 | ? "bg-destructive" |
| 167 | : "bg-muted-foreground/30" |
| 168 | } |
| 169 | pingClassName={null} |
| 170 | /> |
| 171 | <h3 className="text-sm font-medium text-foreground">{link.label}</h3> |
| 172 | </div> |
| 173 | <p className="text-xs text-muted-foreground"> |
| 174 | {m(accessConfig[link.access].label)} · {status} |
| 175 | </p> |
| 176 | <p className="truncate text-xs text-muted-foreground/80">{link.urls.join(", ")}</p> |
| 177 | </div> |
| 178 | {canUnlink ? ( |
| 179 | <div className="flex w-full shrink-0 items-center gap-2 sm:w-auto sm:justify-end"> |
| 180 | <Button |
| 181 | size="xs" |
| 182 | variant="destructive-outline" |
| 183 | disabled={busy} |
| 184 | onClick={() => void forget()} |
| 185 | > |
| 186 | {busy ? gt("Forgetting…") : gt("Forget")} |
| 187 | </Button> |
| 188 | </div> |
| 189 | ) : null} |
| 190 | </div> |
| 191 | </div> |
| 192 | ); |
| 193 | } |
| 194 | |
| 195 | function LinkEnvironmentAction({ environmentId }: { readonly environmentId: EnvironmentId }) { |
| 196 | const gt = useGT(); |
| 197 | const [open, setOpen] = useState(false); |
| 198 | return ( |
| 199 | <LinkEnvironmentDialog |
| 200 | environmentId={environmentId} |
| 201 | open={open} |
| 202 | onOpenChange={setOpen} |
| 203 | trigger={ |
| 204 | <DialogTrigger |
| 205 | render={ |
| 206 | <Button size="xs" variant="default"> |
| 207 | <PlusIcon className="size-3" /> |
| 208 | {gt("Link", { $context: "verb, links an environment" })} |
| 209 | </Button> |
| 210 | } |
| 211 | /> |
| 212 | } |
| 213 | /> |
| 214 | ); |
| 215 | } |
| 216 | |
| 217 | /** What the link dialog opens with when something other than its own button opened it. */ |
| 218 | export interface LinkEnvironmentPrefill { |
| 219 | /** The machine to link to, by environment id, or `ANOTHER_ADDRESS` with `address`. */ |
| 220 | readonly choice: string | null; |
| 221 | readonly address?: string; |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * The one link flow. When this client is connected to the picked machine and may create pairing |
| 226 | * links there, it mints the code itself; otherwise it asks for one. |
| 227 | */ |
| 228 | export function LinkEnvironmentDialog({ |
| 229 | environmentId, |
| 230 | open, |
| 231 | onOpenChange, |
| 232 | trigger = null, |
| 233 | prefill, |
| 234 | }: { |
| 235 | readonly environmentId: EnvironmentId; |
| 236 | readonly open: boolean; |
| 237 | readonly onOpenChange: (open: boolean) => void; |
| 238 | readonly trigger?: ReactNode; |
| 239 | readonly prefill?: LinkEnvironmentPrefill; |
| 240 | }) { |
| 241 | const gt = useGT(); |
| 242 | const m = useMessages(); |
| 243 | const setOpen = onOpenChange; |
| 244 | // Named in the title: from the map, nothing else on screen says which machine is linking. |
| 245 | const source = useEnvironment(environmentId); |
| 246 | const machines = useLinkableMachines(environmentId, open); |
| 247 | // Null until the user picks; with no other machines known, only an address is left. |
| 248 | const [choice, setChoice] = useState<string | null>(prefill?.choice ?? null); |
| 249 | const [address, setAddress] = useState(prefill?.address ?? ""); |
| 250 | const [pairingCode, setPairingCode] = useState(""); |
| 251 | const [access, setAccess] = useState<AuthMcpClientAccess>("approval-required"); |
| 252 | const [busy, setBusy] = useState(false); |
| 253 | // Machines whose minting already failed, so the dialog asks for a code instead. |
| 254 | const [mintFailed, setMintFailed] = useState<ReadonlySet<EnvironmentId>>(new Set()); |
| 255 | // Enter then a click can both run before a re-render; this guard is synchronous. |
| 256 | const inFlight = useRef(false); |
| 257 | const formId = useId(); |
| 258 | const link = useAtomCommand(peerLinkEnvironment.link, { label: LINK_COMMAND_LABEL }); |
| 259 | const linkOnTarget = useAtomCommand(peerLinkEnvironment.link, { |
| 260 | label: "link environment back", |
| 261 | // The failure cause holds the request, and with it the code; keep it out of the console. |
| 262 | reportFailure: false, |
| 263 | reportDefect: false, |
| 264 | }); |
| 265 | const [alsoLinkBack, setAlsoLinkBack] = useState(false); |
| 266 | const selected = choice ?? (machines.length === 0 ? ANOTHER_ADDRESS : null); |
| 267 | const machine = machines.find((candidate) => candidate.environmentId === selected) ?? null; |
| 268 | const targetId = machine?.environmentId ?? null; |
| 269 | const connected = usePreparedConnection(targetId); |
| 270 | const mayMint = useEnvironmentScope(targetId, AuthAccessWriteScope); |
| 271 | // A picked machine that left the list has no address here either. |
| 272 | const needsAddress = selected !== null && (machine === null || machine.urls.length === 0); |
| 273 | // This client mints a code there when it is connected with a session that may |
| 274 | // create pairing links, and only for its own addresses: anything can answer |
| 275 | // as that machine at one typed in, so that one gets a code the user fetched. |
| 276 | const canMint = |
| 277 | targetId !== null && |
| 278 | !needsAddress && |
| 279 | Option.isSome(connected) && |
| 280 | mayMint && |
| 281 | !mintFailed.has(targetId); |
| 282 | const linkBack = useLinkBackOffers(environmentId, targetId === null ? [] : [targetId], open); |
| 283 | const backOffer = linkBack.offerFor(targetId); |
| 284 | const ready = |
| 285 | selected !== null && |
| 286 | (!needsAddress || address.trim().length > 0) && |
| 287 | (canMint || pairingCode.trim().length > 0); |
| 288 | const handleOpenChange = (next: boolean) => { |
| 289 | setOpen(next); |
| 290 | if (!next) { |
| 291 | setChoice(prefill?.choice ?? null); |
| 292 | setAddress(prefill?.address ?? ""); |
| 293 | setPairingCode(""); |
| 294 | setAccess("approval-required"); |
| 295 | setMintFailed(new Set()); |
| 296 | setAlsoLinkBack(false); |
| 297 | } |
| 298 | }; |
| 299 | // Settles to the code to link with, or null after reporting why there is none. |
| 300 | const resolvePairingCode = async () => { |
| 301 | if (!canMint || targetId === null) return pairingCode.trim(); |
| 302 | const minted = await mintPairingCodeOn(targetId, access); |
| 303 | if (Result.isSuccess(minted)) return minted.success; |
| 304 | setMintFailed((failed) => new Set(failed).add(targetId)); |
| 305 | failureToast( |
| 306 | gt("Could not link the environment"), |
| 307 | linkRequestFailureMessage(minted.failure, resolveMessage), |
| 308 | ); |
| 309 | return null; |
| 310 | }; |
| 311 | const submit = async () => { |
| 312 | const [url, ...alternateUrls] = |
| 313 | machine === null || needsAddress ? [address.trim()] : machine.urls; |
| 314 | if (inFlight.current || !ready || url === undefined) return; |
| 315 | inFlight.current = true; |
| 316 | setBusy(true); |
| 317 | const code = await resolvePairingCode(); |
| 318 | const result = |
| 319 | code === null |
| 320 | ? null |
| 321 | : await link({ |
| 322 | environmentId, |
| 323 | input: { |
| 324 | url, |
| 325 | ...(alternateUrls.length > 0 ? { alternateUrls } : {}), |
| 326 | // A typed address links alone: only this app's own addresses name a machine. |
| 327 | ...(machine === null || needsAddress |
| 328 | ? {} |
| 329 | : { expectedEnvironmentId: machine.environmentId, expectedLabel: machine.label }), |
| 330 | pairingCode: code, |
| 331 | access, |
| 332 | }, |
| 333 | }); |
| 334 | // The link back is its own link, made on the other machine: it never undoes this one. |
| 335 | const back = |
| 336 | result?._tag === "Success" && |
| 337 | alsoLinkBack && |
| 338 | backOffer.kind === "offered" && |
| 339 | linkBack.source !== null && |
| 340 | targetId !== null && |
| 341 | // A typed address can answer as another machine: the picked one links back only if it did. |
| 342 | result.value.environmentId === targetId |
| 343 | ? await linkWithMintedCode({ |
| 344 | machine: linkBack.source, |
| 345 | access, |
| 346 | mint: mintPairingCodeOn, |
| 347 | link: async (input) => { |
| 348 | const reverse = await linkOnTarget({ environmentId: targetId, input }); |
| 349 | return reverse._tag === "Success" |
| 350 | ? Result.succeed(reverse.value) |
| 351 | : Result.fail(squashAtomCommandFailure(reverse)); |
| 352 | }, |
| 353 | }) |
| 354 | : null; |
| 355 | inFlight.current = false; |
| 356 | setBusy(false); |
| 357 | if (result === null) return; |
| 358 | if (result._tag === "Success") { |
| 359 | const names = { source: source?.label ?? "", target: result.value.label }; |
| 360 | toastManager.add({ |
| 361 | type: "success", |
| 362 | title: |
| 363 | back?.kind === "linked" |
| 364 | ? gt("Linked {source} and {target} both ways", names) |
| 365 | : gt("Linked {label}", { label: result.value.label }), |
| 366 | }); |
| 367 | if (back?.kind === "failed") { |
| 368 | failureToast( |
| 369 | gt("Could not link {target} back to {source}", names), |
| 370 | back.detail ?? linkBatchFailureLabel(back.failure, m), |
| 371 | ); |
| 372 | } |
| 373 | handleOpenChange(false); |
| 374 | } else if (!isAtomCommandInterrupted(result)) { |
| 375 | failureToast(gt("Could not link the environment"), String(squashAtomCommandFailure(result))); |
| 376 | } |
| 377 | }; |
| 378 | return ( |
| 379 | <Dialog |
| 380 | open={open} |
| 381 | onOpenChange={(next) => { |
| 382 | if (!next && busy) return; |
| 383 | handleOpenChange(next); |
| 384 | }} |
| 385 | > |
| 386 | {trigger} |
| 387 | <DialogPopup className="max-w-md"> |
| 388 | <DialogHeader> |
| 389 | <DialogTitle> |
| 390 | {source === null ? ( |
| 391 | <T>Link an environment</T> |
| 392 | ) : machine === null ? ( |
| 393 | gt("Link {label} to another machine", { label: source.label }) |
| 394 | ) : ( |
| 395 | gt("Link {source} to {target}", { source: source.label, target: machine.label }) |
| 396 | )} |
| 397 | </DialogTitle> |
| 398 | <DialogDescription> |
| 399 | <T> |
| 400 | Agents here can then launch, message and wait on threads there. It appears under |
| 401 | Authorized clients in that environment's Sharing & access, where it can be revoked. |
| 402 | </T> |
| 403 | </DialogDescription> |
| 404 | </DialogHeader> |
| 405 | <DialogPanel> |
| 406 | <form |
| 407 | id={formId} |
| 408 | className="space-y-4" |
| 409 | autoComplete="off" |
| 410 | onSubmit={(event) => { |
| 411 | event.preventDefault(); |
| 412 | void submit(); |
| 413 | }} |
| 414 | > |
| 415 | {machines.length > 0 ? ( |
| 416 | <LinkMachineSelect |
| 417 | machines={machines} |
| 418 | value={selected} |
| 419 | disabled={busy} |
| 420 | onChange={(next) => { |
| 421 | // The opt-in names one machine: it does not carry over to another. |
| 422 | if (next !== selected) setAlsoLinkBack(false); |
| 423 | setChoice(next); |
| 424 | }} |
| 425 | /> |
| 426 | ) : null} |
| 427 | {machine?.linked ? ( |
| 428 | <p className="text-xs text-muted-foreground"> |
| 429 | <T>Already linked. Linking again renews it with the access you pick.</T> |
| 430 | </p> |
| 431 | ) : null} |
| 432 | {needsAddress ? ( |
| 433 | <label className="block space-y-1.5"> |
| 434 | <span className="block text-xs font-medium text-foreground"> |
| 435 | <T>Address</T> |
| 436 | </span> |
| 437 | <Input |
| 438 | value={address} |
| 439 | onChange={(event) => { |
| 440 | setAddress(event.target.value); |
| 441 | // Machines found after typing began must not hide the address being typed. |
| 442 | if (choice === null) setChoice(ANOTHER_ADDRESS); |
| 443 | }} |
| 444 | placeholder="https://box.example.ts.net" |
| 445 | disabled={busy} |
| 446 | autoFocus={machines.length === 0} |
| 447 | nativeInput |
| 448 | spellCheck={false} |
| 449 | /> |
| 450 | {machine !== null ? ( |
| 451 | <span className="block text-xs text-muted-foreground"> |
| 452 | <T> |
| 453 | This app has no address for <Var>{machine.label}</Var> that another machine |
| 454 | can reach. Enter its https or Tailscale address. |
| 455 | </T> |
| 456 | </span> |
| 457 | ) : null} |
| 458 | </label> |
| 459 | ) : null} |
| 460 | {needsAddress && address.trim() ? <LinkAddressWarning address={address} /> : null} |
| 461 | {selected === null || canMint ? null : ( |
| 462 | <label className="block space-y-1.5"> |
| 463 | <span className="block text-xs font-medium text-foreground"> |
| 464 | <T>Pairing code</T> |
| 465 | </span> |
| 466 | <Input |
| 467 | value={pairingCode} |
| 468 | onChange={(event) => setPairingCode(event.target.value)} |
| 469 | placeholder={ |
| 470 | machine === null |
| 471 | ? gt("From Sharing & access there, or t3 pair") |
| 472 | : gt("Paste the code") |
| 473 | } |
| 474 | disabled={busy} |
| 475 | autoComplete="one-time-code" |
| 476 | nativeInput |
| 477 | spellCheck={false} |
| 478 | /> |
| 479 | {machine !== null ? ( |
| 480 | <span className="block text-xs text-muted-foreground"> |
| 481 | <T> |
| 482 | Create a link on <Var>{machine.label}</Var>: Settings → Sharing & access → |
| 483 | Authorized clients → Create link, then paste its code here. |
| 484 | </T> |
| 485 | </span> |
| 486 | ) : null} |
| 487 | </label> |
| 488 | )} |
| 489 | <LinkAccessPicker value={access} onChange={setAccess} disabled={busy} /> |
| 490 | {machine === null || source === null ? null : backOffer.kind === "offered" ? ( |
| 491 | <label className="flex cursor-pointer items-start gap-2.5 text-sm has-data-disabled:cursor-default"> |
| 492 | <Checkbox |
| 493 | checked={alsoLinkBack} |
| 494 | disabled={busy} |
| 495 | onCheckedChange={setAlsoLinkBack} |
| 496 | /> |
| 497 | <span className="min-w-0 space-y-0.5"> |
| 498 | <span className="block font-medium text-foreground"> |
| 499 | {gt("Also link {target} back to {source}", { |
| 500 | source: source.label, |
| 501 | target: machine.label, |
| 502 | })} |
| 503 | </span> |
| 504 | <span className="block text-xs text-muted-foreground"> |
| 505 | {gt("Agents on {target} can then work on {source} too.", { |
| 506 | source: source.label, |
| 507 | target: machine.label, |
| 508 | })} |
| 509 | </span> |
| 510 | </span> |
| 511 | </label> |
| 512 | ) : backOffer.kind === "blocked" ? ( |
| 513 | <p className="text-xs text-muted-foreground"> |
| 514 | {linkBackBlockLabel( |
| 515 | backOffer.reason, |
| 516 | { source: source.label, target: machine.label }, |
| 517 | m, |
| 518 | )} |
| 519 | </p> |
| 520 | ) : null} |
| 521 | </form> |
| 522 | </DialogPanel> |
| 523 | <DialogFooter variant="bare"> |
| 524 | <DialogClose disabled={busy} render={<Button variant="outline" type="button" />}> |
| 525 | <T>Cancel</T> |
| 526 | </DialogClose> |
| 527 | <Button form={formId} type="submit" disabled={busy || !ready}> |
| 528 | {busy ? gt("Linking…") : gt("Link", { $context: "verb, links an environment" })} |
| 529 | </Button> |
| 530 | </DialogFooter> |
| 531 | </DialogPopup> |
| 532 | </Dialog> |
| 533 | ); |
| 534 | } |
| 535 | |
| 536 | /** What a link's health means for the user, in a sentence. */ |
| 537 | export function peerLinkStatusText(health: PeerLinkHealth, gt: ReturnType<typeof useGT>): string { |
| 538 | return health.kind === "expired" |
| 539 | ? gt("Expired. Link it again with a new pairing code.") |
| 540 | : health.kind === "unreachable" |
| 541 | ? (health.detail ?? gt("Not answering right now.")) |
| 542 | : health.kind === "failing" |
| 543 | ? health.detail |
| 544 | : health.expiresInDays !== null |
| 545 | ? gt( |
| 546 | "{days, plural, one {Expires in {days} day. Link it again to renew.} other {Expires in {days} days. Link it again to renew.}}", |
| 547 | { days: health.expiresInDays }, |
| 548 | ) |
| 549 | : gt("Reachable"); |
| 550 | } |
| 551 | |
| 552 | /** Forgets `link` from `environmentId` after the user confirms. */ |
| 553 | export function useForgetPeerLink(environmentId: EnvironmentId, link: PeerLinkSummary) { |
| 554 | const gt = useGT(); |
| 555 | const unlink = useAtomCommand(peerLinkEnvironment.unlink, { label: UNLINK_COMMAND_LABEL }); |
| 556 | const [busy, setBusy] = useState(false); |
| 557 | const forget = async () => { |
| 558 | const confirmed = await (requestConfirmDialog( |
| 559 | gt( |
| 560 | "Forget {label}? Agents here stop working there. Revoke \"T3 Code · …\" in {label}''s Sharing & access to end the session there too.", |
| 561 | { label: link.label }, |
| 562 | ), |
| 563 | { variant: "destructive" }, |
| 564 | ) ?? Promise.resolve(false)); |
| 565 | if (!confirmed) return; |
| 566 | setBusy(true); |
| 567 | const result = await unlink({ environmentId, input: { environmentId: link.environmentId } }); |
| 568 | setBusy(false); |
| 569 | if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) { |
| 570 | failureToast(gt("Could not forget the link"), String(squashAtomCommandFailure(result))); |
| 571 | } |
| 572 | }; |
| 573 | return { busy, forget }; |
| 574 | } |