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 | type ConnectionTarget, |
| 3 | connectionRouteId, |
| 4 | connectionRouteLabelText, |
| 5 | connectionRoutes, |
| 6 | } from "@t3tools/client-runtime/connection"; |
| 7 | import type { DesktopSshEnvironmentTarget, EnvironmentMachineKind } from "@t3tools/contracts"; |
| 8 | import * as Option from "effect/Option"; |
| 9 | import { msg, useMessages } from "gt-react"; |
| 10 | import type { ReactNode } from "react"; |
| 11 | |
| 12 | import { cn } from "~/lib/utils"; |
| 13 | import type { EnvironmentPresentation } from "~/state/environments"; |
| 14 | import { usePreparedConnection } from "~/state/session"; |
| 15 | import { isDesktopLocalConnectionTarget } from "~/connection/desktopLocal"; |
| 16 | import { EnvironmentMachineIcon } from "../EnvironmentMachineIcon"; |
| 17 | import { m, type MessageResolver } from "../../i18n/messages"; |
| 18 | |
| 19 | const T3_CONNECT_NAME = "T3 Connect"; |
| 20 | |
| 21 | export function formatDesktopSshTarget(target: DesktopSshEnvironmentTarget): string { |
| 22 | const authority = target.username ? `${target.username}@${target.hostname}` : target.hostname; |
| 23 | return target.port ? `${authority}:${target.port}` : authority; |
| 24 | } |
| 25 | |
| 26 | export function environmentTransportLabel( |
| 27 | environment: EnvironmentPresentation, |
| 28 | activeTarget: ConnectionTarget | null = null, |
| 29 | resolve: MessageResolver = m, |
| 30 | ): string { |
| 31 | const { entry } = environment; |
| 32 | if (entry.target._tag === "PrimaryConnectionTarget") return resolve(msg("This machine")); |
| 33 | const routes = connectionRoutes(entry); |
| 34 | if (routes.length > 1) { |
| 35 | const active = |
| 36 | activeTarget === null |
| 37 | ? undefined |
| 38 | : routes.find( |
| 39 | (route) => connectionRouteId(route.target) === connectionRouteId(activeTarget), |
| 40 | ); |
| 41 | return active === undefined |
| 42 | ? connectionRouteLabelText(routes[0]!, resolve) |
| 43 | : resolve(msg("via {route}", { route: connectionRouteLabelText(active, resolve) })); |
| 44 | } |
| 45 | if (environment.relayManaged) return T3_CONNECT_NAME; |
| 46 | if (isDesktopLocalConnectionTarget(entry.target)) return "WSL"; |
| 47 | if ( |
| 48 | entry.target._tag === "SshConnectionTarget" && |
| 49 | Option.isSome(entry.profile) && |
| 50 | entry.profile.value._tag === "SshConnectionProfile" |
| 51 | ) { |
| 52 | return `SSH ${formatDesktopSshTarget(entry.profile.value.target)}`; |
| 53 | } |
| 54 | return environment.displayUrl ?? resolve(msg("Remote link")); |
| 55 | } |
| 56 | |
| 57 | export function useEnvironmentTransportLabel(environment: EnvironmentPresentation): string { |
| 58 | const prepared = usePreparedConnection(environment.environmentId); |
| 59 | const resolve = useMessages(); |
| 60 | return environmentTransportLabel( |
| 61 | environment, |
| 62 | environment.connection.phase === "connected" && prepared._tag === "Some" |
| 63 | ? prepared.value.target |
| 64 | : null, |
| 65 | resolve, |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | export function EnvironmentTransportLabel(props: { |
| 70 | readonly environment: EnvironmentPresentation; |
| 71 | }) { |
| 72 | return useEnvironmentTransportLabel(props.environment); |
| 73 | } |
| 74 | |
| 75 | export function EnvironmentRow({ |
| 76 | kind, |
| 77 | label, |
| 78 | subtitle, |
| 79 | below, |
| 80 | detail, |
| 81 | dimmed = false, |
| 82 | className, |
| 83 | children, |
| 84 | }: { |
| 85 | readonly kind: EnvironmentMachineKind; |
| 86 | readonly label: string; |
| 87 | readonly subtitle: ReactNode; |
| 88 | readonly below?: ReactNode; |
| 89 | readonly detail?: ReactNode; |
| 90 | readonly dimmed?: boolean; |
| 91 | readonly className?: string; |
| 92 | readonly children?: ReactNode; |
| 93 | }) { |
| 94 | return ( |
| 95 | <div |
| 96 | className={cn( |
| 97 | "grid grid-cols-[auto_minmax(0,1fr)_auto] items-center gap-x-3 px-3 py-2.5 sm:px-4", |
| 98 | className, |
| 99 | )} |
| 100 | > |
| 101 | {/* Dim only identity so switched-off controls remain readable and distinct from disabled controls. */} |
| 102 | <EnvironmentMachineIcon |
| 103 | aria-hidden |
| 104 | kind={kind} |
| 105 | className={cn("size-4 text-muted-foreground", dimmed && "opacity-60")} |
| 106 | /> |
| 107 | <div className={cn("min-w-0", dimmed && "opacity-60")}> |
| 108 | <p className="truncate text-sm font-medium text-foreground">{label}</p> |
| 109 | <div className="truncate text-xs text-muted-foreground">{subtitle}</div> |
| 110 | {below} |
| 111 | </div> |
| 112 | <div className="flex shrink-0 items-center gap-1">{children}</div> |
| 113 | {detail ? <div className="col-span-2 col-start-2 min-w-0">{detail}</div> : null} |
| 114 | </div> |
| 115 | ); |
| 116 | } |