The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { thrownErrorMessage } from "@t3tools/client-runtime/errors"; |
| 2 | import { m } from "~/i18n/messages"; |
| 3 | import { pipe } from "effect/Function"; |
| 4 | import { ChevronsLeftRightEllipsisIcon, PlusIcon, TerminalIcon } from "lucide-react"; |
| 5 | import { useAtomValue } from "@effect/atom-react"; |
| 6 | import { type KeyboardEvent, type ReactNode, useEffect, useRef, useState } from "react"; |
| 7 | import { T, useGT, useMessages, Var } from "gt-react"; |
| 8 | import { |
| 9 | type DesktopDiscoveredSshHost, |
| 10 | type DesktopSshEnvironmentTarget, |
| 11 | } from "@t3tools/contracts"; |
| 12 | import { |
| 13 | RelayConnectionRegistration, |
| 14 | RelayConnectionTarget, |
| 15 | } from "@t3tools/client-runtime/connection"; |
| 16 | import { |
| 17 | isAtomCommandInterrupted, |
| 18 | squashAtomCommandFailure, |
| 19 | } from "@t3tools/client-runtime/state/runtime"; |
| 20 | import * as Option from "effect/Option"; |
| 21 | |
| 22 | import { cn } from "../../lib/utils"; |
| 23 | import { formatDesktopSshTarget } from "./EnvironmentRow"; |
| 24 | import { Input } from "../ui/input"; |
| 25 | import { CommandShortcut } from "../ui/command"; |
| 26 | import { |
| 27 | Autocomplete, |
| 28 | AutocompleteEmpty, |
| 29 | AutocompleteInput, |
| 30 | AutocompleteItem, |
| 31 | AutocompleteList, |
| 32 | AutocompletePopup, |
| 33 | } from "../ui/autocomplete"; |
| 34 | import { |
| 35 | Dialog, |
| 36 | DialogDescription, |
| 37 | DialogHeader, |
| 38 | DialogPanel, |
| 39 | DialogPopup, |
| 40 | DialogTitle, |
| 41 | DialogTrigger, |
| 42 | } from "../ui/dialog"; |
| 43 | import { stackedThreadToast, toastManager } from "../ui/toast"; |
| 44 | import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; |
| 45 | import { Alert, AlertDescription } from "../ui/alert"; |
| 46 | import { Button } from "../ui/button"; |
| 47 | import { AnimatedHeight } from "../AnimatedHeight"; |
| 48 | import { environmentCatalog } from "~/connection/catalog"; |
| 49 | import { |
| 50 | connectPairing as connectPairingAtom, |
| 51 | connectSshEnvironment as connectSshEnvironmentAtom, |
| 52 | } from "~/connection/onboarding"; |
| 53 | import { useEnvironmentQuery } from "~/state/query"; |
| 54 | import { desktopSshHostsStateAtom, filterDiscoveredSshHosts } from "~/state/desktopSshHosts"; |
| 55 | import { type EnvironmentPresentation, useRelayEnvironmentDiscovery } from "~/state/environments"; |
| 56 | import type { AddEnvironmentPrefill } from "~/state/linkedThreadView"; |
| 57 | import { useAtomCommand } from "../../state/use-atom-command"; |
| 58 | import { primaryServerKeybindingsAtom } from "~/state/server"; |
| 59 | import { |
| 60 | resolveShortcutCommand, |
| 61 | shortcutLabelForCommand, |
| 62 | threadJumpCommandForIndex, |
| 63 | threadJumpIndexFromCommand, |
| 64 | } from "../../keybindings"; |
| 65 | import { |
| 66 | parseManualDesktopSshTarget, |
| 67 | parsePairingUrlFields, |
| 68 | parseRemotePairingFields, |
| 69 | formatDesktopSshConnectionError, |
| 70 | } from "./ConnectionsSettings.logic"; |
| 71 | |
| 72 | const SSH_PROTOCOL_NAME = "SSH"; |
| 73 | const EMPTY_DISCOVERED_SSH_HOSTS: ReadonlyArray<DesktopDiscoveredSshHost> = []; |
| 74 | // known_hosts can list one host on several ports. Its own bracketed form keeps an IPv6 |
| 75 | // host's port separate from the address; no portless known_hosts entry has that form. |
| 76 | const sshHostSuggestionValue = (target: DesktopDiscoveredSshHost) => |
| 77 | target.port === null ? target.alias : `[${target.alias}]:${target.port}`; |
| 78 | |
| 79 | export function AddEnvironmentDialog({ |
| 80 | open, |
| 81 | prefill = null, |
| 82 | routeTarget, |
| 83 | error, |
| 84 | savedEnvironments, |
| 85 | initialHost = "", |
| 86 | onOpenChange, |
| 87 | onErrorChange, |
| 88 | }: { |
| 89 | readonly open: boolean; |
| 90 | /** A machine the user chose elsewhere to pair with; they supply only the pairing code. */ |
| 91 | readonly prefill?: AddEnvironmentPrefill | null; |
| 92 | readonly routeTarget: EnvironmentPresentation | null; |
| 93 | /** An address already known for the machine being added, such as one a link uses. */ |
| 94 | readonly initialHost?: string; |
| 95 | readonly error: string | null; |
| 96 | readonly savedEnvironments: ReadonlyArray<EnvironmentPresentation>; |
| 97 | readonly onOpenChange: (open: boolean) => void; |
| 98 | readonly onErrorChange: (error: string | null) => void; |
| 99 | }) { |
| 100 | const gt = useGT(); |
| 101 | const desktopBridge = window.desktopBridge; |
| 102 | const keybindings = useAtomValue(primaryServerKeybindingsAtom); |
| 103 | const connectPairing = useAtomCommand(connectPairingAtom, { reportFailure: false }); |
| 104 | const connectSshEnvironment = useAtomCommand(connectSshEnvironmentAtom, { |
| 105 | reportFailure: false, |
| 106 | }); |
| 107 | const registerEnvironment = useAtomCommand(environmentCatalog.register, { |
| 108 | reportFailure: false, |
| 109 | }); |
| 110 | const relayDiscoveryState = useRelayEnvironmentDiscovery(); |
| 111 | const savedDesktopSshEnvironmentKeys = (() => { |
| 112 | const keys = new Set<string>(); |
| 113 | for (const environment of savedEnvironments) { |
| 114 | const profile = environment.entry.profile; |
| 115 | if ( |
| 116 | environment.entry.target._tag !== "SshConnectionTarget" || |
| 117 | Option.isNone(profile) || |
| 118 | profile.value._tag !== "SshConnectionProfile" |
| 119 | ) { |
| 120 | continue; |
| 121 | } |
| 122 | const target = profile.value.target; |
| 123 | keys.add(target.alias); |
| 124 | keys.add(formatDesktopSshTarget(target)); |
| 125 | } |
| 126 | return keys; |
| 127 | })(); |
| 128 | const [savedBackendMode, setSavedBackendMode] = useState<"remote" | "ssh">("remote"); |
| 129 | const [savedBackendHost, setSavedBackendHost] = useState(prefill?.host ?? initialHost); |
| 130 | const [savedBackendPairingCode, setSavedBackendPairingCode] = useState(""); |
| 131 | const [savedBackendSshHost, setSavedBackendSshHost] = useState(""); |
| 132 | const [savedBackendSshUsername, setSavedBackendSshUsername] = useState(""); |
| 133 | const [savedBackendSshPort, setSavedBackendSshPort] = useState(""); |
| 134 | const [sshHostSuggestionsOpen, setSshHostSuggestionsOpen] = useState(false); |
| 135 | const highlightedSshHostRef = useRef<DesktopDiscoveredSshHost | undefined>(undefined); |
| 136 | const [isAddingSavedBackend, setIsAddingSavedBackend] = useState(false); |
| 137 | const savedBackendSessionRef = useRef(0); |
| 138 | const isSshDiscoveryActive = desktopBridge !== undefined && open && savedBackendMode === "ssh"; |
| 139 | const desktopSshHosts = useEnvironmentQuery( |
| 140 | isSshDiscoveryActive ? desktopSshHostsStateAtom : null, |
| 141 | useMessages(), |
| 142 | ); |
| 143 | // The discovery atom is kept alive across dialog opens, so re-read SSH config |
| 144 | // each time the SSH tab is shown; stale hosts stay visible while it refreshes. |
| 145 | const refreshDesktopSshHosts = desktopSshHosts.refresh; |
| 146 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- rereads desktop SSH config through the bridge each time the SSH tab is shown |
| 147 | useEffect(() => { |
| 148 | if (isSshDiscoveryActive) refreshDesktopSshHosts(); |
| 149 | }, [isSshDiscoveryActive, refreshDesktopSshHosts]); |
| 150 | const discoveredSshHosts = desktopSshHosts.data ?? EMPTY_DISCOVERED_SSH_HOSTS; |
| 151 | const unsavedDiscoveredSshHosts = discoveredSshHosts.filter((target) => { |
| 152 | const address = formatDesktopSshTarget(target); |
| 153 | return ( |
| 154 | !savedDesktopSshEnvironmentKeys.has(target.alias) && |
| 155 | (target.source === "ssh-config" || !savedDesktopSshEnvironmentKeys.has(address)) |
| 156 | ); |
| 157 | }); |
| 158 | const filteredDiscoveredSshHosts = filterDiscoveredSshHosts( |
| 159 | unsavedDiscoveredSshHosts, |
| 160 | savedBackendSshHost, |
| 161 | ); |
| 162 | const isLoadingDiscoveredSshHosts = desktopSshHosts.isPending && desktopSshHosts.data === null; |
| 163 | const discoveredSshHostsError = desktopSshHosts.error; |
| 164 | const hasSshHostSuggestionContent = |
| 165 | desktopBridge !== undefined && |
| 166 | (isLoadingDiscoveredSshHosts || unsavedDiscoveredSshHosts.length > 0); |
| 167 | const handleOpenChange = (nextOpen: boolean) => { |
| 168 | if (!nextOpen) { |
| 169 | savedBackendSessionRef.current += 1; |
| 170 | setSavedBackendMode("remote"); |
| 171 | setSavedBackendHost(initialHost); |
| 172 | setSavedBackendPairingCode(""); |
| 173 | setSavedBackendSshHost(""); |
| 174 | setSavedBackendSshUsername(""); |
| 175 | setSavedBackendSshPort(""); |
| 176 | } |
| 177 | onOpenChange(nextOpen); |
| 178 | }; |
| 179 | const connectSavedBackendSshTarget = async (target: DesktopSshEnvironmentTarget) => { |
| 180 | const session = savedBackendSessionRef.current; |
| 181 | setIsAddingSavedBackend(true); |
| 182 | onErrorChange(null); |
| 183 | const result = await connectSshEnvironment({ |
| 184 | target, |
| 185 | label: "", |
| 186 | ...(routeTarget ? { expectedEnvironmentId: routeTarget.environmentId } : {}), |
| 187 | }); |
| 188 | if (result._tag === "Failure") { |
| 189 | if (session === savedBackendSessionRef.current && !isAtomCommandInterrupted(result)) { |
| 190 | pipe(squashAtomCommandFailure(result), formatDesktopSshConnectionError, onErrorChange); |
| 191 | } |
| 192 | setIsAddingSavedBackend(false); |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | handleOpenChange(false); |
| 197 | toastManager.add({ |
| 198 | type: "success", |
| 199 | title: routeTarget ? gt("Route added") : gt("Environment connected"), |
| 200 | description: routeTarget |
| 201 | ? gt("{environment} can now be reached over SSH {host}.", { |
| 202 | environment: routeTarget.label, |
| 203 | host: target.alias, |
| 204 | }) |
| 205 | : gt("{host} is ready over an SSH-managed tunnel.", { host: target.alias }), |
| 206 | }); |
| 207 | setIsAddingSavedBackend(false); |
| 208 | }; |
| 209 | |
| 210 | const handleAddSavedBackend = async () => { |
| 211 | if (savedBackendMode === "ssh") { |
| 212 | let target: DesktopSshEnvironmentTarget; |
| 213 | try { |
| 214 | target = parseManualDesktopSshTarget({ |
| 215 | host: savedBackendSshHost, |
| 216 | username: savedBackendSshUsername, |
| 217 | port: savedBackendSshPort, |
| 218 | }); |
| 219 | } catch (error) { |
| 220 | onErrorChange(formatDesktopSshConnectionError(error)); |
| 221 | return; |
| 222 | } |
| 223 | |
| 224 | await connectSavedBackendSshTarget(target); |
| 225 | return; |
| 226 | } |
| 227 | |
| 228 | setIsAddingSavedBackend(true); |
| 229 | onErrorChange(null); |
| 230 | let remotePairingInput: ReturnType<typeof parseRemotePairingFields>; |
| 231 | try { |
| 232 | remotePairingInput = parseRemotePairingFields({ |
| 233 | host: savedBackendHost, |
| 234 | pairingCode: savedBackendPairingCode, |
| 235 | }); |
| 236 | } catch (error) { |
| 237 | const message = |
| 238 | error instanceof Error |
| 239 | ? (thrownErrorMessage(error, m) ?? error.message) |
| 240 | : gt("Failed to add backend."); |
| 241 | onErrorChange(message); |
| 242 | toastManager.add( |
| 243 | stackedThreadToast({ |
| 244 | type: "error", |
| 245 | title: gt("Could not add backend"), |
| 246 | description: message, |
| 247 | }), |
| 248 | ); |
| 249 | setIsAddingSavedBackend(false); |
| 250 | return; |
| 251 | } |
| 252 | |
| 253 | const expected = routeTarget ?? prefill; |
| 254 | const result = await connectPairing({ |
| 255 | ...remotePairingInput, |
| 256 | ...(expected ? { expectedEnvironmentId: expected.environmentId } : {}), |
| 257 | }); |
| 258 | if (result._tag === "Failure") { |
| 259 | if (!isAtomCommandInterrupted(result)) { |
| 260 | const error = squashAtomCommandFailure(result); |
| 261 | const message = |
| 262 | error instanceof Error |
| 263 | ? (thrownErrorMessage(error, m) ?? error.message) |
| 264 | : gt("Failed to add backend."); |
| 265 | onErrorChange(message); |
| 266 | toastManager.add( |
| 267 | stackedThreadToast({ |
| 268 | type: "error", |
| 269 | title: gt("Could not add backend"), |
| 270 | description: message, |
| 271 | }), |
| 272 | ); |
| 273 | } |
| 274 | setIsAddingSavedBackend(false); |
| 275 | return; |
| 276 | } |
| 277 | |
| 278 | handleOpenChange(false); |
| 279 | toastManager.add( |
| 280 | routeTarget |
| 281 | ? { |
| 282 | type: "success", |
| 283 | title: gt("Route added"), |
| 284 | description: gt("{environment} now has another way to connect.", { |
| 285 | environment: routeTarget.label, |
| 286 | }), |
| 287 | } |
| 288 | : { |
| 289 | type: "success", |
| 290 | title: gt("Backend added"), |
| 291 | description: gt("The environment is saved and will reconnect on app startup."), |
| 292 | }, |
| 293 | ); |
| 294 | setIsAddingSavedBackend(false); |
| 295 | }; |
| 296 | |
| 297 | const handleSavedBackendSshFieldKeyDown = (event: KeyboardEvent<HTMLInputElement>) => { |
| 298 | if (event.nativeEvent.isComposing || event.keyCode === 229) return; |
| 299 | if (event.key === "Enter" && savedBackendSshHost.trim().length > 0) { |
| 300 | event.preventDefault(); |
| 301 | void handleAddSavedBackend(); |
| 302 | } |
| 303 | }; |
| 304 | |
| 305 | const handleSelectSshHostSuggestion = async (target: DesktopDiscoveredSshHost) => { |
| 306 | if (isAddingSavedBackend || !desktopBridge) return; |
| 307 | |
| 308 | const session = savedBackendSessionRef.current; |
| 309 | setIsAddingSavedBackend(true); |
| 310 | onErrorChange(null); |
| 311 | setSavedBackendSshHost(target.alias); |
| 312 | let resolved: DesktopSshEnvironmentTarget; |
| 313 | try { |
| 314 | resolved = await desktopBridge.resolveSshHost(target.alias); |
| 315 | } catch (error) { |
| 316 | if (session === savedBackendSessionRef.current) { |
| 317 | onErrorChange(formatDesktopSshConnectionError(error)); |
| 318 | } |
| 319 | setIsAddingSavedBackend(false); |
| 320 | return; |
| 321 | } |
| 322 | if (session !== savedBackendSessionRef.current) { |
| 323 | setIsAddingSavedBackend(false); |
| 324 | return; |
| 325 | } |
| 326 | if (target.port !== null) resolved = { ...resolved, port: target.port }; |
| 327 | setSavedBackendSshUsername(resolved.username ?? ""); |
| 328 | setSavedBackendSshPort(resolved.port === null ? "" : String(resolved.port)); |
| 329 | await connectSavedBackendSshTarget(resolved); |
| 330 | }; |
| 331 | |
| 332 | const handleSavedBackendSshHostKeyDown = (event: KeyboardEvent<HTMLInputElement>) => { |
| 333 | if (event.nativeEvent.isComposing || event.keyCode === 229) return; |
| 334 | |
| 335 | const isSshHostPopupVisible = sshHostSuggestionsOpen && hasSshHostSuggestionContent; |
| 336 | if (isSshHostPopupVisible) { |
| 337 | const command = resolveShortcutCommand(event, keybindings, { |
| 338 | platform: navigator.platform, |
| 339 | context: { modelPickerOpen: false }, |
| 340 | }); |
| 341 | const index = threadJumpIndexFromCommand(command ?? ""); |
| 342 | const target = index === null ? undefined : filteredDiscoveredSshHosts[index]; |
| 343 | if (target) { |
| 344 | event.preventDefault(); |
| 345 | event.stopPropagation(); |
| 346 | setSshHostSuggestionsOpen(false); |
| 347 | void handleSelectSshHostSuggestion(target); |
| 348 | return; |
| 349 | } |
| 350 | |
| 351 | if (event.key === "Escape") { |
| 352 | event.preventDefault(); |
| 353 | event.stopPropagation(); |
| 354 | return; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | const hasHighlightedSshHost = |
| 359 | isSshHostPopupVisible && highlightedSshHostRef.current !== undefined; |
| 360 | if ( |
| 361 | !event.defaultPrevented && |
| 362 | !hasHighlightedSshHost && |
| 363 | event.key === "Enter" && |
| 364 | savedBackendSshHost.trim().length > 0 |
| 365 | ) { |
| 366 | event.preventDefault(); |
| 367 | void handleAddSavedBackend(); |
| 368 | } |
| 369 | }; |
| 370 | |
| 371 | const handleSavedBackendHostChange = (value: string) => { |
| 372 | const parsedPairingUrl = parsePairingUrlFields(value); |
| 373 | if (parsedPairingUrl) { |
| 374 | setSavedBackendHost(parsedPairingUrl.host); |
| 375 | setSavedBackendPairingCode(parsedPairingUrl.pairingCode); |
| 376 | return; |
| 377 | } |
| 378 | setSavedBackendHost(value); |
| 379 | }; |
| 380 | const renderConnectionModeCard = (input: { |
| 381 | readonly mode: "remote" | "ssh"; |
| 382 | readonly title: string; |
| 383 | readonly description: string; |
| 384 | readonly icon?: ReactNode; |
| 385 | }) => { |
| 386 | const selected = savedBackendMode === input.mode; |
| 387 | return ( |
| 388 | <button |
| 389 | type="button" |
| 390 | aria-pressed={selected} |
| 391 | className={cn( |
| 392 | "group flex min-h-24 items-start gap-3 rounded-lg border p-4 text-left", |
| 393 | selected ? "border-primary/50 bg-primary/5" : "border-border/60 hover:bg-muted/40", |
| 394 | )} |
| 395 | disabled={isAddingSavedBackend} |
| 396 | onClick={() => { |
| 397 | if (!selected) onErrorChange(null); |
| 398 | setSavedBackendMode(input.mode); |
| 399 | }} |
| 400 | > |
| 401 | {input.icon ? ( |
| 402 | <span |
| 403 | className={cn( |
| 404 | "mt-0.5 flex size-8 shrink-0 items-center justify-center rounded-md border", |
| 405 | selected |
| 406 | ? "border-primary/30 bg-primary/10 text-primary" |
| 407 | : "border-border/70 bg-background text-muted-foreground group-hover:text-foreground", |
| 408 | )} |
| 409 | > |
| 410 | {input.icon} |
| 411 | </span> |
| 412 | ) : null} |
| 413 | <span className="min-w-0"> |
| 414 | <span className="block text-sm font-medium text-foreground">{input.title}</span> |
| 415 | <span className="mt-1 block text-xs leading-relaxed text-muted-foreground"> |
| 416 | {input.description} |
| 417 | </span> |
| 418 | </span> |
| 419 | </button> |
| 420 | ); |
| 421 | }; |
| 422 | |
| 423 | const renderRemoteFields = () => ( |
| 424 | <div className="space-y-3"> |
| 425 | <div className="grid gap-3 sm:grid-cols-[minmax(0,1fr)_10rem]"> |
| 426 | <label className="block"> |
| 427 | <span className="mb-1.5 block text-xs font-medium text-foreground"> |
| 428 | <T>Host</T> |
| 429 | </span> |
| 430 | <Input |
| 431 | value={savedBackendHost} |
| 432 | onChange={(event) => handleSavedBackendHostChange(event.target.value)} |
| 433 | placeholder="backend.example.com" |
| 434 | disabled={isAddingSavedBackend} |
| 435 | spellCheck={false} |
| 436 | /> |
| 437 | </label> |
| 438 | <label className="block"> |
| 439 | <span className="mb-1.5 block text-xs font-medium text-foreground"> |
| 440 | <T>Pairing code</T> |
| 441 | </span> |
| 442 | <Input |
| 443 | value={savedBackendPairingCode} |
| 444 | onChange={(event) => setSavedBackendPairingCode(event.target.value)} |
| 445 | placeholder={gt("PAIRCODE")} |
| 446 | disabled={isAddingSavedBackend} |
| 447 | spellCheck={false} |
| 448 | /> |
| 449 | </label> |
| 450 | </div> |
| 451 | <div> |
| 452 | <span className="mt-1 block text-2xs text-muted-foreground"> |
| 453 | <T>Paste a full pairing URL here to fill both fields automatically.</T> |
| 454 | </span> |
| 455 | </div> |
| 456 | </div> |
| 457 | ); |
| 458 | const relayRouteOffer = |
| 459 | routeTarget !== null && |
| 460 | !routeTarget.relayManaged && |
| 461 | relayDiscoveryState.environments.has(routeTarget.environmentId) |
| 462 | ? relayDiscoveryState.environments.get(routeTarget.environmentId)!.environment |
| 463 | : null; |
| 464 | const addRelayRoute = async () => { |
| 465 | if (relayRouteOffer === null || routeTarget === null) return; |
| 466 | setIsAddingSavedBackend(true); |
| 467 | const result = await registerEnvironment( |
| 468 | new RelayConnectionRegistration({ |
| 469 | target: new RelayConnectionTarget({ |
| 470 | environmentId: relayRouteOffer.environmentId, |
| 471 | label: routeTarget.label, |
| 472 | }), |
| 473 | }), |
| 474 | ); |
| 475 | setIsAddingSavedBackend(false); |
| 476 | if (result._tag === "Failure") { |
| 477 | if (!isAtomCommandInterrupted(result)) { |
| 478 | const error = squashAtomCommandFailure(result); |
| 479 | onErrorChange( |
| 480 | error instanceof Error |
| 481 | ? (thrownErrorMessage(error, m) ?? error.message) |
| 482 | : gt("Could not add the route."), |
| 483 | ); |
| 484 | } |
| 485 | return; |
| 486 | } |
| 487 | handleOpenChange(false); |
| 488 | toastManager.add({ |
| 489 | type: "success", |
| 490 | title: gt("Route added"), |
| 491 | description: gt( |
| 492 | "{environment} falls back to T3 Connect when its other routes are unreachable.", |
| 493 | { environment: routeTarget.label }, |
| 494 | ), |
| 495 | }); |
| 496 | }; |
| 497 | const renderRemoteModeBody = () => ( |
| 498 | <div className="space-y-4"> |
| 499 | {relayRouteOffer !== null ? ( |
| 500 | <div className="flex items-center justify-between gap-3 rounded-md border border-border px-3 py-2"> |
| 501 | <p className="text-xs text-muted-foreground"> |
| 502 | <T>This machine is on your T3 Connect account. Use it as a fallback route.</T> |
| 503 | </p> |
| 504 | <Button |
| 505 | size="xs" |
| 506 | variant="outline" |
| 507 | disabled={isAddingSavedBackend} |
| 508 | onClick={() => void addRelayRoute()} |
| 509 | > |
| 510 | <T>Add T3 Connect</T> |
| 511 | </Button> |
| 512 | </div> |
| 513 | ) : null} |
| 514 | {renderRemoteFields()} |
| 515 | {error ? <p className="text-xs text-destructive">{error}</p> : null} |
| 516 | <Button |
| 517 | variant="outline" |
| 518 | className="w-full" |
| 519 | disabled={isAddingSavedBackend} |
| 520 | onClick={() => void handleAddSavedBackend()} |
| 521 | > |
| 522 | <PlusIcon className="size-3.5" /> |
| 523 | {isAddingSavedBackend |
| 524 | ? gt("Adding…") |
| 525 | : routeTarget |
| 526 | ? gt("Add route") |
| 527 | : gt("Add environment")} |
| 528 | </Button> |
| 529 | </div> |
| 530 | ); |
| 531 | const renderSshFields = () => ( |
| 532 | <div className="space-y-4"> |
| 533 | <div className="space-y-3"> |
| 534 | <div className="block"> |
| 535 | <label |
| 536 | htmlFor="saved-backend-ssh-host" |
| 537 | className="mb-1.5 block text-xs font-medium text-foreground" |
| 538 | > |
| 539 | <T>SSH host or alias</T> |
| 540 | </label> |
| 541 | <Autocomplete |
| 542 | items={filteredDiscoveredSshHosts} |
| 543 | itemToStringValue={sshHostSuggestionValue} |
| 544 | mode="none" |
| 545 | openOnInputClick |
| 546 | open={sshHostSuggestionsOpen} |
| 547 | onOpenChange={setSshHostSuggestionsOpen} |
| 548 | onItemHighlighted={(target) => { |
| 549 | highlightedSshHostRef.current = target; |
| 550 | }} |
| 551 | value={savedBackendSshHost} |
| 552 | onValueChange={(value, eventDetails) => { |
| 553 | setSavedBackendSshHost(value); |
| 554 | if (eventDetails.reason !== "item-press") return; |
| 555 | |
| 556 | const target = filteredDiscoveredSshHosts.find( |
| 557 | (host) => sshHostSuggestionValue(host) === value, |
| 558 | ); |
| 559 | if (target) void handleSelectSshHostSuggestion(target); |
| 560 | }} |
| 561 | > |
| 562 | <AutocompleteInput |
| 563 | id="saved-backend-ssh-host" |
| 564 | onKeyDown={handleSavedBackendSshHostKeyDown} |
| 565 | placeholder={gt("Search hosts or type devbox")} |
| 566 | disabled={isAddingSavedBackend} |
| 567 | spellCheck={false} |
| 568 | /> |
| 569 | {hasSshHostSuggestionContent ? ( |
| 570 | <AutocompletePopup> |
| 571 | {isLoadingDiscoveredSshHosts ? ( |
| 572 | <div className="px-3 py-2 text-xs text-muted-foreground"> |
| 573 | <T>Loading hosts…</T> |
| 574 | </div> |
| 575 | ) : filteredDiscoveredSshHosts.length > 0 ? ( |
| 576 | <AutocompleteList className="max-h-72"> |
| 577 | {filteredDiscoveredSshHosts.map((target, index) => { |
| 578 | const address = formatDesktopSshTarget(target); |
| 579 | const shortcutCommand = index < 9 ? threadJumpCommandForIndex(index) : null; |
| 580 | const shortcutLabel = shortcutCommand |
| 581 | ? shortcutLabelForCommand(keybindings, shortcutCommand, navigator.platform) |
| 582 | : null; |
| 583 | return ( |
| 584 | <AutocompleteItem |
| 585 | key={`${target.alias}:${target.hostname}:${target.port ?? ""}`} |
| 586 | value={target} |
| 587 | className="h-8 min-h-8 whitespace-nowrap" |
| 588 | > |
| 589 | <span className="min-w-0 truncate text-sm font-medium"> |
| 590 | {target.alias} |
| 591 | </span> |
| 592 | {address !== target.alias ? ( |
| 593 | <span className="min-w-0 flex-1 truncate text-xs text-muted-foreground"> |
| 594 | {address} |
| 595 | </span> |
| 596 | ) : ( |
| 597 | <span className="flex-1" /> |
| 598 | )} |
| 599 | {shortcutLabel ? ( |
| 600 | <CommandShortcut className="shrink-0">{shortcutLabel}</CommandShortcut> |
| 601 | ) : null} |
| 602 | </AutocompleteItem> |
| 603 | ); |
| 604 | })} |
| 605 | </AutocompleteList> |
| 606 | ) : ( |
| 607 | <AutocompleteEmpty className="break-all"> |
| 608 | <T> |
| 609 | No hosts match "<Var>{savedBackendSshHost.trim()}</Var>". |
| 610 | </T> |
| 611 | </AutocompleteEmpty> |
| 612 | )} |
| 613 | </AutocompletePopup> |
| 614 | ) : null} |
| 615 | </Autocomplete> |
| 616 | </div> |
| 617 | <div className="grid gap-3 sm:grid-cols-[minmax(0,1fr)_7rem]"> |
| 618 | <label className="block"> |
| 619 | <span className="mb-1.5 block text-xs font-medium text-foreground"> |
| 620 | <T>Username</T> |
| 621 | </span> |
| 622 | <Input |
| 623 | value={savedBackendSshUsername} |
| 624 | onChange={(event) => setSavedBackendSshUsername(event.target.value)} |
| 625 | onKeyDown={handleSavedBackendSshFieldKeyDown} |
| 626 | placeholder="root" |
| 627 | disabled={isAddingSavedBackend} |
| 628 | spellCheck={false} |
| 629 | /> |
| 630 | </label> |
| 631 | <label className="block"> |
| 632 | <span className="mb-1.5 block text-xs font-medium text-foreground"> |
| 633 | <T>Port</T> |
| 634 | </span> |
| 635 | <Input |
| 636 | value={savedBackendSshPort} |
| 637 | onChange={(event) => setSavedBackendSshPort(event.target.value)} |
| 638 | onKeyDown={handleSavedBackendSshFieldKeyDown} |
| 639 | placeholder="22" |
| 640 | inputMode="numeric" |
| 641 | disabled={isAddingSavedBackend} |
| 642 | spellCheck={false} |
| 643 | /> |
| 644 | </label> |
| 645 | </div> |
| 646 | {error || discoveredSshHostsError ? ( |
| 647 | <Alert variant="error"> |
| 648 | <AlertDescription>{error ?? discoveredSshHostsError}</AlertDescription> |
| 649 | </Alert> |
| 650 | ) : null} |
| 651 | <Button |
| 652 | variant="outline" |
| 653 | className="w-full" |
| 654 | disabled={isAddingSavedBackend} |
| 655 | onClick={() => void handleAddSavedBackend()} |
| 656 | > |
| 657 | <PlusIcon className="size-3.5" /> |
| 658 | {isAddingSavedBackend |
| 659 | ? gt("Adding…") |
| 660 | : routeTarget |
| 661 | ? gt("Add route") |
| 662 | : gt("Add environment")} |
| 663 | </Button> |
| 664 | </div> |
| 665 | </div> |
| 666 | ); |
| 667 | |
| 668 | return ( |
| 669 | <Dialog open={open} onOpenChange={handleOpenChange}> |
| 670 | <Tooltip> |
| 671 | <TooltipTrigger |
| 672 | render={ |
| 673 | <DialogTrigger |
| 674 | render={ |
| 675 | <Button size="xs" variant="ghost-muted" aria-label={gt("Add environment")}> |
| 676 | <PlusIcon className="size-3" /> |
| 677 | <span> |
| 678 | <T>Add environment</T> |
| 679 | </span> |
| 680 | </Button> |
| 681 | } |
| 682 | /> |
| 683 | } |
| 684 | /> |
| 685 | <TooltipPopup side="top"> |
| 686 | <T>Add environment</T> |
| 687 | </TooltipPopup> |
| 688 | </Tooltip> |
| 689 | <DialogPopup className="max-h-[80dvh] sm:max-w-3xl"> |
| 690 | <DialogHeader> |
| 691 | <DialogTitle> |
| 692 | {routeTarget |
| 693 | ? gt("Add a route to {environment}", { environment: routeTarget.label }) |
| 694 | : prefill |
| 695 | ? gt("Connect to {environment}", { environment: prefill.label }) |
| 696 | : gt("Add Environment")} |
| 697 | </DialogTitle> |
| 698 | <DialogDescription> |
| 699 | {routeTarget |
| 700 | ? gt( |
| 701 | "Pair this machine again over another address, such as its Tailscale name. It joins the existing routes instead of adding a second machine.", |
| 702 | ) |
| 703 | : prefill |
| 704 | ? gt( |
| 705 | "Create a pairing code on {environment}, in its Settings → Sharing & access or with `t3 auth pairing create`, and enter it here.", |
| 706 | { environment: prefill.label }, |
| 707 | ) |
| 708 | : gt("Pair another environment to this client.")} |
| 709 | </DialogDescription> |
| 710 | </DialogHeader> |
| 711 | <DialogPanel> |
| 712 | <div className="space-y-4"> |
| 713 | <div className="grid gap-3 sm:grid-cols-2"> |
| 714 | {renderConnectionModeCard({ |
| 715 | mode: "remote", |
| 716 | title: gt("Remote link"), |
| 717 | description: gt("Enter a backend host and pairing code."), |
| 718 | icon: <ChevronsLeftRightEllipsisIcon aria-hidden className="size-4" />, |
| 719 | })} |
| 720 | {desktopBridge |
| 721 | ? renderConnectionModeCard({ |
| 722 | mode: "ssh", |
| 723 | title: SSH_PROTOCOL_NAME, |
| 724 | description: gt("Use local SSH config, agent, and tunnels for the backend."), |
| 725 | icon: <TerminalIcon aria-hidden className="size-4" />, |
| 726 | }) |
| 727 | : null} |
| 728 | </div> |
| 729 | <AnimatedHeight> |
| 730 | {savedBackendMode === "ssh" ? renderSshFields() : renderRemoteModeBody()} |
| 731 | </AnimatedHeight> |
| 732 | </div> |
| 733 | </DialogPanel> |
| 734 | </DialogPopup> |
| 735 | </Dialog> |
| 736 | ); |
| 737 | } |