Open History to see the last commit
1import { 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 { PlusIcon, ScaleIcon } from "lucide-react";
9import { memo } from "react";
10
11import { ADD_TO_MACHINE_LABEL_CLASS, type EnvironmentOption } from "./BranchToolbar.logic";
12import { EnvironmentMachineIcon } from "./EnvironmentMachineIcon";
13import { useComposerMenuProps } from "./chat/composerEventScope";
14import {
15 Select,
16 SelectGroup,
17 SelectGroupLabel,
18 SelectItem,
19 SelectPopup,
20 SelectValue,
21} from "./ui/select";
22
23interface BranchToolbarEnvironmentSelectorProps {
24 autoEnvironmentLabel?: string | undefined;
25 onAutoEnvironment?: (() => void) | undefined;
26 envLocked: boolean;
27 environmentId: EnvironmentId;
28 availableEnvironments: readonly EnvironmentOption[];
29 onEnvironmentChange?: (environmentId: EnvironmentId) => void;
30 onAddToMachine?: (() => void) | undefined;
31}
32
33// Action values cannot collide with server-minted environment IDs.
34const AUTO_VALUE = "action:auto";
35const ADD_MACHINE_VALUE = "action:add-machine";
36const environmentValue = (id: EnvironmentId) => `env:${id}`;
37
38export const BranchToolbarEnvironmentSelector = memo(function BranchToolbarEnvironmentSelector({
39 autoEnvironmentLabel,
40 onAutoEnvironment,
41 envLocked,
42 environmentId,
43 availableEnvironments,
44 onEnvironmentChange,
45 onAddToMachine,
46}: BranchToolbarEnvironmentSelectorProps) {
47 const composerFloatingLayerProps = useComposerMenuProps();
48 const shortcutLabel = shortcutLabelForCommand(
49 useAtomValue(primaryServerKeybindingsAtom),
50 "composer.host",
51 );
52 const activeEnvironment =
53 availableEnvironments.find((env) => env.environmentId === environmentId) ?? null;
54
55 const environmentItems = [
56 ...(onAutoEnvironment
57 ? [{ value: AUTO_VALUE, label: autoEnvironmentLabel ?? "Auto balance" }]
58 : []),
59 ...availableEnvironments.map((env) => ({
60 value: environmentValue(env.environmentId),
61 label: env.label,
62 })),
63 ...(onAddToMachine ? [{ value: ADD_MACHINE_VALUE, label: "Add to another machine" }] : []),
64 ];
65
66 // The static label carries the xs control's height (h-7 sm:h-6) as well as
67 // its padding: the composer context strip has no min-height of its own, and
68 // the glass seam joining it to the composer assumes a fixed strip height, so
69 // a shorter label would drag the seam out of line whenever this label is the
70 // only thing in the strip.
71 if (envLocked || (onEnvironmentChange === undefined && onAddToMachine === undefined)) {
72 const lockedRow = (
73 <span
74 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"
75 data-composer-context-control
76 >
77 <EnvironmentMachineIcon
78 kind={activeEnvironment?.machine ?? "server"}
79 className="size-3 shrink-0"
80 />
81 <ComposerContextLabel>{activeEnvironment?.label ?? "Run on"}</ComposerContextLabel>
82 </span>
83 );
84 return (
85 <Tooltip>
86 <TooltipTrigger render={lockedRow} />
87 <TooltipPopup>{activeEnvironment?.label ?? "Run on"}</TooltipPopup>
88 </Tooltip>
89 );
90 }
91
92 return (
93 <Select
94 modal={false}
95 value={autoEnvironmentLabel ? AUTO_VALUE : environmentValue(environmentId)}
96 onValueChange={(value) => {
97 if (value === ADD_MACHINE_VALUE) {
98 onAddToMachine?.();
99 } else if (value === AUTO_VALUE) {
100 onAutoEnvironment?.();
101 } else if (value?.startsWith("env:")) {
102 onEnvironmentChange?.(value.slice(4) as EnvironmentId);
103 }
104 }}
105 items={environmentItems}
106 >
107 <Tooltip>
108 <TooltipTrigger
109 render={
110 <ComposerSelectControl
111 size="xs"
112 className="min-w-0 max-w-full"
113 aria-label="Run on"
114 data-composer-shortcut="composer.host"
115 data-composer-context-control
116 />
117 }
118 >
119 {autoEnvironmentLabel ? (
120 <ScaleIcon className="size-3 shrink-0" aria-hidden="true" />
121 ) : (
122 <EnvironmentMachineIcon
123 kind={activeEnvironment?.machine ?? "server"}
124 className="size-3 shrink-0"
125 />
126 )}
127 <ComposerContextLabel>
128 <SelectValue />
129 </ComposerContextLabel>
130 </TooltipTrigger>
131 <TooltipPopup>
132 {autoEnvironmentLabel ?? activeEnvironment?.label ?? "Run on"}
133 {shortcutLabel ? ` · ${shortcutLabel}` : null}
134 </TooltipPopup>
135 </Tooltip>
136 <SelectPopup alignItemWithTrigger={false} {...composerFloatingLayerProps}>
137 <SelectGroup>
138 <SelectGroupLabel>Run on</SelectGroupLabel>
139 {onAutoEnvironment && (
140 <SelectItem
141 value={AUTO_VALUE}
142 onClick={() => {
143 if (autoEnvironmentLabel) onAutoEnvironment?.();
144 }}
145 >
146 <span className="inline-flex items-center gap-1.5">
147 <ScaleIcon className="size-3" aria-hidden="true" />
148 {autoEnvironmentLabel ?? "Auto balance"}
149 </span>
150 </SelectItem>
151 )}
152 {availableEnvironments.map((env) => (
153 <SelectItem
154 key={env.environmentId}
155 value={environmentValue(env.environmentId)}
156 disabled={onEnvironmentChange === undefined && env.environmentId !== environmentId}
157 >
158 <span className="inline-flex items-center gap-1.5">
159 <EnvironmentMachineIcon kind={env.machine} className="size-3" />
160 {env.label}
161 </span>
162 </SelectItem>
163 ))}
164 {onAddToMachine ? (
165 <SelectItem value={ADD_MACHINE_VALUE}>
166 <span className={ADD_TO_MACHINE_LABEL_CLASS}>
167 <PlusIcon aria-hidden="true" className="size-3" />
168 Add to another machine
169 </span>
170 </SelectItem>
171 ) : null}
172 </SelectGroup>
173 </SelectPopup>
174 </Select>
175 );
176});