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

BranchToolbarEnvironmentSelector.tsx

171 lines5.9 KB

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