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

BranchToolbarEnvModeSelector.tsx

142 lines4.6 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 shrink-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 <>
59 <FolderGitIcon className="size-3" />
60 {resolveLockedWorkspaceLabel(activeWorktreePath)}
61 </>
62 ) : (
63 <>
64 <FolderIcon className="size-3" />
65 {resolveLockedWorkspaceLabel(activeWorktreePath)}
66 </>
67 )}
68 </span>
69 );
70 }
71​
72 return (
73 <Select
74 modal={false}
75 value={effectiveEnvMode}
76 onValueChange={(value: string | null) => {
77 if (value === PREVIOUS_WORKTREE_SELECT_VALUE) {
78 onUsePreviousWorktree?.();
79 return;
80 }
81 onEnvModeChange(value as EnvMode);
82 }}
83 items={envModeItems}
84 >
85 <SelectTrigger
86 variant="ghost"
87 size="xs"
88 className="min-w-0 shrink font-medium"
89 aria-label="Workspace"
90 data-composer-context-control
91 >
92 {effectiveEnvMode === "worktree" ? (
93 <FolderGit2Icon className="size-3" />
94 ) : activeWorktreePath ? (
95 <FolderGitIcon className="size-3" />
96 ) : (
97 <FolderIcon className="size-3" />
98 )}
99 <span
100 data-composer-label
101 className="min-w-0 max-w-[240px] group-data-[compact]/composer-context:max-w-0"
102 >
103 <span
104 data-composer-label-motion
105 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"
106 >
107 <SelectValue />
108 </span>
109 </span>
110 </SelectTrigger>
111 <SelectPopup>
112 <SelectGroup>
113 <SelectGroupLabel>Workspace</SelectGroupLabel>
114 <SelectItem value="local">
115 <span className="inline-flex items-center gap-1.5">
116 {activeWorktreePath ? (
117 <FolderGitIcon className="size-3" />
118 ) : (
119 <FolderIcon className="size-3" />
120 )}
121 {resolveCurrentWorkspaceLabel(activeWorktreePath)}
122 </span>
123 </SelectItem>
124 <SelectItem value="worktree">
125 <span className="inline-flex items-center gap-1.5">
126 <FolderGit2Icon className="size-3" />
127 {resolveEnvModeLabel("worktree")}
128 </span>
129 </SelectItem>
130 {showPreviousWorktree && previousWorktreeLabel ? (
131 <SelectItem value={PREVIOUS_WORKTREE_SELECT_VALUE}>
132 <span className="inline-flex items-center gap-1.5">
133 <HistoryIcon className="size-3" />
134 {previousWorktreeLabel}
135 </span>
136 </SelectItem>
137 ) : null}
138 </SelectGroup>
139 </SelectPopup>
140 </Select>
141 );
142});