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

EnvironmentIconPicker.tsx

161 lines6.0 KB

1import {
2 ENVIRONMENT_MACHINE_KINDS,
3 isEnvironmentMachineKind,
4 resolveEnvironmentMachineKind,
5 type EnvironmentId,
6 type ServerConfig,
7} from "@t3tools/contracts";
8import { useCallback } from "react";
9​
10import { isElectron } from "../../env";
11import { usePrimarySessionState } from "../../environments/primary";
12import { useUpdateEnvironmentSettings } from "../../hooks/useSettings";
13import { usePrimaryEnvironmentId } from "../../state/environments";
14import { useEnvironmentSessionState } from "../../state/session";
15import { ENVIRONMENT_MACHINE_KIND_LABELS, EnvironmentMachineIcon } from "../EnvironmentMachineIcon";
16import { Select, SelectItem, SelectPopup, SelectTrigger, SelectValue } from "../ui/select";
17import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
18import {
19 resolvePrimaryOperateAccess,
20 resolveRemoteOperateAccess,
21} from "./ProviderSettingsPanel.logic";
22​
23const AUTOMATIC_VALUE = "automatic";
24​
25/**
26 * Why the picker is inert, in the order the user can do something about it.
27 * Null means it can be changed.
28 */
29export function resolveEnvironmentIconPickerLock(input: {
30 readonly serverConfig: ServerConfig | null;
31 readonly operateAccess: "granted" | "denied" | "pending";
32}): string | null {
33 if (input.serverConfig === null) {
34 return "Connect to this environment to change its icon.";
35 }
36 if (input.serverConfig.environment.capabilities.environmentIcon !== true) {
37 return "This environment's server is too old to keep an icon. Update it to choose one.";
38 }
39 if (input.operateAccess === "denied") {
40 return "Your session on this environment cannot change its settings.";
41 }
42 return null;
44​
45// Same split the provider settings use: the desktop app owns its primary
46// server outright, a browser session on the primary checks its cookie
47// session's scopes, and a remote checks the scopes its own server reports.
48function useEnvironmentOperateAccess(environmentId: EnvironmentId) {
49 const isPrimary = usePrimaryEnvironmentId() === environmentId;
50 const primarySession = usePrimarySessionState();
51 const remoteSession = useEnvironmentSessionState(environmentId);
52 if (isPrimary) {
53 return isElectron
54 ? "granted"
55 : resolvePrimaryOperateAccess({
56 isPrimary: true,
57 hasDesktopBridge: false,
58 session: primarySession.data,
59 isPending: primarySession.isPending,
60 hasError: primarySession.error !== null,
61 });
62 }
63 return resolveRemoteOperateAccess({
64 session: remoteSession.data,
65 isPending: remoteSession.isPending,
66 hasError: remoteSession.hasError,
67 });
69​
70/**
71 * Picks the machine glyph an environment wears everywhere it is listed.
72 * "Automatic" clears the override so the server's own detection shows
73 * through; the label says what that currently resolves to so the user can
74 * tell whether detection got it right before overriding. The control stays
75 * visible while locked so the current icon still reads, the same way
76 * server-scoped rows go inert instead of disappearing.
77 */
78export function EnvironmentIconPicker({
79 environmentId,
80 serverConfig,
81 size = "sm",
82}: {
83 readonly environmentId: EnvironmentId;
84 readonly serverConfig: ServerConfig | null;
85 readonly size?: "xs" | "sm";
86}) {
87 const updateSettings = useUpdateEnvironmentSettings(environmentId);
88 const operateAccess = useEnvironmentOperateAccess(environmentId);
89 const lock = resolveEnvironmentIconPickerLock({ serverConfig, operateAccess });
90 const override = serverConfig?.settings.environmentIcon ?? null;
91 const detected = serverConfig?.environment.platform.machine ?? null;
92 const resolved = resolveEnvironmentMachineKind(serverConfig);
93 const value = override ?? AUTOMATIC_VALUE;
94 const automaticLabel =
95 detected === null ? "Automatic" : `Automatic (${ENVIRONMENT_MACHINE_KIND_LABELS[detected]})`;
96​
97 const handleValueChange = useCallback(
98 (next: string | null) => {
99 if (next === null) return;
100 if (next === AUTOMATIC_VALUE) {
101 updateSettings({ environmentIcon: null });
102 } else if (isEnvironmentMachineKind(next)) {
103 updateSettings({ environmentIcon: next });
104 }
105 },
106 [updateSettings],
107 );
108​
109 const select = (
110 <Select value={value} onValueChange={handleValueChange} disabled={lock !== null}>
111 <SelectTrigger size={size} className="w-full sm:w-52" aria-label="Environment icon">
112 <SelectValue>
113 <span className="flex min-w-0 items-center gap-2">
114 <EnvironmentMachineIcon kind={resolved} className="size-3.5 shrink-0" />
115 <span className="truncate">
116 {override === null ? automaticLabel : ENVIRONMENT_MACHINE_KIND_LABELS[override]}
117 </span>
118 </span>
119 </SelectValue>
120 </SelectTrigger>
121 <SelectPopup align="end" alignItemWithTrigger={false}>
122 <SelectItem hideIndicator value={AUTOMATIC_VALUE}>
123 <span className="flex min-w-0 items-center gap-2">
124 <EnvironmentMachineIcon kind={detected ?? "server"} className="size-3.5 shrink-0" />
125 <span className="truncate">{automaticLabel}</span>
126 </span>
127 </SelectItem>
128 {ENVIRONMENT_MACHINE_KINDS.map((kind) => (
129 <SelectItem hideIndicator key={kind} value={kind}>
130 <span className="flex min-w-0 items-center gap-2">
131 <EnvironmentMachineIcon kind={kind} className="size-3.5 shrink-0" />
132 <span className="truncate">{ENVIRONMENT_MACHINE_KIND_LABELS[kind]}</span>
133 </span>
134 </SelectItem>
135 ))}
136 </SelectPopup>
137 </Select>
138 );
139​
140 if (lock === null) {
141 return select;
142 }
143 return (
144 <Tooltip>
145 <TooltipTrigger
146 render={
147 // Focusable so keyboard users can still reach the explanation.
148 <span
149 tabIndex={0}
150 className="flex w-full items-center rounded-md outline-none focus-visible:ring-2 focus-visible:ring-ring sm:w-auto"
151 />
152 }
153 >
154 <span className="flex w-full items-center sm:w-auto">{select}</span>
155 </TooltipTrigger>
156 <TooltipPopup side="top" className="max-w-72">
157 {lock}
158 </TooltipPopup>
159 </Tooltip>
160 );