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

BranchToolbarEnvironmentSelector.tsx

148 lines5.3 KB

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;
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-context-control
99 >
100 {autoEnvironmentLabel ? (
101 <ScaleIcon className="size-3 shrink-0" aria-hidden="true" />
102 ) : (
103 <EnvironmentMachineIcon
104 kind={activeEnvironment?.machine ?? "server"}
105 className="size-3 shrink-0"
106 />
107 )}
108 <span
109 data-composer-label
110 className="min-w-0 max-w-[240px] group-data-[compact]/composer-context:max-w-0"
111 >
112 <span
113 data-composer-label-motion
114 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"
115 >
116 <SelectValue />
117 </span>
118 </span>
119 </SelectTrigger>
120 <SelectPopup alignItemWithTrigger={false} {...composerFloatingLayerProps}>
121 <SelectGroup>
122 <SelectGroupLabel>Run on</SelectGroupLabel>
123 {onAutoEnvironment && (
124 <SelectItem
125 value="auto"
126 onClick={() => {
127 if (autoEnvironmentLabel) onAutoEnvironment?.();
128 }}
129 >
130 <span className="inline-flex items-center gap-1.5">
131 <ScaleIcon className="size-3" aria-hidden="true" />
132 {autoEnvironmentLabel ?? "Auto balance"}
133 </span>
134 </SelectItem>
135 )}
136 {availableEnvironments.map((env) => (
137 <SelectItem key={env.environmentId} value={env.environmentId}>
138 <span className="inline-flex items-center gap-1.5">
139 <EnvironmentMachineIcon kind={env.machine} className="size-3" />
140 {env.label}
141 </span>
142 </SelectItem>
143 ))}
144 </SelectGroup>
145 </SelectPopup>
146 </Select>
147 );
148});