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

BranchToolbarEnvironmentSelector.tsx

89 lines2.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 onEnvironmentChange: (environmentId: EnvironmentId) => void;
22​
23export const BranchToolbarEnvironmentSelector = memo(function BranchToolbarEnvironmentSelector({
24 envLocked,
25 environmentId,
26 availableEnvironments,
27 onEnvironmentChange,
28}: BranchToolbarEnvironmentSelectorProps) {
29 const activeEnvironment = useMemo(() => {
30 return availableEnvironments.find((env) => env.environmentId === environmentId) ?? null;
31 }, [availableEnvironments, environmentId]);
32​
33 const environmentItems = useMemo(
34 () =>
35 availableEnvironments.map((env) => ({
36 value: env.environmentId,
37 label: env.label,
38 })),
39 [availableEnvironments],
40 );
41​
42 if (envLocked) {
43 return (
44 <span className="inline-flex items-center gap-1 border border-transparent px-[calc(--spacing(3)-1px)] text-sm font-medium text-muted-foreground/70 sm:text-xs">
45 {activeEnvironment?.isPrimary ? (
46 <MonitorIcon className="size-3" />
47 ) : (
48 <CloudIcon className="size-3" />
49 )}
50 {activeEnvironment?.label ?? "Run on"}
51 </span>
52 );
53 }
54​
55 return (
56 <Select
57 modal={false}
58 value={environmentId}
59 onValueChange={(value) => onEnvironmentChange(value as EnvironmentId)}
60 items={environmentItems}
61 >
62 <SelectTrigger variant="ghost" size="xs" className="font-medium" aria-label="Run on">
63 {activeEnvironment?.isPrimary ? (
64 <MonitorIcon className="size-3" />
65 ) : (
66 <CloudIcon className="size-3" />
67 )}
68 <SelectValue />
69 </SelectTrigger>
70 <SelectPopup>
71 <SelectGroup>
72 <SelectGroupLabel>Run on</SelectGroupLabel>
73 {availableEnvironments.map((env) => (
74 <SelectItem key={env.environmentId} value={env.environmentId}>
75 <span className="inline-flex items-center gap-1.5">
76 {env.isPrimary ? (
77 <MonitorIcon className="size-3" />
78 ) : (
79 <CloudIcon className="size-3" />
80 )}
81 {env.label}
82 </span>
83 </SelectItem>
84 ))}
85 </SelectGroup>
86 </SelectPopup>
87 </Select>
88 );
89});