Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

fix(web): refetch both relation ends after a relation change

Justin Willhite764cb82eae04
diff --git a/apps/web/src/hooks/mutations/task-relation/use-create-task-relation.test.tsx b/apps/web/src/hooks/mutations/task-relation/use-create-task-relation.test.tsx
new file mode 100644
index 000000000..e38e68fdf
--- /dev/null
+++ b/apps/web/src/hooks/mutations/task-relation/use-create-task-relation.test.tsx
@@ -0,0 +1,45 @@
+import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
+import { act, cleanup, renderHook } from "@testing-library/react";
+import type { ReactNode } from "react";
+import { afterEach, describe, expect, it, vi } from "vite-plus/test";
+import createTaskRelation from "@/fetchers/task-relation/create-task-relation";
+import useCreateTaskRelation from "./use-create-task-relation";
+
+vi.mock("@/fetchers/task-relation/create-task-relation", () => ({
+ default: vi.fn(),
+}));
+
+afterEach(cleanup);
+
+describe("useCreateTaskRelation", () => {
+ it("refreshes the other task's cached relations while it is closed", async () => {
+ const client = new QueryClient({
+ defaultOptions: { queries: { refetchOnMount: false } },
+ });
+ const targetKey = ["task-relations", "task-b"];
+ const reloadTarget = vi.fn().mockResolvedValue([{ id: "rel-1" }]);
+ client.setQueryDefaults(targetKey, { queryFn: reloadTarget });
+ client.setQueryData(targetKey, []);
+ vi.mocked(createTaskRelation).mockResolvedValue(
+ {} as Awaited<ReturnType<typeof createTaskRelation>>,
+ );
+ const { result } = renderHook(useCreateTaskRelation, {
+ wrapper: ({ children }: { children: ReactNode }) => (
+ <QueryClientProvider client={client}>{children}</QueryClientProvider>
+ ),
+ });
+
+ await act(async () => {
+ await result.current.mutateAsync({
+ sourceTaskId: "task-a",
+ targetTaskId: "task-b",
+ relationType: "blocks",
+ });
+ });
+
+ await vi.waitFor(() => {
+ expect(client.getQueryData(targetKey)).toEqual([{ id: "rel-1" }]);
+ });
+ expect(reloadTarget).toHaveBeenCalledTimes(1);
+ });
+});
diff --git a/apps/web/src/hooks/mutations/task-relation/use-create-task-relation.ts b/apps/web/src/hooks/mutations/task-relation/use-create-task-relation.ts
index 7bc7bfe15..fb15826e2 100644
--- a/apps/web/src/hooks/mutations/task-relation/use-create-task-relation.ts
+++ b/apps/web/src/hooks/mutations/task-relation/use-create-task-relation.ts
@@ -10,11 +10,15 @@ function useCreateTaskRelation() {
mutationFn: createTaskRelation,
onSuccess: (_, variables) => {
void invalidateRelationTaskProject(queryClient, variables.sourceTaskId);
+ // Queries default to refetchOnMount: false, so a closed but cached end
+ // must refetch now or reopening it shows the old relations.
queryClient.invalidateQueries({
queryKey: ["task-relations", variables.sourceTaskId],
+ refetchType: "all",
});
queryClient.invalidateQueries({
queryKey: ["task-relations", variables.targetTaskId],
+ refetchType: "all",
});
// The list view reads relations per project rather than per task, and
// the mutation does not carry the project the tasks belong to.
diff --git a/apps/web/src/hooks/mutations/task-relation/use-delete-task-relation.test.tsx b/apps/web/src/hooks/mutations/task-relation/use-delete-task-relation.test.tsx
new file mode 100644
index 000000000..b4e36fbf5
--- /dev/null
+++ b/apps/web/src/hooks/mutations/task-relation/use-delete-task-relation.test.tsx
@@ -0,0 +1,42 @@
+import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
+import { act, cleanup, renderHook } from "@testing-library/react";
+import type { ReactNode } from "react";
+import { afterEach, describe, expect, it, vi } from "vite-plus/test";
+import deleteTaskRelation from "@/fetchers/task-relation/delete-task-relation";
+import useDeleteTaskRelation from "./use-delete-task-relation";
+
+vi.mock("@/fetchers/task-relation/delete-task-relation", () => ({
+ default: vi.fn(),
+}));
+
+afterEach(cleanup);
+
+describe("useDeleteTaskRelation", () => {
+ it("refreshes the other task's cached relations while it is closed", async () => {
+ const client = new QueryClient({
+ defaultOptions: { queries: { refetchOnMount: false } },
+ });
+ const targetKey = ["task-relations", "task-b"];
+ const reloadTarget = vi.fn().mockResolvedValue([]);
+ client.setQueryDefaults(targetKey, { queryFn: reloadTarget });
+ client.setQueryData(targetKey, [{ id: "rel-1" }]);
+ vi.mocked(deleteTaskRelation).mockResolvedValue({
+ sourceTaskId: "task-a",
+ targetTaskId: "task-b",
+ } as Awaited<ReturnType<typeof deleteTaskRelation>>);
+ const { result } = renderHook(() => useDeleteTaskRelation("task-a"), {
+ wrapper: ({ children }: { children: ReactNode }) => (
+ <QueryClientProvider client={client}>{children}</QueryClientProvider>
+ ),
+ });
+
+ await act(async () => {
+ await result.current.mutateAsync("rel-1");
+ });
+
+ await vi.waitFor(() => {
+ expect(client.getQueryData(targetKey)).toEqual([]);
+ });
+ expect(reloadTarget).toHaveBeenCalledTimes(1);
+ });
+});
diff --git a/apps/web/src/hooks/mutations/task-relation/use-delete-task-relation.ts b/apps/web/src/hooks/mutations/task-relation/use-delete-task-relation.ts
index a99b457d8..264e6b60e 100644
--- a/apps/web/src/hooks/mutations/task-relation/use-delete-task-relation.ts
+++ b/apps/web/src/hooks/mutations/task-relation/use-delete-task-relation.ts
@@ -11,8 +11,8 @@ function useDeleteTaskRelation(taskId: string) {
onSuccess: (deleted) => {
void invalidateRelationTaskProject(queryClient, deleted.sourceTaskId);
// Both ends go stale, and only one of them is the task this hook was
- // opened for. Queries default to refetchOnMount: false, so opening the
- // other task would otherwise still list the relation.
+ // opened for. Queries default to refetchOnMount: false, so a closed but
+ // cached end must refetch now or reopening it still lists the relation.
const endpoints = new Set([
taskId,
deleted.sourceTaskId,
@@ -21,6 +21,7 @@ function useDeleteTaskRelation(taskId: string) {
for (const endpoint of endpoints) {
queryClient.invalidateQueries({
queryKey: ["task-relations", endpoint],
+ refetchType: "all",
});
}
// The project-scoped query the list view reads is keyed by neither, and
diff --git a/apps/web/src/hooks/use-project-websocket-cache.test.tsx b/apps/web/src/hooks/use-project-websocket-cache.test.tsx
index 3d09f4423..f782b4842 100644
--- a/apps/web/src/hooks/use-project-websocket-cache.test.tsx
+++ b/apps/web/src/hooks/use-project-websocket-cache.test.tsx
@@ -527,11 +527,14 @@ it("drains queued detail events after retries fall back to polling", async () =>
["labels", "a"],
["task", "a"],
["external-links", "a"],
- ["task-relations", "b"],
])
expect(mocks.client.invalidateQueries).toHaveBeenCalledWith({
queryKey: key,
});
+ expect(mocks.client.invalidateQueries).toHaveBeenCalledWith({
+ queryKey: ["task-relations", "b"],
+ refetchType: "all",
+ });
expect(mocks.getTask).not.toHaveBeenCalled();
} finally {
cleanup();
diff --git a/apps/web/src/hooks/use-project-websocket-relations.test.tsx b/apps/web/src/hooks/use-project-websocket-relations.test.tsx
index b4e313f0d..fc06bfa4b 100644
--- a/apps/web/src/hooks/use-project-websocket-relations.test.tsx
+++ b/apps/web/src/hooks/use-project-websocket-relations.test.tsx
@@ -144,6 +144,24 @@ describe("useProjectWebSocket relation invalidation", () => {
expect(invalidatedKeys()).toContain(projectRelationsKey);
});
+ // A linked task that is closed but cached would otherwise reopen on its old
+ // relations, since queries default to refetchOnMount: false.
+ it("refetches both ends' relations even when they are not open", () => {
+ receive({
+ type: "TASK_RELATION_UPDATED",
+ projectId: "project-1",
+ sourceTaskId: "task-1",
+ targetTaskId: "task-2",
+ });
+
+ for (const taskId of ["task-1", "task-2"]) {
+ expect(invalidate).toHaveBeenCalledWith({
+ queryKey: ["task-relations", taskId],
+ refetchType: "all",
+ });
+ }
+ });
+
it.each(["TASK_DELETED", "TASK_MOVED"])(
"refreshes the project relations on %s",
(type) => {
diff --git a/apps/web/src/hooks/use-project-websocket.ts b/apps/web/src/hooks/use-project-websocket.ts
index f7496c089..256e57cef 100644
--- a/apps/web/src/hooks/use-project-websocket.ts
+++ b/apps/web/src/hooks/use-project-websocket.ts
@@ -104,6 +104,7 @@ export function useProjectWebSocket(projectId: string) {
});
queryClient.invalidateQueries({
queryKey: ["task-relations", message.sourceTaskId],
+ refetchType: "all",
});
}
if (message.targetTaskId) {
@@ -112,6 +113,7 @@ export function useProjectWebSocket(projectId: string) {
});
queryClient.invalidateQueries({
queryKey: ["task-relations", message.targetTaskId],
+ refetchType: "all",
});
}
if (!message.sourceTaskId && !message.targetTaskId) {