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