fix(frontend): resolve the deepest owning remote project and refresh VCS info after branch changes

findRemoteProjectForDirectory now returns the longest matching canonical path, so nested linked-server projects attribute sessions to the innermost project. Switching, creating and renaming a local branch invalidate that directory's VCS info query, so branch labels refresh immediately.
This commit is contained in:
Chris Scott committed 2026-10-24 10:37:41 -04:00
1 parent 4b7652451b
commit adb251081f
8 files changed
+128 -15

No files matched your search

+8
View File
@@ -66,6 +66,7 @@ vi.mock('@tanstack/react-query', async () => {
invalidateQueries: (...args: unknown[]) => unknown
setQueryData: (...args: unknown[]) => unknown
setQueriesData: (...args: unknown[]) => unknown
getQueryData: (...args: unknown[]) => unknown
} | null
return {
invalidateQueries: (...args: unknown[]) => {
@@ -80,6 +81,7 @@ vi.mock('@tanstack/react-query', async () => {
mockSetQueriesData(...args)
return real?.setQueriesData(...args)
},
getQueryData: (...args: unknown[]) => real?.getQueryData(...args),
}
})
}
@@ -335,6 +337,8 @@ describe('useGit', () => {
describe('renameBranch mutation', () => {
it('calls correct API and updates the status cache on success', async () => {
const realClient = mockRealQueryClient.current as QueryClient
realClient.setQueryData(['repo', 1], { fullPath: '/abs/repos/my-repo' })
const { result } = renderHook(() => useGit(1), { wrapper: createWrapper() })
await waitFor(() => {
@@ -343,6 +347,10 @@ describe('useGit', () => {
expect(gitApi.gitRenameBranch).toHaveBeenCalledWith(1, { from: 'main', to: 'renamed' })
expectTargetedStatusCacheUpdate()
expect(mockInvalidateQueries).toHaveBeenCalledWith({
queryKey: ['opencode', 'vcs', '/abs/repos/my-repo', 'info'],
exact: true,
})
expect(toast.showToast.success).toHaveBeenCalledWith('Branch renamed')
})
+11 -6
View File
@@ -3,9 +3,10 @@ import type { QueryClient } from '@tanstack/react-query'
import { gitFetch, gitPull, gitPush, gitCommit, gitStageFiles, gitUnstageFiles, gitDiscardFiles, fetchGitLog, fetchGitDiff, gitReset, gitRenameBranch, gitDeleteBranch, gitStashPush, gitStashApply, gitStashDrop, gitContinueOperation, gitAbortOperation, gitGenerateCommitMessage, gitIntegrateBranch, getApiErrorMessage, fetchGitStatus } from '@/api/git'
import { createBranch } from '@/api/repos'
import type { DeleteBranchRequest, DeleteBranchResult, IntegrateBranchRequest, RenameBranchRequest, StashPushRequest } from '@opencode-manager/shared'
import type { Repo } from '@/api/types'
import type { GitStatusResponse } from '@/types/git'
import { showToast } from '@/lib/toast'
import { invalidateRepoGitCaches, setRepoGitStatusCaches } from '@/lib/queryInvalidation'
import { invalidateRepoGitCaches, invalidateVcsInfoCache, setRepoGitStatusCaches } from '@/lib/queryInvalidation'
interface RepoGitMutationConfig<TData, TVariables> {
mutationFn: (repoId: number, variables: TVariables) => Promise<TData>
@@ -160,12 +161,13 @@ export function useGit(repoId: number | undefined, onError?: (error: unknown) =>
const createBranchMutation = useMutation({
mutationFn: async (branchName: string) => {
if (!repoId) throw new Error('No repo ID')
await createBranch(repoId, branchName)
return fetchGitStatus(repoId)
const createdRepo = await createBranch(repoId, branchName)
return { createdRepo, status: await fetchGitStatus(repoId) }
},
onSuccess: (data) => {
if (repoId) setRepoGitStatusCaches(queryClient, repoId, data)
onSuccess: ({ createdRepo, status }) => {
if (repoId) setRepoGitStatusCaches(queryClient, repoId, status)
invalidateRepoGitCaches(queryClient, repoId, { invalidateStatus: false })
invalidateVcsInfoCache(queryClient, createdRepo.fullPath)
showToast.success('Branch created')
},
onError: handleError,
@@ -191,7 +193,10 @@ export function useGit(repoId: number | undefined, onError?: (error: unknown) =>
{
mutationFn: gitRenameBranch,
statusFromData: (data) => data,
invalidate: (client, id) => invalidateRepoGitCaches(client, id, { invalidateStatus: false }),
invalidate: (client, id) => {
invalidateRepoGitCaches(client, id, { invalidateStatus: false })
invalidateVcsInfoCache(client, client.getQueryData<Repo>(['repo', id])?.fullPath)
},
successMessage: 'Branch renamed',
},
)
+2 -2
View File
@@ -1,6 +1,6 @@
import { useQuery } from '@tanstack/react-query'
import { getVcsInfo } from '@/api/opencode'
import { serverScopedKey } from '@/lib/serverScope'
import { vcsInfoQueryKey } from '@/lib/queryInvalidation'
export interface UseVcsInfoOptions {
enabled?: boolean
@@ -14,7 +14,7 @@ export function useVcsInfo(
const { enabled = true } = options
return useQuery({
queryKey: [...serverScopedKey(['opencode', 'vcs', directory ?? ''], serverId), 'info'],
queryKey: vcsInfoQueryKey(directory, serverId),
queryFn: () => getVcsInfo(directory ?? '', serverId),
enabled: enabled && Boolean(directory),
staleTime: 30_000,