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

EnvironmentRow.tsx

93 lines3.1 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 { isDesktopLocalConnectionTarget } from "~/connection/desktopLocal";
14import { EnvironmentMachineIcon } from "../EnvironmentMachineIcon";
15​
16export function formatDesktopSshTarget(target: DesktopSshEnvironmentTarget): string {
17 const authority = target.username ? `${target.username}@${target.hostname}` : target.hostname;
18 return target.port ? `${authority}:${target.port}` : authority;
20​
21export function environmentTransportLabel(
22 environment: EnvironmentPresentation,
23 activeTarget: ConnectionTarget | null = null,
24): string {
25 const { entry } = environment;
26 if (entry.target._tag === "PrimaryConnectionTarget") return "This machine";
27 const routes = connectionRoutes(entry);
28 if (routes.length > 1) {
29 const active =
30 activeTarget === null
31 ? undefined
32 : routes.find(
33 (route) => connectionRouteId(route.target) === connectionRouteId(activeTarget),
34 );
35 return active === undefined
36 ? connectionRouteLabel(routes[0]!)
37 : `via ${connectionRouteLabel(active)}`;
38 }
39 if (environment.relayManaged) return "T3 Connect";
40 if (isDesktopLocalConnectionTarget(entry.target)) return "WSL";
41 if (
42 entry.target._tag === "SshConnectionTarget" &&
43 Option.isSome(entry.profile) &&
44 entry.profile.value._tag === "SshConnectionProfile"
45 ) {
46 return `SSH ${formatDesktopSshTarget(entry.profile.value.target)}`;
47 }
48 return environment.displayUrl ?? "Remote link";
50​
51export function EnvironmentRow({
52 kind,
53 label,
54 subtitle,
55 below,
56 detail,
57 dimmed = false,
58 className,
59 children,
60}: {
61 readonly kind: EnvironmentMachineKind;
62 readonly label: string;
63 readonly subtitle: ReactNode;
64 readonly below?: ReactNode;
65 readonly detail?: ReactNode;
66 readonly dimmed?: boolean;
67 readonly className?: string;
68 readonly children?: ReactNode;
69}) {
70 return (
71 <div
72 className={cn(
73 "grid grid-cols-[auto_minmax(0,1fr)_auto] items-center gap-x-3 px-3 py-2.5 sm:px-4",
74 className,
75 )}
76 >
77 {/* Dim only the identity so a switched-off row's controls stay readable
78 and distinct from disabled ones. */}
79 <EnvironmentMachineIcon
80 aria-hidden
81 kind={kind}
82 className={cn("size-4 text-muted-foreground", dimmed && "opacity-60")}
83 />
84 <div className={cn("min-w-0", dimmed && "opacity-60")}>
85 <p className="truncate text-sm font-medium text-foreground">{label}</p>
86 <div className="truncate text-xs text-muted-foreground">{subtitle}</div>
87 {below}
88 </div>
89 <div className="flex shrink-0 items-center gap-1">{children}</div>
90 {detail ? <div className="col-span-2 col-start-2 min-w-0">{detail}</div> : null}
91 </div>
92 );