| ... | @@ -1,7 +1,7 @@ | ... | @@ -1,7 +1,7 @@ |
| 1 | import type { GitBranch, ThreadId } from "@t3tools/contracts"; | 1 | import type { GitBranch, ThreadId } from "@t3tools/contracts"; |
| 2 | import { useQuery, useQueryClient } from "@tanstack/react-query"; | 2 | import { useQuery, useQueryClient } from "@tanstack/react-query"; |
| 3 | import { useVirtualizer } from "@tanstack/react-virtual"; | 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 | import { newCommandId } from "../lib/utils"; | 6 | import { newCommandId } from "../lib/utils"; |
| 7 | import { gitBranchesQueryOptions, invalidateGitQueries } from "../lib/gitReactQuery"; | 7 | import { gitBranchesQueryOptions, invalidateGitQueries } from "../lib/gitReactQuery"; |
| ... | @@ -11,6 +11,7 @@ import { useStore } from "../store"; | ... | @@ -11,6 +11,7 @@ import { useStore } from "../store"; |
| 11 | import { | 11 | import { |
| 12 | dedupeRemoteBranchesWithLocalMatches, | 12 | dedupeRemoteBranchesWithLocalMatches, |
| 13 | deriveLocalBranchNameFromRemoteRef, | 13 | deriveLocalBranchNameFromRemoteRef, |
| | 14 | resolveBranchToolbarValue, |
| 14 | } from "./BranchToolbar.logic"; | 15 | } from "./BranchToolbar.logic"; |
| 15 | import { Button } from "./ui/button"; | 16 | import { Button } from "./ui/button"; |
| 16 | import { | 17 | import { |
| ... | @@ -49,7 +50,7 @@ export default function BranchToolbar({ | ... | @@ -49,7 +50,7 @@ export default function BranchToolbar({ |
| 49 | | 50 | |
| 50 | const [isBranchMenuOpen, setIsBranchMenuOpen] = useState(false); | 51 | const [isBranchMenuOpen, setIsBranchMenuOpen] = useState(false); |
| 51 | const [branchQuery, setBranchQuery] = useState(""); | 52 | const [branchQuery, setBranchQuery] = useState(""); |
| 52 | const branchListRef = useRef<HTMLDivElement | null>(null); | 53 | const branchListScrollElementRef = useRef<HTMLDivElement | null>(null); |
| 53 | | 54 | |
| 54 | const serverThread = threads.find((thread) => thread.id === threadId); | 55 | const serverThread = threads.find((thread) => thread.id === threadId); |
| 55 | const activeProjectId = serverThread?.projectId ?? draftThread?.projectId ?? null; | 56 | const activeProjectId = serverThread?.projectId ?? draftThread?.projectId ?? null; |
| ... | @@ -73,10 +74,12 @@ export default function BranchToolbar({ | ... | @@ -73,10 +74,12 @@ export default function BranchToolbar({ |
| 73 | [branchesQuery.data?.branches], | 74 | [branchesQuery.data?.branches], |
| 74 | ); | 75 | ); |
| 75 | const currentGitBranch = branches.find((branch) => branch.current)?.name ?? null; | 76 | const currentGitBranch = branches.find((branch) => branch.current)?.name ?? null; |
| 76 | const canonicalActiveBranch = | 77 | const canonicalActiveBranch = resolveBranchToolbarValue({ |
| 77 | effectiveEnvMode === "worktree" && !activeWorktreePath | 78 | envMode: effectiveEnvMode, |
| 78 | ? activeThreadBranch | 79 | activeWorktreePath, |
| 79 | : (currentGitBranch ?? activeThreadBranch); | 80 | activeThreadBranch, |
| | 81 | currentGitBranch, |
| | 82 | }); |
| 80 | const branchNames = useMemo(() => branches.map((branch) => branch.name), [branches]); | 83 | const branchNames = useMemo(() => branches.map((branch) => branch.name), [branches]); |
| 81 | const branchByName = useMemo( | 84 | const branchByName = useMemo( |
| 82 | () => new Map(branches.map((branch) => [branch.name, branch] as const)), | 85 | () => new Map(branches.map((branch) => [branch.name, branch] as const)), |
| ... | @@ -121,50 +124,89 @@ export default function BranchToolbar({ | ... | @@ -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 | const branchListVirtualizer = useVirtualizer({ | 175 | const branchListVirtualizer = useVirtualizer({ |
| 125 | count: filteredBranchPickerItems.length, | 176 | count: filteredBranchPickerItems.length, |
| 126 | estimateSize: () => 28, | 177 | estimateSize: () => 28, |
| 127 | getScrollElement: () => branchListRef.current?.parentElement ?? null, | 178 | getScrollElement: () => branchListScrollElementRef.current, |
| 128 | overscan: 12, | 179 | overscan: 12, |
| 129 | enabled: isBranchMenuOpen, | 180 | enabled: isBranchMenuOpen, |
| | 181 | initialRect: { |
| | 182 | height: 224, |
| | 183 | width: 0, |
| | 184 | }, |
| 130 | }); | 185 | }); |
| 131 | const virtualBranchRows = branchListVirtualizer.getVirtualItems(); | 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) => { | 197 | useEffect(() => { |
| 134 | if (!activeThreadId) return; | 198 | if (effectiveEnvMode !== "worktree" || activeWorktreePath || activeThreadBranch || !currentGitBranch) { |
| 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); | | |
| 160 | return; | 199 | return; |
| 161 | } | 200 | } |
| 162 | setDraftThreadContext(threadId, { | 201 | setThreadBranch(currentGitBranch, null); |
| 163 | branch, | 202 | }, [activeThreadBranch, activeWorktreePath, currentGitBranch, effectiveEnvMode, setThreadBranch]); |
| 164 | worktreePath, | 203 | |
| 165 | envMode: effectiveEnvMode, | 204 | useEffect(() => { |
| | 205 | if (!isBranchMenuOpen) return; |
| | 206 | queueMicrotask(() => { |
| | 207 | branchListVirtualizer.measure(); |
| 166 | }); | 208 | }); |
| 167 | }; | 209 | }, [branchListVirtualizer, filteredBranchPickerItems.length, isBranchMenuOpen]); |
| 168 | | 210 | |
| 169 | const selectBranch = (branch: GitBranch) => { | 211 | const selectBranch = (branch: GitBranch) => { |
| 170 | const api = readNativeApi(); | 212 | const api = readNativeApi(); |
| ... | @@ -296,7 +338,6 @@ export default function BranchToolbar({ | ... | @@ -296,7 +338,6 @@ export default function BranchToolbar({ |
| 296 | onOpenChange={(open) => { | 338 | onOpenChange={(open) => { |
| 297 | setIsBranchMenuOpen(open); | 339 | setIsBranchMenuOpen(open); |
| 298 | if (!open) setBranchQuery(""); | 340 | if (!open) setBranchQuery(""); |
| 299 | else void invalidateGitQueries(queryClient); | | |
| 300 | }} | 341 | }} |
| 301 | open={isBranchMenuOpen} | 342 | open={isBranchMenuOpen} |
| 302 | value={resolvedActiveBranch} | 343 | value={resolvedActiveBranch} |
| ... | @@ -329,7 +370,7 @@ export default function BranchToolbar({ | ... | @@ -329,7 +370,7 @@ export default function BranchToolbar({ |
| 329 | </div> | 370 | </div> |
| 330 | <ComboboxEmpty>No branches found.</ComboboxEmpty> | 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 | <div | 374 | <div |
| 334 | className="relative" | 375 | className="relative" |
| 335 | style={{ | 376 | style={{ |