Open History to see the last commit
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;
23}
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 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">
52 {activeEnvironment?.isPrimary ? (
53 <MonitorIcon className="size-3 shrink-0" />
54 ) : (
55 <CloudIcon className="size-3 shrink-0" />
56 )}
57 <span
58 data-composer-label
59 className="min-w-0 max-w-[240px] truncate transition-[max-width,opacity] duration-300 ease-out group-data-[compact]/composer-context:max-w-0 group-data-[compact]/composer-context:opacity-0"
60 >
61 {activeEnvironment?.label ?? "Run on"}
62 </span>
63 </span>
64 );
65 }
66
67 return (
68 <Select
69 modal={false}
70 value={environmentId}
71 onValueChange={(value) => onEnvironmentChange(value as EnvironmentId)}
72 items={environmentItems}
73 >
74 <SelectTrigger
75 variant="ghost"
76 size="xs"
77 className="min-w-0 max-w-full font-medium"
78 aria-label="Run on"
79 >
80 {activeEnvironment?.isPrimary ? (
81 <MonitorIcon className="size-3 shrink-0" />
82 ) : (
83 <CloudIcon className="size-3 shrink-0" />
84 )}
85 <span
86 data-composer-label
87 className="min-w-0 max-w-[240px] truncate transition-[max-width,opacity] duration-300 ease-out group-data-[compact]/composer-context:max-w-0 group-data-[compact]/composer-context:opacity-0"
88 >
89 <SelectValue />
90 </span>
91 </SelectTrigger>
92 <SelectPopup>
93 <SelectGroup>
94 <SelectGroupLabel>Run on</SelectGroupLabel>
95 {availableEnvironments.map((env) => (
96 <SelectItem key={env.environmentId} value={env.environmentId}>
97 <span className="inline-flex items-center gap-1.5">
98 {env.isPrimary ? (
99 <MonitorIcon className="size-3" />
100 ) : (
101 <CloudIcon className="size-3" />
102 )}
103 {env.label}
104 </span>
105 </SelectItem>
106 ))}
107 </SelectGroup>
108 </SelectPopup>
109 </Select>
110 );
111});