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 { composerFloatingLayerProps } 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 activeEnvironment = useMemo(() => {
38 return availableEnvironments.find((env) => env.environmentId === environmentId) ?? null;
39 }, [availableEnvironments, environmentId]);
40
41 const environmentItems = useMemo(
42 () => [
43 ...(onAutoEnvironment
44 ? [{ value: "auto", label: autoEnvironmentLabel ?? "Auto balance" }]
45 : []),
46 ...availableEnvironments.map((env) => ({
47 value: env.environmentId,
48 label: env.label,
49 })),
50 ],
51 [availableEnvironments, autoEnvironmentLabel, onAutoEnvironment],
52 );
53
54 // The static label carries the xs control's height (h-7 sm:h-6) as well as
55 // its padding: the composer context strip has no min-height of its own, and
56 // the glass seam joining it to the composer assumes a fixed strip height, so
57 // a shorter label would drag the seam out of line whenever this label is the
58 // only thing in the strip.
59 if (envLocked || onEnvironmentChange === undefined) {
60 return (
61 <span
62 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"
63 data-composer-context-control
64 >
65 <EnvironmentMachineIcon
66 kind={activeEnvironment?.machine ?? "server"}
67 className="size-3 shrink-0"
68 />
69 <span
70 data-composer-label
71 className="min-w-0 max-w-[240px] group-data-[compact]/composer-context:max-w-0"
72 >
73 <span
74 data-composer-label-motion
75 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"
76 >
77 {activeEnvironment?.label ?? "Run on"}
78 </span>
79 </span>
80 </span>
81 );
82 }
83
84 return (
85 <Select
86 modal={false}
87 value={autoEnvironmentLabel ? "auto" : environmentId}
88 onValueChange={(value) =>
89 value === "auto" ? onAutoEnvironment?.() : onEnvironmentChange(value as EnvironmentId)
90 }
91 items={environmentItems}
92 >
93 <SelectTrigger
94 variant="ghost"
95 size="xs"
96 className="min-w-0 max-w-full font-normal text-xs!"
97 aria-label="Run on"
98 data-composer-shortcut="composer.host"
99 data-composer-context-control
100 >
101 {autoEnvironmentLabel ? (
102 <ScaleIcon className="size-3 shrink-0" aria-hidden="true" />
103 ) : (
104 <EnvironmentMachineIcon
105 kind={activeEnvironment?.machine ?? "server"}
106 className="size-3 shrink-0"
107 />
108 )}
109 <span
110 data-composer-label
111 className="min-w-0 max-w-[240px] group-data-[compact]/composer-context:max-w-0"
112 >
113 <span
114 data-composer-label-motion
115 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"
116 >
117 <SelectValue />
118 </span>
119 </span>
120 </SelectTrigger>
121 <SelectPopup alignItemWithTrigger={false} {...composerFloatingLayerProps}>
122 <SelectGroup>
123 <SelectGroupLabel>Run on</SelectGroupLabel>
124 {onAutoEnvironment && (
125 <SelectItem
126 value="auto"
127 onClick={() => {
128 if (autoEnvironmentLabel) onAutoEnvironment?.();
129 }}
130 >
131 <span className="inline-flex items-center gap-1.5">
132 <ScaleIcon className="size-3" aria-hidden="true" />
133 {autoEnvironmentLabel ?? "Auto balance"}
134 </span>
135 </SelectItem>
136 )}
137 {availableEnvironments.map((env) => (
138 <SelectItem key={env.environmentId} value={env.environmentId}>
139 <span className="inline-flex items-center gap-1.5">
140 <EnvironmentMachineIcon kind={env.machine} className="size-3" />
141 {env.label}
142 </span>
143 </SelectItem>
144 ))}
145 </SelectGroup>
146 </SelectPopup>
147 </Select>
148 );
149});