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

EnvironmentRow.tsx

109 lines3.6 KB

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