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

BranchToolbarEnvModeSelector.tsx

150 lines5.0 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 { useComposerMenuProps } 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;
31​
32export const BranchToolbarEnvModeSelector = memo(function BranchToolbarEnvModeSelector({
33 envLocked,
34 effectiveEnvMode,
35 activeWorktreePath,
36 onEnvModeChange,
37 previousWorktreeLabel,
38 onUsePreviousWorktree,
39}: BranchToolbarEnvModeSelectorProps) {
40 const composerFloatingLayerProps = useComposerMenuProps();
41 const showPreviousWorktree = Boolean(previousWorktreeLabel && onUsePreviousWorktree);
42 const envModeItems = useMemo(
43 () => [
44 { value: "local", label: resolveCurrentWorkspaceLabel(activeWorktreePath) },
45 { value: "worktree", label: resolveEnvModeLabel("worktree") },
46 ...(showPreviousWorktree && previousWorktreeLabel
47 ? [{ value: PREVIOUS_WORKTREE_SELECT_VALUE, label: previousWorktreeLabel }]
48 : []),
49 ],
50 [activeWorktreePath, previousWorktreeLabel, showPreviousWorktree],
51 );
52​
53 if (envLocked) {
54 return (
55 <span
56 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"
57 data-composer-context-control
58 >
59 {activeWorktreePath ? (
60 <FolderGitIcon className="size-3 shrink-0" />
61 ) : (
62 <FolderIcon className="size-3 shrink-0" />
63 )}
64 <span
65 data-composer-label
66 className="min-w-0 max-w-[240px] group-data-[compact]/composer-context:max-w-0"
67 >
68 <span
69 data-composer-label-motion
70 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"
71 >
72 {resolveLockedWorkspaceLabel(activeWorktreePath)}
73 </span>
74 </span>
75 </span>
76 );
77 }
78​
79 return (
80 <Select
81 modal={false}
82 value={effectiveEnvMode}
83 onValueChange={(value: string | null) => {
84 if (value === PREVIOUS_WORKTREE_SELECT_VALUE) {
85 onUsePreviousWorktree?.();
86 return;
87 }
88 onEnvModeChange(value as EnvMode);
89 }}
90 items={envModeItems}
91 >
92 <SelectTrigger
93 variant="ghost"
94 size="xs"
95 className="min-w-0 shrink font-normal text-xs!"
96 aria-label="Workspace"
97 data-composer-shortcut="composer.workspace"
98 data-composer-context-control
99 >
100 {effectiveEnvMode === "worktree" ? (
101 <FolderGit2Icon className="size-3" />
102 ) : activeWorktreePath ? (
103 <FolderGitIcon className="size-3" />
104 ) : (
105 <FolderIcon className="size-3" />
106 )}
107 <span
108 data-composer-label
109 className="min-w-0 max-w-[240px] group-data-[compact]/composer-context:max-w-0"
110 >
111 <span
112 data-composer-label-motion
113 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"
114 >
115 <SelectValue />
116 </span>
117 </span>
118 </SelectTrigger>
119 <SelectPopup alignItemWithTrigger={false} {...composerFloatingLayerProps}>
120 <SelectGroup>
121 <SelectGroupLabel>Workspace</SelectGroupLabel>
122 <SelectItem value="local">
123 <span className="inline-flex items-center gap-1.5">
124 {activeWorktreePath ? (
125 <FolderGitIcon className="size-3" />
126 ) : (
127 <FolderIcon className="size-3" />
128 )}
129 {resolveCurrentWorkspaceLabel(activeWorktreePath)}
130 </span>
131 </SelectItem>
132 <SelectItem value="worktree">
133 <span className="inline-flex items-center gap-1.5">
134 <FolderGit2Icon className="size-3" />
135 {resolveEnvModeLabel("worktree")}
136 </span>
137 </SelectItem>
138 {showPreviousWorktree && previousWorktreeLabel ? (
139 <SelectItem value={PREVIOUS_WORKTREE_SELECT_VALUE}>
140 <span className="inline-flex items-center gap-1.5">
141 <HistoryIcon className="size-3" />
142 {previousWorktreeLabel}
143 </span>
144 </SelectItem>
145 ) : null}
146 </SelectGroup>
147 </SelectPopup>
148 </Select>
149 );
150});