| ... | ... | @@ -1,7 +1,7 @@ |
| 1 | 1 | import type { GitBranch, ThreadId } from "@t3tools/contracts"; |
| 2 | 2 | import { useQuery, useQueryClient } from "@tanstack/react-query"; |
| 3 | 3 | import { useVirtualizer } from "@tanstack/react-virtual"; |
| 4 | | import { useMemo, useOptimistic, useRef, useState, useTransition } from "react"; |
| 4 | import { useCallback, useEffect, useMemo, useOptimistic, useRef, useState, useTransition } from "react"; |
| 5 | 5 | |
| 6 | 6 | import { newCommandId } from "../lib/utils"; |
| 7 | 7 | import { gitBranchesQueryOptions, invalidateGitQueries } from "../lib/gitReactQuery"; |
| ... | ... | @@ -11,6 +11,7 @@ import { useStore } from "../store"; |
| 11 | 11 | import { |
| 12 | 12 | dedupeRemoteBranchesWithLocalMatches, |
| 13 | 13 | deriveLocalBranchNameFromRemoteRef, |
| 14 | resolveBranchToolbarValue, |
| 14 | 15 | } from "./BranchToolbar.logic"; |
| 15 | 16 | import { Button } from "./ui/button"; |
| 16 | 17 | import { |
| ... | ... | @@ -49,7 +50,7 @@ export default function BranchToolbar({ |
| 49 | 50 | |
| 50 | 51 | const [isBranchMenuOpen, setIsBranchMenuOpen] = useState(false); |
| 51 | 52 | const [branchQuery, setBranchQuery] = useState(""); |
| 52 | | const branchListRef = useRef<HTMLDivElement | null>(null); |
| 53 | const branchListScrollElementRef = useRef<HTMLDivElement | null>(null); |
| 53 | 54 | |
| 54 | 55 | const serverThread = threads.find((thread) => thread.id === threadId); |
| 55 | 56 | const activeProjectId = serverThread?.projectId ?? draftThread?.projectId ?? null; |
| ... | ... | @@ -73,10 +74,12 @@ export default function BranchToolbar({ |
| 73 | 74 | [branchesQuery.data?.branches], |
| 74 | 75 | ); |
| 75 | 76 | const currentGitBranch = branches.find((branch) => branch.current)?.name ?? null; |
| 76 | | const canonicalActiveBranch = |
| 77 | | effectiveEnvMode === "worktree" && !activeWorktreePath |
| 78 | | ? activeThreadBranch |
| 79 | | : (currentGitBranch ?? activeThreadBranch); |
| 77 | const canonicalActiveBranch = resolveBranchToolbarValue({ |
| 78 | envMode: effectiveEnvMode, |
| 79 | activeWorktreePath, |
| 80 | activeThreadBranch, |
| 81 | currentGitBranch, |
| 82 | }); |
| 80 | 83 | const branchNames = useMemo(() => branches.map((branch) => branch.name), [branches]); |
| 81 | 84 | const branchByName = useMemo( |
| 82 | 85 | () => new Map(branches.map((branch) => [branch.name, branch] as const)), |
| ... | ... | @@ -121,50 +124,89 @@ export default function BranchToolbar({ |
| 121 | 124 | }); |
| 122 | 125 | }; |
| 123 | 126 | |
| 127 | const setThreadBranch = useCallback( |
| 128 | (branch: string | null, worktreePath: string | null) => { |
| 129 | if (!activeThreadId) return; |
| 130 | const api = readNativeApi(); |
| 131 | // If the effective cwd is about to change, stop the running session so the |
| 132 | // next message creates a new one with the correct cwd. |
| 133 | const sessionId = serverThread?.session?.sessionId; |
| 134 | if (sessionId && worktreePath !== activeWorktreePath && api) { |
| 135 | void api.orchestration |
| 136 | .dispatchCommand({ |
| 137 | type: "thread.session.stop", |
| 138 | commandId: newCommandId(), |
| 139 | threadId: activeThreadId, |
| 140 | createdAt: new Date().toISOString(), |
| 141 | }) |
| 142 | .catch(() => undefined); |
| 143 | } |
| 144 | if (api && hasServerThread) { |
| 145 | void api.orchestration.dispatchCommand({ |
| 146 | type: "thread.meta.update", |
| 147 | commandId: newCommandId(), |
| 148 | threadId: activeThreadId, |
| 149 | branch, |
| 150 | worktreePath, |
| 151 | }); |
| 152 | } |
| 153 | if (hasServerThread) { |
| 154 | setThreadBranchAction(activeThreadId, branch, worktreePath); |
| 155 | return; |
| 156 | } |
| 157 | setDraftThreadContext(threadId, { |
| 158 | branch, |
| 159 | worktreePath, |
| 160 | envMode: effectiveEnvMode, |
| 161 | }); |
| 162 | }, |
| 163 | [ |
| 164 | activeThreadId, |
| 165 | serverThread?.session?.sessionId, |
| 166 | activeWorktreePath, |
| 167 | hasServerThread, |
| 168 | setThreadBranchAction, |
| 169 | setDraftThreadContext, |
| 170 | threadId, |
| 171 | effectiveEnvMode, |
| 172 | ], |
| 173 | ); |
| 174 | |
| 124 | 175 | const branchListVirtualizer = useVirtualizer({ |
| 125 | 176 | count: filteredBranchPickerItems.length, |
| 126 | 177 | estimateSize: () => 28, |
| 127 | | getScrollElement: () => branchListRef.current?.parentElement ?? null, |
| 178 | getScrollElement: () => branchListScrollElementRef.current, |
| 128 | 179 | overscan: 12, |
| 129 | 180 | enabled: isBranchMenuOpen, |
| 181 | initialRect: { |
| 182 | height: 224, |
| 183 | width: 0, |
| 184 | }, |
| 130 | 185 | }); |
| 131 | 186 | const virtualBranchRows = branchListVirtualizer.getVirtualItems(); |
| 187 | const setBranchListRef = useCallback( |
| 188 | (element: HTMLDivElement | null) => { |
| 189 | branchListScrollElementRef.current = (element?.parentElement as HTMLDivElement | null) ?? null; |
| 190 | if (element) { |
| 191 | branchListVirtualizer.measure(); |
| 192 | } |
| 193 | }, |
| 194 | [branchListVirtualizer], |
| 195 | ); |
| 132 | 196 | |
| 133 | | const setThreadBranch = (branch: string | null, worktreePath: string | null) => { |
| 134 | | if (!activeThreadId) return; |
| 135 | | const api = readNativeApi(); |
| 136 | | // If the effective cwd is about to change, stop the running session so the |
| 137 | | // next message creates a new one with the correct cwd. |
| 138 | | const sessionId = serverThread?.session?.sessionId; |
| 139 | | if (sessionId && worktreePath !== activeWorktreePath && api) { |
| 140 | | void api.orchestration |
| 141 | | .dispatchCommand({ |
| 142 | | type: "thread.session.stop", |
| 143 | | commandId: newCommandId(), |
| 144 | | threadId: activeThreadId, |
| 145 | | createdAt: new Date().toISOString(), |
| 146 | | }) |
| 147 | | .catch(() => undefined); |
| 148 | | } |
| 149 | | if (api && hasServerThread) { |
| 150 | | void api.orchestration.dispatchCommand({ |
| 151 | | type: "thread.meta.update", |
| 152 | | commandId: newCommandId(), |
| 153 | | threadId: activeThreadId, |
| 154 | | branch, |
| 155 | | worktreePath, |
| 156 | | }); |
| 157 | | } |
| 158 | | if (hasServerThread) { |
| 159 | | setThreadBranchAction(activeThreadId, branch, worktreePath); |
| 197 | useEffect(() => { |
| 198 | if (effectiveEnvMode !== "worktree" || activeWorktreePath || activeThreadBranch || !currentGitBranch) { |
| 160 | 199 | return; |
| 161 | 200 | } |
| 162 | | setDraftThreadContext(threadId, { |
| 163 | | branch, |
| 164 | | worktreePath, |
| 165 | | envMode: effectiveEnvMode, |
| 201 | setThreadBranch(currentGitBranch, null); |
| 202 | }, [activeThreadBranch, activeWorktreePath, currentGitBranch, effectiveEnvMode, setThreadBranch]); |
| 203 | |
| 204 | useEffect(() => { |
| 205 | if (!isBranchMenuOpen) return; |
| 206 | queueMicrotask(() => { |
| 207 | branchListVirtualizer.measure(); |
| 166 | 208 | }); |
| 167 | | }; |
| 209 | }, [branchListVirtualizer, filteredBranchPickerItems.length, isBranchMenuOpen]); |
| 168 | 210 | |
| 169 | 211 | const selectBranch = (branch: GitBranch) => { |
| 170 | 212 | const api = readNativeApi(); |
| ... | ... | @@ -296,7 +338,6 @@ export default function BranchToolbar({ |
| 296 | 338 | onOpenChange={(open) => { |
| 297 | 339 | setIsBranchMenuOpen(open); |
| 298 | 340 | if (!open) setBranchQuery(""); |
| 299 | | else void invalidateGitQueries(queryClient); |
| 300 | 341 | }} |
| 301 | 342 | open={isBranchMenuOpen} |
| 302 | 343 | value={resolvedActiveBranch} |
| ... | ... | @@ -329,7 +370,7 @@ export default function BranchToolbar({ |
| 329 | 370 | </div> |
| 330 | 371 | <ComboboxEmpty>No branches found.</ComboboxEmpty> |
| 331 | 372 | |
| 332 | | <ComboboxList ref={branchListRef} className="max-h-56"> |
| 373 | <ComboboxList ref={setBranchListRef} className="max-h-56"> |
| 333 | 374 | <div |
| 334 | 375 | className="relative" |
| 335 | 376 | style={{ |