Update cached branch list after branch checkout
- add `projectGitBranchesToCurrent` helper to promote the checked-out branch and clear previous current flags - optimistically update branches query cache in checkout mutations before invalidation - add tests covering reorder, missing-branch insertion, and undefined cache behavior
This commit is contained in:
1 parent
6708fe996a
commit
ee56d5b6c8
2 files changed
+99
-3
No files matched your search
@@ -0,0 +1,50 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { projectGitBranchesToCurrent } from "./gitReactQuery";
|
||||
|
||||
describe("projectGitBranchesToCurrent", () => {
|
||||
it("moves the checked out branch to the top and marks it current", () => {
|
||||
const result = projectGitBranchesToCurrent(
|
||||
{
|
||||
isRepo: true,
|
||||
branches: [
|
||||
{ name: "main", current: true, isDefault: true, worktreePath: null },
|
||||
{ name: "feature/a", current: false, isDefault: false, worktreePath: null },
|
||||
{ name: "feature/b", current: false, isDefault: false, worktreePath: null },
|
||||
],
|
||||
},
|
||||
"feature/a",
|
||||
);
|
||||
|
||||
expect(result).toEqual({
|
||||
isRepo: true,
|
||||
branches: [
|
||||
{ name: "feature/a", current: true, isDefault: false, worktreePath: null },
|
||||
{ name: "main", current: false, isDefault: true, worktreePath: null },
|
||||
{ name: "feature/b", current: false, isDefault: false, worktreePath: null },
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
it("inserts a missing branch as current", () => {
|
||||
const result = projectGitBranchesToCurrent(
|
||||
{
|
||||
isRepo: true,
|
||||
branches: [{ name: "main", current: true, isDefault: true, worktreePath: null }],
|
||||
},
|
||||
"feature/new",
|
||||
);
|
||||
|
||||
expect(result).toEqual({
|
||||
isRepo: true,
|
||||
branches: [
|
||||
{ name: "feature/new", current: true, isDefault: false, worktreePath: null },
|
||||
{ name: "main", current: false, isDefault: true, worktreePath: null },
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
it("returns undefined when no cached branch list exists yet", () => {
|
||||
expect(projectGitBranchesToCurrent(undefined, "feature/new")).toBeUndefined();
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { GitStackedAction, NativeApi } from "@t3tools/contracts";
|
||||
import type { GitListBranchesResult, GitStackedAction, NativeApi } from "@t3tools/contracts";
|
||||
import { mutationOptions, queryOptions, type QueryClient } from "@tanstack/react-query";
|
||||
|
||||
export const gitQueryKeys = {
|
||||
@@ -11,6 +11,50 @@ export function invalidateGitQueries(queryClient: QueryClient) {
|
||||
return queryClient.invalidateQueries({ queryKey: gitQueryKeys.all });
|
||||
}
|
||||
|
||||
export function projectGitBranchesToCurrent(
|
||||
existing: GitListBranchesResult | undefined,
|
||||
currentBranch: string,
|
||||
): GitListBranchesResult | undefined {
|
||||
if (!existing) return existing;
|
||||
|
||||
let nextCurrent: GitListBranchesResult["branches"][number] | null = null;
|
||||
const remaining: GitListBranchesResult["branches"] = [];
|
||||
|
||||
for (const branch of existing.branches) {
|
||||
if (branch.name === currentBranch) {
|
||||
nextCurrent = branch.current ? branch : { ...branch, current: true };
|
||||
continue;
|
||||
}
|
||||
remaining.push(branch.current ? { ...branch, current: false } : branch);
|
||||
}
|
||||
|
||||
if (!nextCurrent) {
|
||||
nextCurrent = {
|
||||
name: currentBranch,
|
||||
current: true,
|
||||
isDefault: false,
|
||||
worktreePath: null,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
...existing,
|
||||
branches: [nextCurrent, ...remaining],
|
||||
};
|
||||
}
|
||||
|
||||
export function setGitBranchesCurrentBranch(
|
||||
queryClient: QueryClient,
|
||||
cwd: string | null,
|
||||
currentBranch: string,
|
||||
) {
|
||||
if (!cwd) return;
|
||||
|
||||
queryClient.setQueryData<GitListBranchesResult>(gitQueryKeys.branches(cwd), (existing) =>
|
||||
projectGitBranchesToCurrent(existing, currentBranch),
|
||||
);
|
||||
}
|
||||
|
||||
export function gitStatusQueryOptions(api: NativeApi | undefined, cwd: string | null) {
|
||||
return queryOptions({
|
||||
queryKey: gitQueryKeys.status(cwd),
|
||||
@@ -63,7 +107,8 @@ export function gitCheckoutMutationOptions(input: {
|
||||
if (!input.api || !input.cwd) throw new Error("Git checkout is unavailable.");
|
||||
return input.api.git.checkout({ cwd: input.cwd, branch });
|
||||
},
|
||||
onSuccess: async () => {
|
||||
onSuccess: async (_result, branch) => {
|
||||
setGitBranchesCurrentBranch(input.queryClient, input.cwd, branch);
|
||||
await invalidateGitQueries(input.queryClient);
|
||||
},
|
||||
});
|
||||
@@ -80,7 +125,8 @@ export function gitCreateBranchAndCheckoutMutationOptions(input: {
|
||||
await input.api.git.createBranch({ cwd: input.cwd, branch });
|
||||
return input.api.git.checkout({ cwd: input.cwd, branch });
|
||||
},
|
||||
onSuccess: async () => {
|
||||
onSuccess: async (_result, branch) => {
|
||||
setGitBranchesCurrentBranch(input.queryClient, input.cwd, branch);
|
||||
await invalidateGitQueries(input.queryClient);
|
||||
},
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user