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

BranchToolbarEnvironmentSelector.tsx

125 lines4.6 KB

1import type { EnvironmentId } from "@t3tools/contracts";
2import { CloudIcon, MonitorIcon } from "lucide-react";
3import { memo, useMemo } from "react";
4​
5import type { EnvironmentOption } from "./BranchToolbar.logic";
6import {
7 Select,
8 SelectGroup,
9 SelectGroupLabel,
10 SelectItem,
11 SelectPopup,
12 SelectTrigger,
13 SelectValue,
14} from "./ui/select";
15​
16interface BranchToolbarEnvironmentSelectorProps {
17 envLocked: boolean;
18 environmentId: EnvironmentId;
19 availableEnvironments: readonly EnvironmentOption[];
20 // Absent when there is only one environment to show: the indicator still
21 // renders (as a static label) so remote projects are always identifiable.
22 onEnvironmentChange?: (environmentId: EnvironmentId) => void;
24​
25export const BranchToolbarEnvironmentSelector = memo(function BranchToolbarEnvironmentSelector({
26 envLocked,
27 environmentId,
28 availableEnvironments,
29 onEnvironmentChange,
30}: BranchToolbarEnvironmentSelectorProps) {
31 const activeEnvironment = useMemo(() => {
32 return availableEnvironments.find((env) => env.environmentId === environmentId) ?? null;
33 }, [availableEnvironments, environmentId]);
34​
35 const environmentItems = useMemo(
36 () =>
37 availableEnvironments.map((env) => ({
38 value: env.environmentId,
39 label: env.label,
40 })),
41 [availableEnvironments],
42 );
43​
44 // The static label carries the xs control's height (h-7 sm:h-6) as well as
45 // its padding: the composer context strip has no min-height of its own, and
46 // the glass seam joining it to the composer assumes a fixed strip height, so
47 // a shorter label would drag the seam out of line whenever this label is the
48 // only thing in the strip.
49 if (envLocked || onEnvironmentChange === undefined) {
50 return (
51 <span
52 className="inline-flex h-7 min-w-0 max-w-full items-center gap-1 border border-transparent px-[calc(--spacing(3)-1px)] text-sm font-medium text-muted-foreground/70 sm:h-6 sm:text-xs"
53 data-composer-context-control
54 >
55 {activeEnvironment?.isPrimary ? (
56 <MonitorIcon className="size-3 shrink-0" />
57 ) : (
58 <CloudIcon 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-medium"
86 aria-label="Run on"
87 data-composer-context-control
88 >
89 {activeEnvironment?.isPrimary ? (
90 <MonitorIcon className="size-3 shrink-0" />
91 ) : (
92 <CloudIcon className="size-3 shrink-0" />
93 )}
94 <span
95 data-composer-label
96 className="min-w-0 max-w-[240px] group-data-[compact]/composer-context:max-w-0"
97 >
98 <span
99 data-composer-label-motion
100 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"
101 >
102 <SelectValue />
103 </span>
104 </span>
105 </SelectTrigger>
106 <SelectPopup>
107 <SelectGroup>
108 <SelectGroupLabel>Run on</SelectGroupLabel>
109 {availableEnvironments.map((env) => (
110 <SelectItem key={env.environmentId} value={env.environmentId}>
111 <span className="inline-flex items-center gap-1.5">
112 {env.isPrimary ? (
113 <MonitorIcon className="size-3" />
114 ) : (
115 <CloudIcon className="size-3" />
116 )}
117 {env.label}
118 </span>
119 </SelectItem>
120 ))}
121 </SelectGroup>
122 </SelectPopup>
123 </Select>
124 );
125});