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