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