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-shortcut="composer.workspace"
97 data-composer-context-control
98 >
99 {effectiveEnvMode === "worktree" ? (
100 <FolderGit2Icon className="size-3" />
101 ) : activeWorktreePath ? (
102 <FolderGitIcon className="size-3" />
103 ) : (
104 <FolderIcon className="size-3" />
105 )}
106 <span
107 data-composer-label
108 className="min-w-0 max-w-[240px] group-data-[compact]/composer-context:max-w-0"
109 >
110 <span
111 data-composer-label-motion
112 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"
113 >
114 <SelectValue />
115 </span>
116 </span>
117 </SelectTrigger>
118 <SelectPopup alignItemWithTrigger={false} {...composerFloatingLayerProps}>
119 <SelectGroup>
120 <SelectGroupLabel>Workspace</SelectGroupLabel>
121 <SelectItem value="local">
122 <span className="inline-flex items-center gap-1.5">
123 {activeWorktreePath ? (
124 <FolderGitIcon className="size-3" />
125 ) : (
126 <FolderIcon className="size-3" />
127 )}
128 {resolveCurrentWorkspaceLabel(activeWorktreePath)}
129 </span>
130 </SelectItem>
131 <SelectItem value="worktree">
132 <span className="inline-flex items-center gap-1.5">
133 <FolderGit2Icon className="size-3" />
134 {resolveEnvModeLabel("worktree")}
135 </span>
136 </SelectItem>
137 {showPreviousWorktree && previousWorktreeLabel ? (
138 <SelectItem value={PREVIOUS_WORKTREE_SELECT_VALUE}>
139 <span className="inline-flex items-center gap-1.5">
140 <HistoryIcon className="size-3" />
141 {previousWorktreeLabel}
142 </span>
143 </SelectItem>
144 ) : null}
145 </SelectGroup>
146 </SelectPopup>
147 </Select>
148 );
149});