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

BranchToolbarEnvironmentSelector.tsx

241 lines8.2 KB

1import { ComposerControl, ComposerSelectControl } from "./chat/ComposerControl";
2import { ComposerContextLabel } from "./ComposerContextLabel";
3import { Tooltip, TooltipTrigger, TooltipPopup } from "./ui/tooltip";
4import { useAtomValue } from "@effect/atom-react";
5import { shortcutLabelForCommand } from "../keybindings";
6import { primaryServerKeybindingsAtom } from "../state/server";
7import type { EnvironmentId } from "@t3tools/contracts";
8import { CircleArrowUpIcon, ScaleIcon } from "lucide-react";
9import { memo } from "react";
10​
11import type { EnvironmentOption } from "./BranchToolbar.logic";
12import {
13 ServerUpdateAction,
14 type PendingServerUpdate,
15 type ServerUpdateControl,
16} from "./ServerUpdateAction";
17import { EnvironmentMachineIcon } from "./EnvironmentMachineIcon";
18import { useComposerMenuProps } from "./chat/composerEventScope";
19import {
20 Select,
21 SelectGroup,
22 SelectGroupLabel,
23 SelectItem,
24 SelectPopup,
25 SelectValue,
26} from "./ui/select";
27​
28interface BranchToolbarEnvironmentSelectorProps {
29 autoEnvironmentLabel?: string | undefined;
30 onAutoEnvironment?: (() => void) | undefined;
31 envLocked: boolean;
32 environmentId: EnvironmentId;
33 availableEnvironments: readonly EnvironmentOption[];
34 onEnvironmentChange?: (environmentId: EnvironmentId) => void;
35 serverUpdate?: PendingServerUpdate | null;
37​
38// Action values cannot collide with server-minted environment IDs.
39const AUTO_VALUE = "action:auto";
40const UPDATE_VALUE = "action:update";
41const environmentValue = (id: EnvironmentId) => `env:${id}`;
42​
43export const BranchToolbarEnvironmentSelector = memo(function BranchToolbarEnvironmentSelector(
44 props: BranchToolbarEnvironmentSelectorProps,
45) {
46 return props.serverUpdate ? (
47 <ServerUpdateAction
48 {...props.serverUpdate}
49 render={(updateControl) => <EnvironmentSelector {...props} updateControl={updateControl} />}
50 />
51 ) : (
52 <EnvironmentSelector {...props} updateControl={null} />
53 );
54});
55​
56function EnvironmentSelector({
57 autoEnvironmentLabel,
58 onAutoEnvironment,
59 envLocked,
60 environmentId,
61 availableEnvironments,
62 onEnvironmentChange,
63 serverUpdate,
64 updateControl,
65}: BranchToolbarEnvironmentSelectorProps & {
66 updateControl: ServerUpdateControl | null;
67}) {
68 const composerFloatingLayerProps = useComposerMenuProps();
69 const shortcutLabel = shortcutLabelForCommand(
70 useAtomValue(primaryServerKeybindingsAtom),
71 "composer.host",
72 );
73 const activeEnvironment =
74 availableEnvironments.find((env) => env.environmentId === environmentId) ?? null;
75​
76 const environmentItems = [
77 ...(onAutoEnvironment
78 ? [{ value: AUTO_VALUE, label: autoEnvironmentLabel ?? "Auto balance" }]
79 : []),
80 ...availableEnvironments.map((env) => ({
81 value: environmentValue(env.environmentId),
82 label: env.label,
83 })),
84 ...(serverUpdate && updateControl
85 ? [{ value: UPDATE_VALUE, label: `${updateControl.label} ${serverUpdate.serverLabel}` }]
86 : []),
87 ];
88 const updateTooltip = serverUpdate
89 ? `${serverUpdate.serverLabel} ${serverUpdate.serverVersion} → ${serverUpdate.targetVersion}. ${
90 serverUpdate.selfUpdate === "desktop-managed" && !serverUpdate.desktopAppUpdate
91 ? "Update the desktop app on that machine."
92 : (updateControl?.label ?? "Update")
93 }`
94 : null;
95 const environmentIcon = serverUpdate ? (
96 <CircleArrowUpIcon aria-hidden="true" className="size-3 shrink-0" />
97 ) : (
98 <EnvironmentMachineIcon
99 kind={activeEnvironment?.machine ?? "server"}
100 className="size-3 shrink-0"
101 />
102 );
103 const chipBody = (
104 <>
105 {environmentIcon}
106 <ComposerContextLabel>
107 {activeEnvironment?.label ?? serverUpdate?.serverLabel ?? "Run on"}
108 </ComposerContextLabel>
109 </>
110 );
111​
112 // The static label carries the xs control's height (h-7 sm:h-6) as well as
113 // its padding: the composer context strip has no min-height of its own, and
114 // the glass seam joining it to the composer assumes a fixed strip height, so
115 // a shorter label would drag the seam out of line whenever this label is the
116 // only thing in the strip.
117 if (envLocked || onEnvironmentChange === undefined) {
118 const lockedRow = updateControl ? (
119 <ComposerControl
120 size="xs"
121 className="min-w-0 max-w-full"
122 aria-label={`${updateControl.label} ${serverUpdate?.serverLabel}`}
123 disabled={updateControl.disabled}
124 onClick={updateControl.onClick}
125 data-composer-context-control
126 >
127 {chipBody}
128 </ComposerControl>
129 ) : (
130 <span
131 className="inline-flex h-7 min-w-0 max-w-full items-center gap-1 border border-transparent px-1.75 font-normal text-muted-foreground/70 text-xs sm:h-6"
132 data-composer-context-control
133 >
134 {chipBody}
135 </span>
136 );
137 return (
138 <Tooltip>
139 <TooltipTrigger
140 render={
141 updateControl?.disabled ? (
142 <span className="inline-flex min-w-0 max-w-full" data-composer-context-control>
143 {lockedRow}
144 </span>
145 ) : (
146 lockedRow
147 )
148 }
149 />
150 <TooltipPopup role={serverUpdate ? "tooltip" : undefined}>
151 {updateTooltip ?? activeEnvironment?.label ?? "Run on"}
152 </TooltipPopup>
153 </Tooltip>
154 );
155 }
156​
157 return (
158 <Select
159 modal={false}
160 value={autoEnvironmentLabel ? AUTO_VALUE : environmentValue(environmentId)}
161 onValueChange={(value) => {
162 if (value === UPDATE_VALUE) {
163 updateControl?.onClick();
164 } else if (value === AUTO_VALUE) {
165 onAutoEnvironment?.();
166 } else if (value?.startsWith("env:")) {
167 onEnvironmentChange(value.slice(4) as EnvironmentId);
168 }
169 }}
170 items={environmentItems}
171 >
172 <Tooltip>
173 <TooltipTrigger
174 render={
175 <ComposerSelectControl
176 size="xs"
177 className="min-w-0 max-w-full"
178 aria-label="Run on"
179 data-composer-shortcut="composer.host"
180 data-composer-context-control
181 />
182 }
183 >
184 {autoEnvironmentLabel ? (
185 <ScaleIcon className="size-3 shrink-0" aria-hidden="true" />
186 ) : (
187 environmentIcon
188 )}
189 <ComposerContextLabel>
190 <SelectValue />
191 </ComposerContextLabel>
192 </TooltipTrigger>
193 <TooltipPopup role={serverUpdate ? "tooltip" : undefined}>
194 {updateTooltip ?? autoEnvironmentLabel ?? activeEnvironment?.label ?? "Run on"}
195 {shortcutLabel ? ` · ${shortcutLabel}` : null}
196 </TooltipPopup>
197 </Tooltip>
198 <SelectPopup alignItemWithTrigger={false} {...composerFloatingLayerProps}>
199 <SelectGroup>
200 <SelectGroupLabel>Run on</SelectGroupLabel>
201 {onAutoEnvironment && (
202 <SelectItem
203 value={AUTO_VALUE}
204 onClick={() => {
205 if (autoEnvironmentLabel) onAutoEnvironment?.();
206 }}
207 >
208 <span className="inline-flex items-center gap-1.5">
209 <ScaleIcon className="size-3" aria-hidden="true" />
210 {autoEnvironmentLabel ?? "Auto balance"}
211 </span>
212 </SelectItem>
213 )}
214 {availableEnvironments.map((env) => (
215 <SelectItem key={env.environmentId} value={environmentValue(env.environmentId)}>
216 <span className="inline-flex items-center gap-1.5">
217 {serverUpdate?.environmentId === env.environmentId ? (
218 <CircleArrowUpIcon aria-hidden="true" className="size-3" />
219 ) : (
220 <EnvironmentMachineIcon kind={env.machine} className="size-3" />
221 )}
222 {env.label}
223 </span>
224 </SelectItem>
225 ))}
226 </SelectGroup>
227 {serverUpdate && updateControl ? (
228 <SelectGroup>
229 <SelectGroupLabel>Server update</SelectGroupLabel>
230 <SelectItem value={UPDATE_VALUE} disabled={updateControl.disabled}>
231 <span className="inline-flex items-center gap-1.5">
232 <CircleArrowUpIcon aria-hidden="true" className="size-3" />
233 {updateControl.label} {serverUpdate.serverLabel}
234 </span>
235 </SelectItem>
236 </SelectGroup>
237 ) : null}
238 </SelectPopup>
239 </Select>
240 );