-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathBranchSelector.tsx
More file actions
226 lines (210 loc) · 7.39 KB
/
BranchSelector.tsx
File metadata and controls
226 lines (210 loc) · 7.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import { Combobox } from "@components/ui/combobox/Combobox";
import { useGitInteractionStore } from "@features/git-interaction/state/gitInteractionStore";
import { getSuggestedBranchName } from "@features/git-interaction/utils/getSuggestedBranchName";
import { invalidateGitBranchQueries } from "@features/git-interaction/utils/gitCacheKeys";
import { GitBranch, Plus } from "@phosphor-icons/react";
import { Flex, Spinner, Tooltip } from "@radix-ui/themes";
import { useTRPC } from "@renderer/trpc";
import { toast } from "@renderer/utils/toast";
import { useMutation, useQuery } from "@tanstack/react-query";
import { useEffect, useState } from "react";
interface BranchSelectorProps {
repoPath: string | null;
currentBranch: string | null;
defaultBranch?: string | null;
disabled?: boolean;
loading?: boolean;
variant?: "outline" | "ghost";
workspaceMode?: "worktree" | "local" | "cloud";
selectedBranch?: string | null;
onBranchSelect?: (branch: string | null) => void;
cloudBranches?: string[];
cloudBranchesLoading?: boolean;
cloudBranchesFetchingMore?: boolean;
onCloudPickerOpen?: () => void;
onCloudBranchCommit?: () => void;
taskId?: string;
}
export function BranchSelector({
repoPath,
currentBranch,
defaultBranch,
disabled,
loading,
variant = "outline",
workspaceMode,
selectedBranch,
onBranchSelect,
cloudBranches,
cloudBranchesLoading,
cloudBranchesFetchingMore,
onCloudPickerOpen,
onCloudBranchCommit,
taskId,
}: BranchSelectorProps) {
const [open, setOpen] = useState(false);
const trpc = useTRPC();
const { actions } = useGitInteractionStore();
const isCloudMode = workspaceMode === "cloud";
const isSelectionOnly = workspaceMode === "worktree" || isCloudMode;
const displayedBranch = isSelectionOnly ? selectedBranch : currentBranch;
useEffect(() => {
if (isSelectionOnly && defaultBranch && !selectedBranch && onBranchSelect) {
onBranchSelect(defaultBranch);
}
}, [isSelectionOnly, defaultBranch, selectedBranch, onBranchSelect]);
const { data: localBranches = [] } = useQuery(
trpc.git.getAllBranches.queryOptions(
{ directoryPath: repoPath as string },
{ enabled: !isCloudMode && !!repoPath && open, staleTime: 10_000 },
),
);
const branches = isCloudMode ? (cloudBranches ?? []) : localBranches;
const effectiveLoading = loading || (isCloudMode && cloudBranchesLoading);
const cloudStillLoading =
isCloudMode && cloudBranchesLoading && branches.length === 0;
const checkoutMutation = useMutation(
trpc.git.checkoutBranch.mutationOptions({
onSuccess: () => {
if (repoPath) invalidateGitBranchQueries(repoPath);
},
onError: (error, { branchName }) => {
const message =
error instanceof Error ? error.message : "Unknown error occurred";
toast.error(`Failed to checkout ${branchName}`, {
description: message,
});
},
}),
);
const handleBranchChange = (value: string) => {
if (isSelectionOnly) {
onBranchSelect?.(value || null);
} else if (value && value !== currentBranch) {
checkoutMutation.mutate({
directoryPath: repoPath as string,
branchName: value,
});
}
if (isCloudMode && value) {
// User committed to a branch — pause the background pagination. If they
// later re-open the picker, `onCloudPickerOpen` will resume it from
// wherever the cached pages left off.
onCloudBranchCommit?.();
}
setOpen(false);
};
const handleOpenChange = (next: boolean) => {
setOpen(next);
if (isCloudMode && next) {
onCloudPickerOpen?.();
}
};
const displayText = effectiveLoading
? "Loading..."
: (displayedBranch ?? "No branch");
// Show the spinner on the trigger while the first page is still loading.
// Once we have branches to show, any "loading more" background work is
// surfaced inside the open picker instead, so the trigger goes back to its
// normal branch icon.
const showSpinner =
effectiveLoading || (isCloudMode && open && cloudBranchesFetchingMore);
const triggerContent = (
<Flex align="center" gap="2" style={{ minWidth: 0 }}>
{showSpinner ? (
<Spinner size="1" />
) : (
<GitBranch size={16} weight="regular" style={{ flexShrink: 0 }} />
)}
<span className="combobox-trigger-text">{displayText}</span>
</Flex>
);
return (
<Tooltip content={displayedBranch} delayDuration={300}>
<Combobox.Root
value={displayedBranch ?? ""}
onValueChange={handleBranchChange}
open={open}
onOpenChange={handleOpenChange}
size="1"
disabled={disabled || !repoPath || cloudStillLoading}
>
<Combobox.Trigger variant={variant} placeholder="No branch">
{triggerContent}
</Combobox.Trigger>
<Combobox.Content
items={branches}
limit={50}
pinned={[displayedBranch, defaultBranch].filter(Boolean) as string[]}
>
{({ filtered, hasMore, moreCount }) => (
<>
<Combobox.Input placeholder="Search branches" />
{isCloudMode && cloudBranchesFetchingMore && (
<Flex
align="center"
gap="1"
className="combobox-label"
style={{ padding: "6px 8px" }}
>
<Spinner size="1" />
Loading more ({branches.length})…
</Flex>
)}
<Combobox.Empty>No branches found.</Combobox.Empty>
{filtered.length > 0 && (
<Combobox.Group
heading={isCloudMode ? "Remote branches" : "Local branches"}
>
{filtered.map((branch) => (
<Combobox.Item
key={branch}
value={branch}
icon={<GitBranch size={11} weight="regular" />}
>
{branch}
</Combobox.Item>
))}
{hasMore && (
<div className="combobox-label">
{moreCount} more {moreCount === 1 ? "branch" : "branches"}{" "}
— type to filter
</div>
)}
</Combobox.Group>
)}
{!isCloudMode && (
<Combobox.Footer>
<button
type="button"
className="combobox-footer-button"
onClick={() => {
setOpen(false);
actions.openBranch(
taskId
? getSuggestedBranchName(
taskId,
repoPath ?? undefined,
)
: undefined,
);
}}
>
<Flex
align="center"
gap="2"
style={{ color: "var(--accent-11)" }}
>
<Plus size={11} weight="bold" />
<span>Create new branch</span>
</Flex>
</button>
</Combobox.Footer>
)}
</>
)}
</Combobox.Content>
</Combobox.Root>
</Tooltip>
);
}