Scope git query invalidation by cwd (#1670)
This commit is contained in:
1 parent
801dfe5b41
commit
b92c78f552
3 files changed
+84
-5
No files matched your search
@@ -47,6 +47,7 @@ import {
|
||||
gitPullMutationOptions,
|
||||
gitRunStackedActionMutationOptions,
|
||||
gitStatusQueryOptions,
|
||||
invalidateGitStatusQuery,
|
||||
invalidateGitQueries,
|
||||
} from "~/lib/gitReactQuery";
|
||||
import { newCommandId, randomUUID } from "~/lib/utils";
|
||||
@@ -286,8 +287,8 @@ export default function GitActionsControl({ gitCwd, activeThreadId }: GitActions
|
||||
|
||||
useEffect(() => {
|
||||
if (!isGitStatusOutOfSync) return;
|
||||
void invalidateGitQueries(queryClient);
|
||||
}, [isGitStatusOutOfSync, queryClient]);
|
||||
void invalidateGitQueries(queryClient, { cwd: gitCwd });
|
||||
}, [gitCwd, isGitStatusOutOfSync, queryClient]);
|
||||
|
||||
const gitStatusForActions = isGitStatusOutOfSync ? null : gitStatus;
|
||||
|
||||
@@ -814,7 +815,7 @@ export default function GitActionsControl({ gitCwd, activeThreadId }: GitActions
|
||||
<GroupSeparator className="hidden @3xl/header-actions:block" />
|
||||
<Menu
|
||||
onOpenChange={(open) => {
|
||||
if (open) void invalidateGitQueries(queryClient);
|
||||
if (open) void invalidateGitStatusQuery(queryClient, gitCwd);
|
||||
}}
|
||||
>
|
||||
<MenuTrigger
|
||||
|
||||
@@ -1,10 +1,24 @@
|
||||
import { QueryClient } from "@tanstack/react-query";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
vi.mock("../nativeApi", () => ({
|
||||
ensureNativeApi: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("../wsRpcClient", () => ({
|
||||
getWsRpcClient: vi.fn(),
|
||||
}));
|
||||
|
||||
import {
|
||||
gitBranchesQueryOptions,
|
||||
gitMutationKeys,
|
||||
gitQueryKeys,
|
||||
gitPreparePullRequestThreadMutationOptions,
|
||||
gitPullMutationOptions,
|
||||
gitRunStackedActionMutationOptions,
|
||||
invalidateGitStatusQuery,
|
||||
gitStatusQueryOptions,
|
||||
invalidateGitQueries,
|
||||
} from "./gitReactQuery";
|
||||
|
||||
describe("gitMutationKeys", () => {
|
||||
@@ -49,3 +63,51 @@ describe("git mutation options", () => {
|
||||
expect(options.mutationKey).toEqual(gitMutationKeys.preparePullRequestThread("/repo/a"));
|
||||
});
|
||||
});
|
||||
|
||||
describe("invalidateGitQueries", () => {
|
||||
it("can invalidate a single cwd without blasting other git query scopes", async () => {
|
||||
const queryClient = new QueryClient();
|
||||
|
||||
queryClient.setQueryData(gitQueryKeys.status("/repo/a"), { ok: "a" });
|
||||
queryClient.setQueryData(gitQueryKeys.branches("/repo/a"), { ok: "a-branches" });
|
||||
queryClient.setQueryData(gitQueryKeys.status("/repo/b"), { ok: "b" });
|
||||
queryClient.setQueryData(gitQueryKeys.branches("/repo/b"), { ok: "b-branches" });
|
||||
|
||||
await invalidateGitQueries(queryClient, { cwd: "/repo/a" });
|
||||
|
||||
expect(
|
||||
queryClient.getQueryState(gitStatusQueryOptions("/repo/a").queryKey)?.isInvalidated,
|
||||
).toBe(true);
|
||||
expect(
|
||||
queryClient.getQueryState(gitBranchesQueryOptions("/repo/a").queryKey)?.isInvalidated,
|
||||
).toBe(true);
|
||||
expect(
|
||||
queryClient.getQueryState(gitStatusQueryOptions("/repo/b").queryKey)?.isInvalidated,
|
||||
).toBe(false);
|
||||
expect(
|
||||
queryClient.getQueryState(gitBranchesQueryOptions("/repo/b").queryKey)?.isInvalidated,
|
||||
).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("invalidateGitStatusQuery", () => {
|
||||
it("invalidates only status for the selected cwd", async () => {
|
||||
const queryClient = new QueryClient();
|
||||
|
||||
queryClient.setQueryData(gitQueryKeys.status("/repo/a"), { ok: "a" });
|
||||
queryClient.setQueryData(gitQueryKeys.branches("/repo/a"), { ok: "a-branches" });
|
||||
queryClient.setQueryData(gitQueryKeys.status("/repo/b"), { ok: "b" });
|
||||
|
||||
await invalidateGitStatusQuery(queryClient, "/repo/a");
|
||||
|
||||
expect(
|
||||
queryClient.getQueryState(gitStatusQueryOptions("/repo/a").queryKey)?.isInvalidated,
|
||||
).toBe(true);
|
||||
expect(
|
||||
queryClient.getQueryState(gitBranchesQueryOptions("/repo/a").queryKey)?.isInvalidated,
|
||||
).toBe(false);
|
||||
expect(
|
||||
queryClient.getQueryState(gitStatusQueryOptions("/repo/b").queryKey)?.isInvalidated,
|
||||
).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -23,10 +23,26 @@ export const gitMutationKeys = {
|
||||
["git", "mutation", "prepare-pull-request-thread", cwd] as const,
|
||||
};
|
||||
|
||||
export function invalidateGitQueries(queryClient: QueryClient) {
|
||||
export function invalidateGitQueries(queryClient: QueryClient, input?: { cwd?: string | null }) {
|
||||
const cwd = input?.cwd ?? null;
|
||||
if (cwd !== null) {
|
||||
return Promise.all([
|
||||
queryClient.invalidateQueries({ queryKey: gitQueryKeys.status(cwd) }),
|
||||
queryClient.invalidateQueries({ queryKey: gitQueryKeys.branches(cwd) }),
|
||||
]);
|
||||
}
|
||||
|
||||
return queryClient.invalidateQueries({ queryKey: gitQueryKeys.all });
|
||||
}
|
||||
|
||||
export function invalidateGitStatusQuery(queryClient: QueryClient, cwd: string | null) {
|
||||
if (cwd === null) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
return queryClient.invalidateQueries({ queryKey: gitQueryKeys.status(cwd) });
|
||||
}
|
||||
|
||||
export function gitStatusQueryOptions(cwd: string | null) {
|
||||
return queryOptions({
|
||||
queryKey: gitQueryKeys.status(cwd),
|
||||
|
||||
Reference in new issue
Block a user