Open History to see the last commit
1import type { EnvironmentId } from "@t3tools/contracts";
2import { memo, useMemo } from "react";
3
4import type { EnvironmentOption } from "./BranchToolbar.logic";
5import { EnvironmentMachineIcon } from "./EnvironmentMachineIcon";
6import { composerFloatingLayerProps } from "./chat/composerEventScope";
7import {
8 Select,
9 SelectGroup,
10 SelectGroupLabel,
11 SelectItem,
12 SelectPopup,
13 SelectTrigger,
14 SelectValue,
15} from "./ui/select";
16
17interface BranchToolbarEnvironmentSelectorProps {
18 envLocked: boolean;
19 environmentId: EnvironmentId;
20 availableEnvironments: readonly EnvironmentOption[];
21 // Absent when there is only one environment to show: the indicator still
22 // renders (as a static label) so remote projects are always identifiable.
23 onEnvironmentChange?: (environmentId: EnvironmentId) => void;
24}
25
26export const BranchToolbarEnvironmentSelector = memo(function BranchToolbarEnvironmentSelector({
27 envLocked,
28 environmentId,
29 availableEnvironments,
30 onEnvironmentChange,
31}: BranchToolbarEnvironmentSelectorProps) {
32 const activeEnvironment = useMemo(() => {
33 return availableEnvironments.find((env) => env.environmentId === environmentId) ?? null;
34 }, [availableEnvironments, environmentId]);
35
36 const environmentItems = useMemo(
37 () =>
38 availableEnvironments.map((env) => ({
39 value: env.environmentId,
40 label: env.label,
41 })),
42 [availableEnvironments],
43 );
44
45 // The static label carries the xs control's height (h-7 sm:h-6) as well as
46 // its padding: the composer context strip has no min-height of its own, and
47 // the glass seam joining it to the composer assumes a fixed strip height, so
48 // a shorter label would drag the seam out of line whenever this label is the
49 // only thing in the strip.
50 if (envLocked || onEnvironmentChange === undefined) {
51 return (
52 <span
53 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"
54 data-composer-context-control
55 >
56 <EnvironmentMachineIcon
57 kind={activeEnvironment?.machine ?? "server"}
58 className="size-3 shrink-0"
59 />
60 <span
61 data-composer-label
62 className="min-w-0 max-w-[240px] group-data-[compact]/composer-context:max-w-0"
63 >
64 <span
65 data-composer-label-motion
66 className="block w-full min-w-0 max-w-[240px] origin-left truncate transition-[opacity,transform] duration-180 ease-[cubic-bezier(0.32,0.72,0,1)] group-data-[compact]/composer-context:[transform:translateX(-0.25rem)_scaleX(0.95)] group-data-[compact]/composer-context:opacity-0 motion-reduce:transform-none motion-reduce:transition-opacity"
67 >
68 {activeEnvironment?.label ?? "Run on"}
69 </span>
70 </span>
71 </span>
72 );
73 }
74
75 return (
76 <Select
77 modal={false}
78 value={environmentId}
79 onValueChange={(value) => onEnvironmentChange(value as EnvironmentId)}
80 items={environmentItems}
81 >
82 <SelectTrigger
83 variant="ghost"
84 size="xs"
85 className="min-w-0 max-w-full font-normal text-xs!"
86 aria-label="Run on"
87 data-composer-context-control
88 >
89 <EnvironmentMachineIcon
90 kind={activeEnvironment?.machine ?? "server"}
91 className="size-3 shrink-0"
92 />
93 <span
94 data-composer-label
95 className="min-w-0 max-w-[240px] group-data-[compact]/composer-context:max-w-0"
96 >
97 <span
98 data-composer-label-motion
99 className="block w-full min-w-0 max-w-[240px] origin-left truncate transition-[opacity,transform] duration-180 ease-[cubic-bezier(0.32,0.72,0,1)] group-data-[compact]/composer-context:[transform:translateX(-0.25rem)_scaleX(0.95)] group-data-[compact]/composer-context:opacity-0 motion-reduce:transform-none motion-reduce:transition-opacity"
100 >
101 <SelectValue />
102 </span>
103 </span>
104 </SelectTrigger>
105 <SelectPopup alignItemWithTrigger={false} {...composerFloatingLayerProps}>
106 <SelectGroup>
107 <SelectGroupLabel>Run on</SelectGroupLabel>
108 {availableEnvironments.map((env) => (
109 <SelectItem key={env.environmentId} value={env.environmentId}>
110 <span className="inline-flex items-center gap-1.5">
111 <EnvironmentMachineIcon kind={env.machine} className="size-3" />
112 {env.label}
113 </span>
114 </SelectItem>
115 ))}
116 </SelectGroup>
117 </SelectPopup>
118 </Select>
119 );
120});