From 75430230c1cb10f66b5e1a09284885db04a85b7b Mon Sep 17 00:00:00 2001 From: Chris Scott Date: Sun, 21 Dec 2025 14:26:46 -0500 Subject: [PATCH] Use API as source of truth for branch switcher current branch --- .../src/components/repo/BranchSwitcher.tsx | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/frontend/src/components/repo/BranchSwitcher.tsx b/frontend/src/components/repo/BranchSwitcher.tsx index 4a756c9b..a5e3aca5 100644 --- a/frontend/src/components/repo/BranchSwitcher.tsx +++ b/frontend/src/components/repo/BranchSwitcher.tsx @@ -38,6 +38,8 @@ export function BranchSwitcher({ repoId, currentBranch, isWorktree, repoUrl, rep staleTime: 1000 * 30, }); + const activeBranch = branches?.current ?? currentBranch; + const handleDropdownOpenChange = useCallback((open: boolean) => { if (open && repoId) { refetchBranches(); @@ -46,10 +48,11 @@ export function BranchSwitcher({ repoId, currentBranch, isWorktree, repoUrl, rep const switchBranchMutation = useMutation({ mutationFn: (branch: string) => switchBranch(repoId, branch), - onSuccess: (updatedRepo) => { + onSuccess: async (updatedRepo) => { queryClient.setQueryData(["repo", repoId], updatedRepo); - queryClient.invalidateQueries({ queryKey: ["branches", repoId] }); queryClient.invalidateQueries({ queryKey: ["repos"] }); + await refetchBranches(); + showToast.success(`Switched to branch: ${updatedRepo.currentBranch}`); }, onError: (error) => { if (error instanceof GitAuthError) { @@ -60,8 +63,7 @@ export function BranchSwitcher({ repoId, currentBranch, isWorktree, repoUrl, rep }, }); - const isCurrentBranchInList = branches?.all?.includes(currentBranch) ?? false; - const showLoading = switchBranchMutation.isPending || (branchesLoading && !isCurrentBranchInList); + const showLoading = switchBranchMutation.isPending || branchesLoading; return ( <> @@ -78,7 +80,7 @@ export function BranchSwitcher({ repoId, currentBranch, isWorktree, repoUrl, rep ) : ( )} - {!iconOnly && {currentBranch}} + {!iconOnly && {activeBranch}} @@ -87,7 +89,7 @@ export function BranchSwitcher({ repoId, currentBranch, isWorktree, repoUrl, rep
- Worktree: {currentBranch} + Worktree: {activeBranch}
@@ -123,7 +125,7 @@ export function BranchSwitcher({ repoId, currentBranch, isWorktree, repoUrl, rep
{branch} - {branch === currentBranch && } + {branch === activeBranch && }
)) @@ -179,7 +181,7 @@ export function BranchSwitcher({ repoId, currentBranch, isWorktree, repoUrl, rep isOpen={gitChangesOpen} onClose={() => setGitChangesOpen(false)} repoId={repoId} - currentBranch={currentBranch} + currentBranch={activeBranch} repoLocalPath={repoLocalPath} />