Open History to see the last commit
1import type { EnvironmentId } from "@t3tools/contracts";
2import { CloudIcon, FolderIcon, ServerIcon } from "lucide-react";
3import { memo, useMemo } from "react";
4
5import type { EnvironmentOption } from "./BranchToolbar.logic";
6import { Select, SelectItem, SelectPopup, SelectTrigger, SelectValue } from "./ui/select";
7
8interface BranchToolbarEnvironmentSelectorProps {
9 envLocked: boolean;
10 environmentId: EnvironmentId;
11 availableEnvironments: readonly EnvironmentOption[];
12 onEnvironmentChange: (environmentId: EnvironmentId) => void;
13}
14
15export const BranchToolbarEnvironmentSelector = memo(function BranchToolbarEnvironmentSelector({
16 envLocked,
17 environmentId,
18 availableEnvironments,
19 onEnvironmentChange,
20}: BranchToolbarEnvironmentSelectorProps) {
21 const activeEnvironmentLabel = useMemo(() => {
22 return availableEnvironments.find((env) => env.environmentId === environmentId)?.label ?? null;
23 }, [availableEnvironments, environmentId]);
24
25 const environmentItems = useMemo(
26 () =>
27 availableEnvironments.map((env) => ({
28 value: env.environmentId,
29 label: env.label,
30 })),
31 [availableEnvironments],
32 );
33
34 if (envLocked) {
35 return (
36 <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">
37 <ServerIcon className="size-3" />
38 {activeEnvironmentLabel ?? "Environment"}
39 </span>
40 );
41 }
42
43 return (
44 <Select
45 value={environmentId}
46 onValueChange={(value) => onEnvironmentChange(value as EnvironmentId)}
47 items={environmentItems}
48 >
49 <SelectTrigger variant="ghost" size="xs" className="font-medium">
50 <ServerIcon className="size-3" />
51 <SelectValue />
52 </SelectTrigger>
53 <SelectPopup>
54 {availableEnvironments.map((env) => (
55 <SelectItem key={env.environmentId} value={env.environmentId}>
56 <span className="inline-flex items-center gap-1.5">
57 {env.isPrimary ? <FolderIcon className="size-3" /> : <CloudIcon className="size-3" />}
58 {env.label}
59 </span>
60 </SelectItem>
61 ))}
62 </SelectPopup>
63 </Select>
64 );
65});