Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

EnvironmentRow.tsx

118 lines4.0 KB

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