| 1 | import { findErrorTraceId } from "@t3tools/client-runtime/errors"; |
| 2 | import { |
| 3 | type EnvironmentConnectionPresentation, |
| 4 | RelayConnectionRegistration, |
| 5 | RelayConnectionTarget, |
| 6 | orchestrationProtocolCompatibilityError, |
| 7 | } from "@t3tools/client-runtime/connection"; |
| 8 | import { relayOfflineReasonMessage } from "@t3tools/client-runtime/relay"; |
| 9 | import { |
| 10 | isAtomCommandInterrupted, |
| 11 | squashAtomCommandFailure, |
| 12 | } from "@t3tools/client-runtime/state/runtime"; |
| 13 | import { |
| 14 | type EnvironmentId, |
| 15 | resolveEnvironmentMachineKind, |
| 16 | type ServerConfig, |
| 17 | } from "@t3tools/contracts"; |
| 18 | import type { |
| 19 | RelayClientEnvironmentRecord, |
| 20 | RelayEnvironmentStatusResponse, |
| 21 | } from "@t3tools/contracts/relay"; |
| 22 | import * as Option from "effect/Option"; |
| 23 | import { type ReactNode, useCallback, useEffect, useEffectEvent, useState } from "react"; |
| 24 | |
| 25 | import { environmentCatalog } from "~/connection/catalog"; |
| 26 | import { cn } from "~/lib/utils"; |
| 27 | import { relayEnvironmentDiscovery } from "~/state/relay"; |
| 28 | import { useRelayEnvironmentDiscovery } from "~/state/environments"; |
| 29 | import { useAtomCommand } from "~/state/use-atom-command"; |
| 30 | import { ConnectionStatusDot } from "../ConnectionStatusDot"; |
| 31 | import { EnvironmentMachineIcon } from "../EnvironmentMachineIcon"; |
| 32 | import { ITEM_ROW_CLASSNAME, ITEM_ROW_INNER_CLASSNAME } from "../settings/itemRows"; |
| 33 | import { Checkbox } from "../ui/checkbox"; |
| 34 | import { Button } from "../ui/button"; |
| 35 | import { Skeleton } from "../ui/skeleton"; |
| 36 | import { toastManager } from "../ui/toast"; |
| 37 | import { Tooltip, TooltipTrigger, TooltipPopup } from "../ui/tooltip"; |
| 38 | import { presentSavedCloudEnvironmentConnection } from "./cloudEnvironmentConnectionPresentation"; |
| 39 | |
| 40 | const EMPTY_DISCOVERY_REFRESH_INTERVAL_MS = 5_000; |
| 41 | |
| 42 | function discoveredCompatibilityError( |
| 43 | status: Option.Option<RelayEnvironmentStatusResponse> | undefined, |
| 44 | ) { |
| 45 | const descriptor = status === undefined ? undefined : Option.getOrNull(status)?.descriptor; |
| 46 | return descriptor === undefined ? null : orchestrationProtocolCompatibilityError(descriptor); |
| 47 | } |
| 48 | |
| 49 | export interface SavedCloudEnvironmentConnection { |
| 50 | readonly environmentId: EnvironmentId; |
| 51 | readonly connection: EnvironmentConnectionPresentation; |
| 52 | /** False for a machine saved over another route (LAN, Tailscale, SSH) only. */ |
| 53 | readonly relayManaged: boolean; |
| 54 | /** Present once connected; carries the user's icon override. */ |
| 55 | readonly serverConfig?: ServerConfig | null; |
| 56 | } |
| 57 | |
| 58 | function RemoteEnvironmentRowsSkeleton() { |
| 59 | return ( |
| 60 | <div className={ITEM_ROW_CLASSNAME}> |
| 61 | <div className={ITEM_ROW_INNER_CLASSNAME}> |
| 62 | <div className="min-w-0 flex-1 space-y-2"> |
| 63 | <Skeleton shape="pill" className="h-4 w-32" /> |
| 64 | <Skeleton shape="pill" className="h-3 w-20" /> |
| 65 | </div> |
| 66 | <Skeleton className="h-7 w-16" /> |
| 67 | </div> |
| 68 | </div> |
| 69 | ); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * The user's T3 Connect environments from relay discovery, each with a |
| 74 | * Connect button. The primary environment is always excluded; already-saved |
| 75 | * environments are hidden unless `showSavedEnvironments` renders them with |
| 76 | * their live connection state (used by onboarding, where the full device mesh |
| 77 | * should be visible). |
| 78 | */ |
| 79 | export function CloudEnvironmentConnectRows({ |
| 80 | primaryEnvironmentId, |
| 81 | savedEnvironments, |
| 82 | showSavedEnvironments = false, |
| 83 | refreshWhileEmpty = false, |
| 84 | empty = null, |
| 85 | selection, |
| 86 | onDiscoveryReady, |
| 87 | }: { |
| 88 | readonly primaryEnvironmentId: EnvironmentId | null; |
| 89 | readonly savedEnvironments: ReadonlyArray<SavedCloudEnvironmentConnection>; |
| 90 | readonly showSavedEnvironments?: boolean; |
| 91 | readonly refreshWhileEmpty?: boolean; |
| 92 | readonly empty?: ReactNode; |
| 93 | readonly onDiscoveryReady?: () => void; |
| 94 | readonly selection?: { |
| 95 | readonly autoSelectedComputers?: Set<EnvironmentId>; |
| 96 | readonly selectedIds: ReadonlySet<EnvironmentId>; |
| 97 | readonly onChange: (environmentId: EnvironmentId, selected: boolean) => void; |
| 98 | }; |
| 99 | }) { |
| 100 | const environmentsState = useRelayEnvironmentDiscovery(); |
| 101 | const registerEnvironment = useAtomCommand(environmentCatalog.register, { |
| 102 | reportFailure: false, |
| 103 | }); |
| 104 | const refreshRelayEnvironments = useAtomCommand(relayEnvironmentDiscovery.refresh, { |
| 105 | reportFailure: false, |
| 106 | }); |
| 107 | const refreshDiscoveryWhenIdle = useEffectEvent(async () => { |
| 108 | if (environmentsState.refreshing || environmentsState.offline) return; |
| 109 | await refreshRelayEnvironments(); |
| 110 | }); |
| 111 | const connectRelayEnvironment = useCallback( |
| 112 | (environment: RelayClientEnvironmentRecord) => |
| 113 | registerEnvironment( |
| 114 | new RelayConnectionRegistration({ |
| 115 | target: new RelayConnectionTarget({ |
| 116 | environmentId: environment.environmentId, |
| 117 | label: environment.label, |
| 118 | }), |
| 119 | }), |
| 120 | ), |
| 121 | [registerEnvironment], |
| 122 | ); |
| 123 | const [connectingEnvironmentIds, setConnectingEnvironmentIds] = useState< |
| 124 | ReadonlySet<EnvironmentId> |
| 125 | >(new Set()); |
| 126 | const savedById = new Map( |
| 127 | savedEnvironments |
| 128 | .filter((environment) => environment.relayManaged) |
| 129 | .map((environment) => [environment.environmentId, environment]), |
| 130 | ); |
| 131 | // Saved over another route only: T3 Connect would be an added fallback. |
| 132 | const savedWithoutRelay = new Set( |
| 133 | savedEnvironments |
| 134 | .filter((environment) => !environment.relayManaged) |
| 135 | .map((environment) => environment.environmentId), |
| 136 | ); |
| 137 | |
| 138 | useEffect(() => { |
| 139 | let active = true; |
| 140 | if (onDiscoveryReady || !refreshWhileEmpty || document.visibilityState === "visible") { |
| 141 | void refreshRelayEnvironments().then(() => { |
| 142 | if (active) onDiscoveryReady?.(); |
| 143 | }); |
| 144 | } |
| 145 | return () => { |
| 146 | active = false; |
| 147 | }; |
| 148 | }, [refreshRelayEnvironments, refreshWhileEmpty, onDiscoveryReady]); |
| 149 | |
| 150 | const connectEnvironment = async (environment: RelayClientEnvironmentRecord) => { |
| 151 | if ( |
| 152 | discoveredCompatibilityError( |
| 153 | environmentsState.environments.get(environment.environmentId)?.status, |
| 154 | ) !== null |
| 155 | ) |
| 156 | return false; |
| 157 | setConnectingEnvironmentIds((current) => new Set([...current, environment.environmentId])); |
| 158 | const result = await connectRelayEnvironment(environment); |
| 159 | setConnectingEnvironmentIds((current) => { |
| 160 | const next = new Set(current); |
| 161 | next.delete(environment.environmentId); |
| 162 | return next; |
| 163 | }); |
| 164 | if (result._tag === "Success") { |
| 165 | toastManager.add({ |
| 166 | type: "success", |
| 167 | title: savedWithoutRelay.has(environment.environmentId) |
| 168 | ? "T3 Connect route added" |
| 169 | : "Environment added", |
| 170 | description: savedWithoutRelay.has(environment.environmentId) |
| 171 | ? `${environment.label} falls back to T3 Connect when its other routes are unreachable.` |
| 172 | : `Connecting to ${environment.label} through T3 Connect.`, |
| 173 | }); |
| 174 | return true; |
| 175 | } |
| 176 | if (isAtomCommandInterrupted(result)) { |
| 177 | return false; |
| 178 | } |
| 179 | const cause = squashAtomCommandFailure(result); |
| 180 | const message = |
| 181 | cause instanceof Error ? cause.message : "Could not connect the T3 Connect environment."; |
| 182 | const traceId = findErrorTraceId(cause); |
| 183 | console.error("[t3-connect] Could not connect environment", { message, traceId, cause }); |
| 184 | toastManager.add({ |
| 185 | type: "error", |
| 186 | title: "Could not connect environment", |
| 187 | description: message, |
| 188 | data: traceId |
| 189 | ? { |
| 190 | secondaryActionProps: { |
| 191 | children: "Copy trace ID", |
| 192 | onClick: () => void navigator.clipboard?.writeText(traceId), |
| 193 | }, |
| 194 | } |
| 195 | : undefined, |
| 196 | }); |
| 197 | return false; |
| 198 | }; |
| 199 | |
| 200 | // During onboarding selection a machine saved over another route already |
| 201 | // has its own row elsewhere, and selecting it must not add a T3 Connect |
| 202 | // route as a side effect, so it is left out here. |
| 203 | const visibleEnvironments = [...environmentsState.environments.values()].filter( |
| 204 | ({ environment }) => |
| 205 | environment.environmentId !== primaryEnvironmentId && |
| 206 | (showSavedEnvironments || !savedById.has(environment.environmentId)) && |
| 207 | !(selection && savedWithoutRelay.has(environment.environmentId)), |
| 208 | ); |
| 209 | const selectNewComputers = useEffectEvent(() => { |
| 210 | const seen = selection?.autoSelectedComputers; |
| 211 | if (!selection || !seen) return; |
| 212 | for (const { environment, status, availability } of visibleEnvironments) { |
| 213 | const id = environment.environmentId; |
| 214 | if (availability === "checking") continue; |
| 215 | if ( |
| 216 | discoveredCompatibilityError(status) !== null || |
| 217 | savedById.get(id)?.connection.phase === "unsupported" |
| 218 | ) { |
| 219 | seen.add(id); |
| 220 | if (selection.selectedIds.has(id)) selection.onChange(id, false); |
| 221 | continue; |
| 222 | } |
| 223 | if (seen.has(id)) continue; |
| 224 | seen.add(id); |
| 225 | selection.onChange(id, true); |
| 226 | if (!savedById.has(id)) { |
| 227 | void connectEnvironment(environment).then((connected) => { |
| 228 | if (!connected) selection.onChange(id, false); |
| 229 | }); |
| 230 | } |
| 231 | } |
| 232 | }); |
| 233 | useEffect(() => { |
| 234 | selectNewComputers(); |
| 235 | }, [environmentsState.environments]); |
| 236 | |
| 237 | // Discovery clears its list on refresh, so poll only until a machine appears. |
| 238 | const shouldRefreshWhileEmpty = |
| 239 | refreshWhileEmpty && visibleEnvironments.length === 0 && !environmentsState.offline; |
| 240 | |
| 241 | useEffect(() => { |
| 242 | if (!shouldRefreshWhileEmpty) return; |
| 243 | let timer: ReturnType<typeof setTimeout> | undefined; |
| 244 | let disposed = false; |
| 245 | let pending = false; |
| 246 | const visible = () => document.visibilityState === "visible"; |
| 247 | const schedule = () => { |
| 248 | clearTimeout(timer); |
| 249 | if (!disposed && visible()) { |
| 250 | timer = setTimeout(() => void refresh(), EMPTY_DISCOVERY_REFRESH_INTERVAL_MS); |
| 251 | } |
| 252 | }; |
| 253 | const refresh = async () => { |
| 254 | if (disposed || pending || !visible()) return; |
| 255 | clearTimeout(timer); |
| 256 | pending = true; |
| 257 | try { |
| 258 | await refreshDiscoveryWhenIdle(); |
| 259 | } finally { |
| 260 | pending = false; |
| 261 | schedule(); |
| 262 | } |
| 263 | }; |
| 264 | const onFocus = () => void refresh(); |
| 265 | const onVisibilityChange = () => { |
| 266 | clearTimeout(timer); |
| 267 | if (visible()) void refresh(); |
| 268 | }; |
| 269 | |
| 270 | schedule(); |
| 271 | window.addEventListener("focus", onFocus); |
| 272 | document.addEventListener("visibilitychange", onVisibilityChange); |
| 273 | return () => { |
| 274 | disposed = true; |
| 275 | clearTimeout(timer); |
| 276 | window.removeEventListener("focus", onFocus); |
| 277 | document.removeEventListener("visibilitychange", onVisibilityChange); |
| 278 | }; |
| 279 | }, [shouldRefreshWhileEmpty]); |
| 280 | |
| 281 | const standalone = showSavedEnvironments || savedEnvironments.length === 0; |
| 282 | |
| 283 | if ( |
| 284 | !refreshWhileEmpty && |
| 285 | standalone && |
| 286 | visibleEnvironments.length === 0 && |
| 287 | environmentsState.refreshing && |
| 288 | environmentsState.environments.size === 0 |
| 289 | ) { |
| 290 | return <RemoteEnvironmentRowsSkeleton />; |
| 291 | } |
| 292 | |
| 293 | if (standalone && visibleEnvironments.length === 0) { |
| 294 | // A failed or offline discovery is not "no environments" — misreporting it |
| 295 | // as empty would read as the user's devices having disappeared. |
| 296 | const discoveryProblem = environmentsState.offline |
| 297 | ? "You appear to be offline." |
| 298 | : (Option.getOrNull(environmentsState.error)?.message ?? null); |
| 299 | if (discoveryProblem !== null && !environmentsState.refreshing) { |
| 300 | return ( |
| 301 | <div className={ITEM_ROW_CLASSNAME}> |
| 302 | <p className="text-sm font-medium text-destructive"> |
| 303 | Could not load T3 Connect environments |
| 304 | </p> |
| 305 | <p className="mt-1 text-xs text-muted-foreground">{discoveryProblem}</p> |
| 306 | <Button |
| 307 | size="sm" |
| 308 | variant="outline" |
| 309 | className="mt-3" |
| 310 | onClick={() => void refreshRelayEnvironments()} |
| 311 | > |
| 312 | Try again |
| 313 | </Button> |
| 314 | </div> |
| 315 | ); |
| 316 | } |
| 317 | return empty; |
| 318 | } |
| 319 | |
| 320 | return visibleEnvironments.map(({ environment, availability, error, status }) => { |
| 321 | const savedEnvironment = savedById.get(environment.environmentId); |
| 322 | const compatibilityError = discoveredCompatibilityError(status); |
| 323 | const unsupported = |
| 324 | compatibilityError !== null || savedEnvironment?.connection.phase === "unsupported"; |
| 325 | const unsupportedDetail = |
| 326 | compatibilityError?.message ?? savedEnvironment?.connection.error ?? null; |
| 327 | const savedConnection = unsupported |
| 328 | ? presentSavedCloudEnvironmentConnection({ |
| 329 | phase: "unsupported", |
| 330 | error: unsupportedDetail, |
| 331 | traceId: null, |
| 332 | }) |
| 333 | : savedEnvironment |
| 334 | ? presentSavedCloudEnvironmentConnection(savedEnvironment.connection) |
| 335 | : null; |
| 336 | // A connected machine's own config (with the user's icon pick) wins. Before |
| 337 | // that, the relay's health probe already carries the server's descriptor, so |
| 338 | // a machine can wear its detected glyph before this device ever connects. |
| 339 | const relayStatus = status === undefined ? null : Option.getOrNull(status); |
| 340 | const descriptor = relayStatus?.descriptor; |
| 341 | // Why the relay reports this environment offline, when it knows more than |
| 342 | // "no answer". Shown for saved and unsaved rows alike. |
| 343 | const offlineReason = |
| 344 | availability === "offline" && relayStatus !== null |
| 345 | ? relayOfflineReasonMessage(relayStatus) |
| 346 | : null; |
| 347 | const machineKind = resolveEnvironmentMachineKind( |
| 348 | savedEnvironment?.serverConfig ?? |
| 349 | (descriptor === undefined ? null : { environment: descriptor }), |
| 350 | ); |
| 351 | const dotClassName = savedConnection |
| 352 | ? savedConnection.tone === "connected" |
| 353 | ? "bg-success" |
| 354 | : savedConnection.tone === "connecting" |
| 355 | ? "bg-warning" |
| 356 | : savedConnection.tone === "error" |
| 357 | ? "bg-destructive" |
| 358 | : "bg-muted-foreground/35" |
| 359 | : availability === "online" |
| 360 | ? "bg-success" |
| 361 | : availability === "error" |
| 362 | ? "bg-destructive" |
| 363 | : availability === "checking" |
| 364 | ? "bg-warning" |
| 365 | : "bg-muted-foreground/35"; |
| 366 | const notAdded = savedWithoutRelay.has(environment.environmentId) |
| 367 | ? "Saved without T3 Connect" |
| 368 | : "Not added"; |
| 369 | const statusText = |
| 370 | unsupported && !savedEnvironment |
| 371 | ? `T3 Connect · ${notAdded} · Client not supported` |
| 372 | : offlineReason !== null |
| 373 | ? offlineReason |
| 374 | : savedConnection |
| 375 | ? savedConnection.statusText |
| 376 | : availability === "online" |
| 377 | ? `T3 Connect · ${notAdded} · Relay online` |
| 378 | : availability === "offline" |
| 379 | ? `T3 Connect · ${notAdded} · Relay offline` |
| 380 | : availability === "checking" |
| 381 | ? `T3 Connect · ${notAdded} · Checking relay status…` |
| 382 | : (Option.getOrNull(error)?.message ?? |
| 383 | `T3 Connect · ${notAdded} · Relay status unavailable`); |
| 384 | if (selection) { |
| 385 | return ( |
| 386 | <label |
| 387 | key={environment.environmentId} |
| 388 | className="flex cursor-pointer items-center gap-3 rounded-lg border border-border bg-background px-3 py-2.5 has-disabled:cursor-default" |
| 389 | > |
| 390 | <Checkbox |
| 391 | checked={!unsupported && selection.selectedIds.has(environment.environmentId)} |
| 392 | disabled={unsupported || connectingEnvironmentIds.has(environment.environmentId)} |
| 393 | onCheckedChange={async (checked) => { |
| 394 | if (unsupported) return; |
| 395 | selection.onChange(environment.environmentId, checked); |
| 396 | if (checked && !savedEnvironment) { |
| 397 | const connected = await connectEnvironment(environment); |
| 398 | if (!connected) selection.onChange(environment.environmentId, false); |
| 399 | } |
| 400 | }} |
| 401 | /> |
| 402 | <EnvironmentMachineIcon |
| 403 | aria-hidden |
| 404 | kind={machineKind} |
| 405 | className="size-4 shrink-0 text-muted-foreground" |
| 406 | /> |
| 407 | <span className="min-w-0 flex-1 truncate text-sm font-medium">{environment.label}</span> |
| 408 | <Tooltip> |
| 409 | <TooltipTrigger |
| 410 | render={<span tabIndex={0} />} |
| 411 | className={cn( |
| 412 | "max-w-[45%] shrink-0 truncate text-xs", |
| 413 | savedConnection?.tone === "error" ? "text-destructive" : "text-muted-foreground", |
| 414 | )} |
| 415 | > |
| 416 | {connectingEnvironmentIds.has(environment.environmentId) |
| 417 | ? "Connecting…" |
| 418 | : (savedConnection?.buttonLabel ?? |
| 419 | (availability === "online" |
| 420 | ? "Available" |
| 421 | : availability === "offline" |
| 422 | ? "Offline" |
| 423 | : availability === "error" |
| 424 | ? "Unavailable" |
| 425 | : "Checking…"))} |
| 426 | </TooltipTrigger> |
| 427 | <TooltipPopup>{unsupportedDetail ?? statusText}</TooltipPopup> |
| 428 | </Tooltip> |
| 429 | </label> |
| 430 | ); |
| 431 | } |
| 432 | return ( |
| 433 | <div key={environment.environmentId} className={ITEM_ROW_CLASSNAME}> |
| 434 | <div className={ITEM_ROW_INNER_CLASSNAME}> |
| 435 | <div className="min-w-0"> |
| 436 | <div className="flex items-center gap-2"> |
| 437 | <ConnectionStatusDot |
| 438 | dotClassName={dotClassName} |
| 439 | pingClassName={ |
| 440 | savedConnection?.tone === "connecting" || |
| 441 | (savedConnection === null && availability === "checking") |
| 442 | ? "bg-warning/60 duration-2000" |
| 443 | : null |
| 444 | } |
| 445 | tooltipText={ |
| 446 | unsupportedDetail !== null |
| 447 | ? unsupportedDetail |
| 448 | : offlineReason !== null |
| 449 | ? offlineReason |
| 450 | : savedConnection |
| 451 | ? savedConnection.statusText |
| 452 | : availability === "online" |
| 453 | ? "Relay online" |
| 454 | : availability === "offline" |
| 455 | ? "Relay offline" |
| 456 | : availability === "checking" |
| 457 | ? "Checking relay status" |
| 458 | : (Option.getOrNull(error)?.message ?? "Relay status unavailable") |
| 459 | } |
| 460 | /> |
| 461 | <EnvironmentMachineIcon |
| 462 | aria-hidden |
| 463 | kind={machineKind} |
| 464 | className="size-4 shrink-0 text-muted-foreground" |
| 465 | /> |
| 466 | <p className="truncate text-sm font-medium">{environment.label}</p> |
| 467 | </div> |
| 468 | <p |
| 469 | className={cn( |
| 470 | "mt-1 truncate text-xs", |
| 471 | savedConnection?.tone === "error" || |
| 472 | (savedConnection?.tone === "connecting" && savedEnvironment?.connection.error) || |
| 473 | (savedConnection === null && availability === "error") |
| 474 | ? "text-destructive" |
| 475 | : "text-muted-foreground", |
| 476 | )} |
| 477 | > |
| 478 | {statusText} |
| 479 | </p> |
| 480 | </div> |
| 481 | {unsupported && !savedEnvironment ? ( |
| 482 | <Tooltip> |
| 483 | <TooltipTrigger render={<span className="inline-flex" tabIndex={0} />}> |
| 484 | <Button size="sm" disabled> |
| 485 | Add |
| 486 | </Button> |
| 487 | </TooltipTrigger> |
| 488 | <TooltipPopup>{unsupportedDetail ?? "Client not supported"}</TooltipPopup> |
| 489 | </Tooltip> |
| 490 | ) : savedConnection ? ( |
| 491 | <Button size="sm" variant="outline" disabled> |
| 492 | {savedConnection.buttonLabel} |
| 493 | </Button> |
| 494 | ) : ( |
| 495 | <Button |
| 496 | size="sm" |
| 497 | disabled={connectingEnvironmentIds.size > 0} |
| 498 | onClick={() => void connectEnvironment(environment)} |
| 499 | > |
| 500 | {connectingEnvironmentIds.has(environment.environmentId) |
| 501 | ? "Adding…" |
| 502 | : savedWithoutRelay.has(environment.environmentId) |
| 503 | ? "Add route" |
| 504 | : "Add"} |
| 505 | </Button> |
| 506 | )} |
| 507 | </div> |
| 508 | </div> |
| 509 | ); |
| 510 | }); |
| 511 | } |