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

BranchToolbarEnvironmentSelector.tsx

106 lines3.5 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 if (envLocked || onEnvironmentChange === undefined) {
45 return (
46 <span className="inline-flex 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:text-xs">
47 {activeEnvironment?.isPrimary ? (
48 <MonitorIcon className="size-3 shrink-0" />
49 ) : (
50 <CloudIcon className="size-3 shrink-0" />
51 )}
52 <span
53 data-composer-label
54 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"
55 >
56 {activeEnvironment?.label ?? "Run on"}
57 </span>
58 </span>
59 );
60 }
61​
62 return (
63 <Select
64 modal={false}
65 value={environmentId}
66 onValueChange={(value) => onEnvironmentChange(value as EnvironmentId)}
67 items={environmentItems}
68 >
69 <SelectTrigger
70 variant="ghost"
71 size="xs"
72 className="min-w-0 max-w-full font-medium"
73 aria-label="Run on"
74 >
75 {activeEnvironment?.isPrimary ? (
76 <MonitorIcon className="size-3 shrink-0" />
77 ) : (
78 <CloudIcon className="size-3 shrink-0" />
79 )}
80 <span
81 data-composer-label
82 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"
83 >
84 <SelectValue />
85 </span>
86 </SelectTrigger>
87 <SelectPopup>
88 <SelectGroup>
89 <SelectGroupLabel>Run on</SelectGroupLabel>
90 {availableEnvironments.map((env) => (
91 <SelectItem key={env.environmentId} value={env.environmentId}>
92 <span className="inline-flex items-center gap-1.5">
93 {env.isPrimary ? (
94 <MonitorIcon className="size-3" />
95 ) : (
96 <CloudIcon className="size-3" />
97 )}
98 {env.label}
99 </span>
100 </SelectItem>
101 ))}
102 </SelectGroup>
103 </SelectPopup>
104 </Select>
105 );
106});