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

BranchToolbarEnvironmentSelector.tsx

174 lines6.1 KB

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