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): stop task edits refetching the project relations

Juan23abf8a33cb4
diff --git a/apps/web/src/hooks/mutations/task/use-update-task-assignee.ts b/apps/web/src/hooks/mutations/task/use-update-task-assignee.ts
index baad024f8..42236ff06 100644
--- a/apps/web/src/hooks/mutations/task/use-update-task-assignee.ts
+++ b/apps/web/src/hooks/mutations/task/use-update-task-assignee.ts
@@ -1,5 +1,6 @@
import { useMutation, useQueryClient } from "@tanstack/react-query";
import updateTaskAssignee from "@/fetchers/task/update-task-assignee";
+import { isPerTaskRelationQuery } from "@/lib/relation-query-keys";
import type Task from "@/types/task";
export function useUpdateTaskAssignee() {
@@ -23,9 +24,10 @@ export function useUpdateTaskAssignee() {
queryClient.invalidateQueries({
queryKey: ["activities", variables.id],
});
- queryClient.invalidateQueries({
- queryKey: ["task-relations"],
- });
+ // Per-task relation responses embed the linked tasks' status and
+ // assignee, so they stale here; the project-scoped query returns edges
+ // alone and does not.
+ queryClient.invalidateQueries({ predicate: isPerTaskRelationQuery });
},
});
}
diff --git a/apps/web/src/hooks/mutations/task/use-update-task-status.ts b/apps/web/src/hooks/mutations/task/use-update-task-status.ts
index 53ae96631..b6259ee1d 100644
--- a/apps/web/src/hooks/mutations/task/use-update-task-status.ts
+++ b/apps/web/src/hooks/mutations/task/use-update-task-status.ts
@@ -1,5 +1,6 @@
import { useMutation, useQueryClient } from "@tanstack/react-query";
import updateTaskStatus from "@/fetchers/task/update-task-status";
+import { isPerTaskRelationQuery } from "@/lib/relation-query-keys";
import type Task from "@/types/task";
export function useUpdateTaskStatus() {
@@ -23,9 +24,10 @@ export function useUpdateTaskStatus() {
queryClient.invalidateQueries({
queryKey: ["activities", variables.id],
});
- queryClient.invalidateQueries({
- queryKey: ["task-relations"],
- });
+ // Per-task relation responses embed the linked tasks' status and
+ // assignee, so they stale here; the project-scoped query returns edges
+ // alone and does not.
+ queryClient.invalidateQueries({ predicate: isPerTaskRelationQuery });
},
});
}
diff --git a/apps/web/src/hooks/use-project-websocket.ts b/apps/web/src/hooks/use-project-websocket.ts
index 5760e7119..42ab07c96 100644
--- a/apps/web/src/hooks/use-project-websocket.ts
+++ b/apps/web/src/hooks/use-project-websocket.ts
@@ -3,6 +3,7 @@ import { useQueryClient } from "@tanstack/react-query";
import { useEffect, useRef } from "react";
import { getApiUrl } from "@/fetchers/get-api-url";
import { authClient } from "@/lib/auth-client";
+import { isPerTaskRelationQuery } from "@/lib/relation-query-keys";
export function getWsUrl(projectId: string) {
const base = getApiUrl("ws");
@@ -114,9 +115,7 @@ export function useProjectWebSocket(projectId: string) {
// change stales them, while the project query returns edges
// alone and a prefix match would refetch it needlessly.
queryClient.invalidateQueries({
- predicate: (query) =>
- query.queryKey[0] === "task-relations" &&
- query.queryKey[1] !== "project",
+ predicate: isPerTaskRelationQuery,
});
}
} else {
diff --git a/apps/web/src/lib/relation-query-keys.test.ts b/apps/web/src/lib/relation-query-keys.test.ts
new file mode 100644
index 000000000..3e7c7714f
--- /dev/null
+++ b/apps/web/src/lib/relation-query-keys.test.ts
@@ -0,0 +1,21 @@
+import { describe, expect, it } from "vitest";
+import { isPerTaskRelationQuery } from "./relation-query-keys";
+
+const q = (queryKey: readonly unknown[]) => ({ queryKey });
+
+describe("isPerTaskRelationQuery", () => {
+ it("matches a per-task relation query", () => {
+ expect(isPerTaskRelationQuery(q(["task-relations", "task-1"]))).toBe(true);
+ });
+
+ it("excludes the project-scoped query", () => {
+ expect(
+ isPerTaskRelationQuery(q(["task-relations", "project", "project-1"])),
+ ).toBe(false);
+ });
+
+ it("ignores unrelated queries", () => {
+ expect(isPerTaskRelationQuery(q(["tasks", "project-1"]))).toBe(false);
+ expect(isPerTaskRelationQuery(q([]))).toBe(false);
+ });
+});
diff --git a/apps/web/src/lib/relation-query-keys.ts b/apps/web/src/lib/relation-query-keys.ts
new file mode 100644
index 000000000..b0d508116
--- /dev/null
+++ b/apps/web/src/lib/relation-query-keys.ts
@@ -0,0 +1,18 @@
+/**
+ * Matches every per-task relation query while excluding the project-scoped one.
+ *
+ * The two live under the same `task-relations` prefix but answer different
+ * questions. A per-task response embeds each linked task's title, status and
+ * assignee, so an ordinary task edit stales it. The project response is edges
+ * alone — ids and relation types — and changes only when a relation is created
+ * or removed, or a task joins or leaves the project. A plain prefix
+ * invalidation cannot tell them apart and refetches the project query on every
+ * status or assignee change.
+ */
+export function isPerTaskRelationQuery(query: {
+ queryKey: readonly unknown[];
+}) {
+ return (
+ query.queryKey[0] === "task-relations" && query.queryKey[1] !== "project"
+ );
+}