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 onEnvironmentChange: (environmentId: EnvironmentId) => void;
21}
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 value={environmentId}
58 onValueChange={(value) => onEnvironmentChange(value as EnvironmentId)}
59 items={environmentItems}
60 >
61 <SelectTrigger variant="ghost" size="xs" className="font-medium" aria-label="Run on">
62 {activeEnvironment?.isPrimary ? (
63 <MonitorIcon className="size-3" />
64 ) : (
65 <CloudIcon className="size-3" />
66 )}
67 <SelectValue />
68 </SelectTrigger>
69 <SelectPopup>
70 <SelectGroup>
71 <SelectGroupLabel>Run on</SelectGroupLabel>
72 {availableEnvironments.map((env) => (
73 <SelectItem key={env.environmentId} value={env.environmentId}>
74 <span className="inline-flex items-center gap-1.5">
75 {env.isPrimary ? (
76 <MonitorIcon className="size-3" />
77 ) : (
78 <CloudIcon className="size-3" />
79 )}
80 {env.label}
81 </span>
82 </SelectItem>
83 ))}
84 </SelectGroup>
85 </SelectPopup>
86 </Select>
87 );
88});