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

BranchToolbarEnvModeSelector.tsx

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