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 { cn } from "../lib/utils";
7import {
8 THREAD_DETAILS_PANEL_ICON_CLASS,
9 THREAD_DETAILS_PANEL_LOCKED_ROW_CLASS,
10 THREAD_DETAILS_PANEL_ROW_POPUP_CLASS,
11 THREAD_DETAILS_PANEL_SELECT_ROW_CLASS,
12} from "./chat/threadDetailsPanelStyles";
13import { EnvironmentMachineIcon } from "./EnvironmentMachineIcon";
14import { composerFloatingLayerProps } from "./chat/composerEventScope";
15import {
16 Select,
17 SelectGroup,
18 SelectGroupLabel,
19 SelectItem,
20 SelectPopup,
21 SelectTrigger,
22 SelectValue,
23} from "./ui/select";
24
25interface BranchToolbarEnvironmentSelectorProps {
26 autoEnvironmentLabel?: string | undefined;
27 onAutoEnvironment?: (() => void) | undefined;
28 envLocked: boolean;
29 environmentId: EnvironmentId;
30 availableEnvironments: readonly EnvironmentOption[];
31 onEnvironmentChange?: (environmentId: EnvironmentId) => void;
32 displayMode?: "toolbar" | "panel";
33}
34
35export const BranchToolbarEnvironmentSelector = memo(function BranchToolbarEnvironmentSelector({
36 autoEnvironmentLabel,
37 onAutoEnvironment,
38 envLocked,
39 environmentId,
40 availableEnvironments,
41 onEnvironmentChange,
42 displayMode = "toolbar",
43}: BranchToolbarEnvironmentSelectorProps) {
44 const activeEnvironment = useMemo(() => {
45 return availableEnvironments.find((env) => env.environmentId === environmentId) ?? null;
46 }, [availableEnvironments, environmentId]);
47
48 const environmentItems = useMemo(
49 () => [
50 ...(onAutoEnvironment
51 ? [{ value: "auto", label: autoEnvironmentLabel ?? "Auto balance" }]
52 : []),
53 ...availableEnvironments.map((env) => ({
54 value: env.environmentId,
55 label: env.label,
56 })),
57 ],
58 [availableEnvironments, autoEnvironmentLabel, onAutoEnvironment],
59 );
60
61 // The static label carries the xs control's height (h-7 sm:h-6) as well as
62 // its padding: the composer context strip has no min-height of its own, and
63 // the glass seam joining it to the composer assumes a fixed strip height, so
64 // a shorter label would drag the seam out of line whenever this label is the
65 // only thing in the strip.
66 if (envLocked || onEnvironmentChange === undefined) {
67 return (
68 <span
69 className={cn(
70 "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",
71 displayMode === "panel" && THREAD_DETAILS_PANEL_LOCKED_ROW_CLASS,
72 )}
73 data-composer-context-control
74 >
75 <EnvironmentMachineIcon
76 kind={activeEnvironment?.machine ?? "server"}
77 className={displayMode === "panel" ? THREAD_DETAILS_PANEL_ICON_CLASS : "size-3 shrink-0"}
78 />
79 <span
80 data-composer-label
81 className="min-w-0 max-w-[240px] truncate transition-[max-width,opacity] duration-300 ease-out group-data-[compact]/composer-context:max-w-0 group-data-[compact]/composer-context:opacity-0"
82 >
83 <span
84 data-composer-label-motion
85 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"
86 >
87 {activeEnvironment?.label ?? "Run on"}
88 </span>
89 </span>
90 </span>
91 );
92 }
93
94 return (
95 <Select
96 modal={false}
97 value={autoEnvironmentLabel ? "auto" : environmentId}
98 onValueChange={(value) =>
99 value === "auto" ? onAutoEnvironment?.() : onEnvironmentChange(value as EnvironmentId)
100 }
101 items={environmentItems}
102 >
103 <SelectTrigger
104 variant="ghost"
105 size={displayMode === "panel" ? "default" : "xs"}
106 className={cn(
107 "min-w-0 max-w-full font-normal text-xs!",
108 displayMode === "panel" && THREAD_DETAILS_PANEL_SELECT_ROW_CLASS,
109 )}
110 aria-label="Run on"
111 data-composer-context-control
112 >
113 {autoEnvironmentLabel ? (
114 <ScaleIcon
115 className={
116 displayMode === "panel" ? THREAD_DETAILS_PANEL_ICON_CLASS : "size-3 shrink-0"
117 }
118 aria-hidden="true"
119 />
120 ) : (
121 <EnvironmentMachineIcon
122 kind={activeEnvironment?.machine ?? "server"}
123 className={
124 displayMode === "panel" ? THREAD_DETAILS_PANEL_ICON_CLASS : "size-3 shrink-0"
125 }
126 />
127 )}
128 <span
129 data-composer-label
130 className="min-w-0 max-w-[240px] truncate transition-[max-width,opacity] duration-300 ease-out group-data-[compact]/composer-context:max-w-0 group-data-[compact]/composer-context:opacity-0"
131 >
132 <span
133 data-composer-label-motion
134 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"
135 >
136 <SelectValue />
137 </span>
138 </span>
139 </SelectTrigger>
140 <SelectPopup
141 alignItemWithTrigger={false}
142 {...(displayMode === "toolbar" ? composerFloatingLayerProps : {})}
143 {...(displayMode === "panel"
144 ? {
145 popupClassName: THREAD_DETAILS_PANEL_ROW_POPUP_CLASS,
146 }
147 : {})}
148 >
149 <SelectGroup>
150 <SelectGroupLabel>Run on</SelectGroupLabel>
151 {onAutoEnvironment && (
152 <SelectItem
153 value="auto"
154 onClick={() => {
155 if (autoEnvironmentLabel) onAutoEnvironment?.();
156 }}
157 >
158 <span className="inline-flex items-center gap-1.5">
159 <ScaleIcon className="size-3" aria-hidden="true" />
160 {autoEnvironmentLabel ?? "Auto balance"}
161 </span>
162 </SelectItem>
163 )}
164 {availableEnvironments.map((env) => (
165 <SelectItem key={env.environmentId} value={env.environmentId}>
166 <span className="inline-flex items-center gap-1.5">
167 <EnvironmentMachineIcon kind={env.machine} className="size-3" />
168 {env.label}
169 </span>
170 </SelectItem>
171 ))}
172 </SelectGroup>
173 </SelectPopup>
174 </Select>
175 );
176});