Open History to see the last commit
1import type { EnvironmentId } from "@t3tools/contracts";
2import { ScaleIcon } from "lucide-react";
3import { memo, useMemo } from "react";
4
5import type { EnvironmentOption } from "./BranchToolbar.logic";
6import { EnvironmentMachineIcon } from "./EnvironmentMachineIcon";
7import { useComposerMenuProps } from "./chat/composerEventScope";
8import {
9 Select,
10 SelectGroup,
11 SelectGroupLabel,
12 SelectItem,
13 SelectPopup,
14 SelectTrigger,
15 SelectValue,
16} from "./ui/select";
17
18interface BranchToolbarEnvironmentSelectorProps {
19 autoEnvironmentLabel?: string | undefined;
20 onAutoEnvironment?: (() => void) | undefined;
21 envLocked: boolean;
22 environmentId: EnvironmentId;
23 availableEnvironments: readonly EnvironmentOption[];
24 // Absent when there is only one environment to show: the indicator still
25 // renders (as a static label) so remote projects are always identifiable.
26 onEnvironmentChange?: (environmentId: EnvironmentId) => void;
27}
28
29export const BranchToolbarEnvironmentSelector = memo(function BranchToolbarEnvironmentSelector({
30 autoEnvironmentLabel,
31 onAutoEnvironment,
32 envLocked,
33 environmentId,
34 availableEnvironments,
35 onEnvironmentChange,
36}: BranchToolbarEnvironmentSelectorProps) {
37 const composerFloatingLayerProps = useComposerMenuProps();
38 const activeEnvironment = useMemo(() => {
39 return availableEnvironments.find((env) => env.environmentId === environmentId) ?? null;
40 }, [availableEnvironments, environmentId]);
41
42 const environmentItems = useMemo(
43 () => [
44 ...(onAutoEnvironment
45 ? [{ value: "auto", label: autoEnvironmentLabel ?? "Auto balance" }]
46 : []),
47 ...availableEnvironments.map((env) => ({
48 value: env.environmentId,
49 label: env.label,
50 })),
51 ],
52 [availableEnvironments, autoEnvironmentLabel, onAutoEnvironment],
53 );
54
55 // The static label carries the xs control's height (h-7 sm:h-6) as well as
56 // its padding: the composer context strip has no min-height of its own, and
57 // the glass seam joining it to the composer assumes a fixed strip height, so
58 // a shorter label would drag the seam out of line whenever this label is the
59 // only thing in the strip.
60 if (envLocked || onEnvironmentChange === undefined) {
61 return (
62 <span
63 className="inline-flex h-7 min-w-0 max-w-full items-center gap-1 border border-transparent px-[calc(--spacing(2)-1px)] font-normal text-muted-foreground/70 text-xs sm:h-6"
64 data-composer-context-control
65 >
66 <EnvironmentMachineIcon
67 kind={activeEnvironment?.machine ?? "server"}
68 className="size-3 shrink-0"
69 />
70 <span
71 data-composer-label
72 className="min-w-0 max-w-[240px] group-data-[compact]/composer-context:max-w-0"
73 >
74 <span
75 data-composer-label-motion
76 className="block w-full min-w-0 max-w-[240px] truncate transition-opacity duration-180 ease-[cubic-bezier(0.32,0.72,0,1)] group-data-[compact]/composer-context:opacity-0 motion-reduce:transition-none"
77 >
78 {activeEnvironment?.label ?? "Run on"}
79 </span>
80 </span>
81 </span>
82 );
83 }
84
85 return (
86 <Select
87 modal={false}
88 value={autoEnvironmentLabel ? "auto" : environmentId}
89 onValueChange={(value) =>
90 value === "auto" ? onAutoEnvironment?.() : onEnvironmentChange(value as EnvironmentId)
91 }
92 items={environmentItems}
93 >
94 <SelectTrigger
95 variant="ghost"
96 size="xs"
97 className="min-w-0 max-w-full font-normal text-xs!"
98 aria-label="Run on"
99 data-composer-shortcut="composer.host"
100 data-composer-context-control
101 >
102 {autoEnvironmentLabel ? (
103 <ScaleIcon className="size-3 shrink-0" aria-hidden="true" />
104 ) : (
105 <EnvironmentMachineIcon
106 kind={activeEnvironment?.machine ?? "server"}
107 className="size-3 shrink-0"
108 />
109 )}
110 <span
111 data-composer-label
112 className="min-w-0 max-w-[240px] group-data-[compact]/composer-context:max-w-0"
113 >
114 <span
115 data-composer-label-motion
116 className="block w-full min-w-0 max-w-[240px] truncate transition-opacity duration-180 ease-[cubic-bezier(0.32,0.72,0,1)] group-data-[compact]/composer-context:opacity-0 motion-reduce:transition-none"
117 >
118 <SelectValue />
119 </span>
120 </span>
121 </SelectTrigger>
122 <SelectPopup alignItemWithTrigger={false} {...composerFloatingLayerProps}>
123 <SelectGroup>
124 <SelectGroupLabel>Run on</SelectGroupLabel>
125 {onAutoEnvironment && (
126 <SelectItem
127 value="auto"
128 onClick={() => {
129 if (autoEnvironmentLabel) onAutoEnvironment?.();
130 }}
131 >
132 <span className="inline-flex items-center gap-1.5">
133 <ScaleIcon className="size-3" aria-hidden="true" />
134 {autoEnvironmentLabel ?? "Auto balance"}
135 </span>
136 </SelectItem>
137 )}
138 {availableEnvironments.map((env) => (
139 <SelectItem key={env.environmentId} value={env.environmentId}>
140 <span className="inline-flex items-center gap-1.5">
141 <EnvironmentMachineIcon kind={env.machine} className="size-3" />
142 {env.label}
143 </span>
144 </SelectItem>
145 ))}
146 </SelectGroup>
147 </SelectPopup>
148 </Select>
149 );
150});