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

BranchToolbarEnvironmentSelector.tsx

173 lines6.1 KB

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;
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 // Match the xs control's height and padding: the composer strip has no min-height,
67 // and its glass seam assumes that height even when this label is the only control.
68 if (envLocked || (onEnvironmentChange === undefined && onAddToMachine === undefined)) {
69 const lockedRow = (
70 <span
71 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-sm sm:h-6 sm:text-xs"
72 data-composer-context-control
73 >
74 <EnvironmentMachineIcon
75 kind={activeEnvironment?.machine ?? "server"}
76 className="size-3 shrink-0"
77 />
78 <ComposerContextLabel>{activeEnvironment?.label ?? "Run on"}</ComposerContextLabel>
79 </span>
80 );
81 return (
82 <Tooltip>
83 <TooltipTrigger render={lockedRow} />
84 <TooltipPopup>{activeEnvironment?.label ?? "Run on"}</TooltipPopup>
85 </Tooltip>
86 );
87 }
88​
89 return (
90 <Select
91 modal={false}
92 value={autoEnvironmentLabel ? AUTO_VALUE : environmentValue(environmentId)}
93 onValueChange={(value) => {
94 if (value === ADD_MACHINE_VALUE) {
95 onAddToMachine?.();
96 } else if (value === AUTO_VALUE) {
97 onAutoEnvironment?.();
98 } else if (value?.startsWith("env:")) {
99 onEnvironmentChange?.(value.slice(4) as EnvironmentId);
100 }
101 }}
102 items={environmentItems}
103 >
104 <Tooltip>
105 <TooltipTrigger
106 render={
107 <ComposerSelectControl
108 size="xs"
109 className="min-w-0 max-w-full"
110 aria-label="Run on"
111 data-composer-shortcut="composer.host"
112 data-composer-context-control
113 />
114 }
115 >
116 {autoEnvironmentLabel ? (
117 <ScaleIcon className="size-3 shrink-0" aria-hidden="true" />
118 ) : (
119 <EnvironmentMachineIcon
120 kind={activeEnvironment?.machine ?? "server"}
121 className="size-3 shrink-0"
122 />
123 )}
124 <ComposerContextLabel>
125 <SelectValue />
126 </ComposerContextLabel>
127 </TooltipTrigger>
128 <TooltipPopup>
129 {autoEnvironmentLabel ?? activeEnvironment?.label ?? "Run on"}
130 {shortcutLabel ? ` · ${shortcutLabel}` : null}
131 </TooltipPopup>
132 </Tooltip>
133 <SelectPopup alignItemWithTrigger={false} {...composerFloatingLayerProps}>
134 <SelectGroup>
135 <SelectGroupLabel>Run on</SelectGroupLabel>
136 {onAutoEnvironment && (
137 <SelectItem
138 value={AUTO_VALUE}
139 onClick={() => {
140 if (autoEnvironmentLabel) onAutoEnvironment?.();
141 }}
142 >
143 <span className="inline-flex items-center gap-1.5">
144 <ScaleIcon className="size-3" aria-hidden="true" />
145 {autoEnvironmentLabel ?? "Auto balance"}
146 </span>
147 </SelectItem>
148 )}
149 {availableEnvironments.map((env) => (
150 <SelectItem
151 key={env.environmentId}
152 value={environmentValue(env.environmentId)}
153 disabled={onEnvironmentChange === undefined && env.environmentId !== environmentId}
154 >
155 <span className="inline-flex items-center gap-1.5">
156 <EnvironmentMachineIcon kind={env.machine} className="size-3" />
157 {env.label}
158 </span>
159 </SelectItem>
160 ))}
161 {onAddToMachine ? (
162 <SelectItem value={ADD_MACHINE_VALUE}>
163 <span className={ADD_TO_MACHINE_LABEL_CLASS}>
164 <PlusIcon aria-hidden="true" className="size-3" />
165 Add to another machine
166 </span>
167 </SelectItem>
168 ) : null}
169 </SelectGroup>
170 </SelectPopup>
171 </Select>
172 );
173});