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

BranchToolbarEnvironmentSelector.tsx

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