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

BranchToolbarEnvModeSelector.tsx

147 lines5.1 KB

1import { FolderGit2Icon, FolderGitIcon, FolderIcon, HistoryIcon } from "lucide-react";
2import { memo, useMemo } from "react";
3​
4import {
5 resolveCurrentWorkspaceLabel,
6 resolveEnvModeLabel,
7 resolveLockedWorkspaceLabel,
8 type EnvMode,
9} from "./BranchToolbar.logic";
10import {
11 Select,
12 SelectGroup,
13 SelectGroupLabel,
14 SelectItem,
15 SelectPopup,
16 SelectTrigger,
17 SelectValue,
18} from "./ui/select";
19​
20export const PREVIOUS_WORKTREE_SELECT_VALUE = "previous-worktree";
21​
22interface BranchToolbarEnvModeSelectorProps {
23 envLocked: boolean;
24 effectiveEnvMode: EnvMode;
25 activeWorktreePath: string | null;
26 onEnvModeChange: (mode: EnvMode) => void;
27 previousWorktreeLabel?: string | null;
28 onUsePreviousWorktree?: () => void;
30​
31export const BranchToolbarEnvModeSelector = memo(function BranchToolbarEnvModeSelector({
32 envLocked,
33 effectiveEnvMode,
34 activeWorktreePath,
35 onEnvModeChange,
36 previousWorktreeLabel,
37 onUsePreviousWorktree,
38}: BranchToolbarEnvModeSelectorProps) {
39 const showPreviousWorktree = Boolean(previousWorktreeLabel && onUsePreviousWorktree);
40 const envModeItems = useMemo(
41 () => [
42 { value: "local", label: resolveCurrentWorkspaceLabel(activeWorktreePath) },
43 { value: "worktree", label: resolveEnvModeLabel("worktree") },
44 ...(showPreviousWorktree && previousWorktreeLabel
45 ? [{ value: PREVIOUS_WORKTREE_SELECT_VALUE, label: previousWorktreeLabel }]
46 : []),
47 ],
48 [activeWorktreePath, previousWorktreeLabel, showPreviousWorktree],
49 );
50​
51 if (envLocked) {
52 return (
53 <span
54 className="inline-flex h-7 min-w-0 items-center gap-1 border border-transparent px-[calc(--spacing(3)-1px)] text-sm font-medium text-muted-foreground/70 sm:h-6 sm:text-xs"
55 data-composer-context-control
56 >
57 {activeWorktreePath ? (
58 <FolderGitIcon className="size-3 shrink-0" />
59 ) : (
60 <FolderIcon className="size-3 shrink-0" />
61 )}
62 <span
63 data-composer-label
64 className="min-w-0 max-w-[240px] group-data-[compact]/composer-context:max-w-0"
65 >
66 <span
67 data-composer-label-motion
68 className="block w-full min-w-0 max-w-[240px] origin-left truncate transition-[opacity,transform] duration-180 ease-[cubic-bezier(0.32,0.72,0,1)] group-data-[compact]/composer-context:[transform:translateX(-0.25rem)_scaleX(0.95)] group-data-[compact]/composer-context:opacity-0 motion-reduce:transform-none motion-reduce:transition-opacity"
69 >
70 {resolveLockedWorkspaceLabel(activeWorktreePath)}
71 </span>
72 </span>
73 </span>
74 );
75 }
76​
77 return (
78 <Select
79 modal={false}
80 value={effectiveEnvMode}
81 onValueChange={(value: string | null) => {
82 if (value === PREVIOUS_WORKTREE_SELECT_VALUE) {
83 onUsePreviousWorktree?.();
84 return;
85 }
86 onEnvModeChange(value as EnvMode);
87 }}
88 items={envModeItems}
89 >
90 <SelectTrigger
91 variant="ghost"
92 size="xs"
93 className="min-w-0 shrink font-medium"
94 aria-label="Workspace"
95 data-composer-context-control
96 >
97 {effectiveEnvMode === "worktree" ? (
98 <FolderGit2Icon className="size-3" />
99 ) : activeWorktreePath ? (
100 <FolderGitIcon className="size-3" />
101 ) : (
102 <FolderIcon className="size-3" />
103 )}
104 <span
105 data-composer-label
106 className="min-w-0 max-w-[240px] group-data-[compact]/composer-context:max-w-0"
107 >
108 <span
109 data-composer-label-motion
110 className="block w-full min-w-0 max-w-[240px] origin-left truncate transition-[opacity,transform] duration-180 ease-[cubic-bezier(0.32,0.72,0,1)] group-data-[compact]/composer-context:[transform:translateX(-0.25rem)_scaleX(0.95)] group-data-[compact]/composer-context:opacity-0 motion-reduce:transform-none motion-reduce:transition-opacity"
111 >
112 <SelectValue />
113 </span>
114 </span>
115 </SelectTrigger>
116 <SelectPopup>
117 <SelectGroup>
118 <SelectGroupLabel>Workspace</SelectGroupLabel>
119 <SelectItem value="local">
120 <span className="inline-flex items-center gap-1.5">
121 {activeWorktreePath ? (
122 <FolderGitIcon className="size-3" />
123 ) : (
124 <FolderIcon className="size-3" />
125 )}
126 {resolveCurrentWorkspaceLabel(activeWorktreePath)}
127 </span>
128 </SelectItem>
129 <SelectItem value="worktree">
130 <span className="inline-flex items-center gap-1.5">
131 <FolderGit2Icon className="size-3" />
132 {resolveEnvModeLabel("worktree")}
133 </span>
134 </SelectItem>
135 {showPreviousWorktree && previousWorktreeLabel ? (
136 <SelectItem value={PREVIOUS_WORKTREE_SELECT_VALUE}>
137 <span className="inline-flex items-center gap-1.5">
138 <HistoryIcon className="size-3" />
139 {previousWorktreeLabel}
140 </span>
141 </SelectItem>
142 ) : null}
143 </SelectGroup>
144 </SelectPopup>
145 </Select>
146 );
147});