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): validate persisted expansion and narrow relation refetches

Juan65efaacb6d00
diff --git a/apps/web/src/components/list-view/index.tsx b/apps/web/src/components/list-view/index.tsx
index d20a93c66..90f7bec3f 100644
--- a/apps/web/src/components/list-view/index.tsx
+++ b/apps/web/src/components/list-view/index.tsx
@@ -30,6 +30,10 @@ import useGetProjectTaskRelations from "@/hooks/queries/task-relation/use-get-pr
import { useRegisterShortcuts } from "@/hooks/use-keyboard-shortcuts";
import { cn } from "@/lib/cn";
import { getColumnIcon } from "@/lib/column";
+import {
+ readExpandedRows,
+ writeExpandedRows,
+} from "@/lib/expanded-rows-storage";
import { buildSubtaskChildren, flattenSubtaskRows } from "@/lib/subtask-tree";
import { toast } from "@/lib/toast";
import useBulkSelectionStore from "@/store/bulk-selection";
@@ -40,19 +44,6 @@ import { ArchiveTasksModal } from "../shared/modals/archive-tasks-modal";
import CreateTaskModal from "../shared/modals/create-task-modal";
import TaskRow from "./task-row";
-function expandedRowsStorageKey(projectId: string) {
- return `kaneo:list-view:expanded-subtasks:${projectId}`;
-}
-
-function readExpandedRows(projectId: string): Record<string, boolean> {
- try {
- const stored = localStorage.getItem(expandedRowsStorageKey(projectId));
- return stored ? (JSON.parse(stored) as Record<string, boolean>) : {};
- } catch {
- return {};
- }
-}
-
type ListViewProps = {
project: ProjectWithTasks;
disableDragDrop?: boolean;
@@ -128,14 +119,7 @@ function ListView({ project, disableDragDrop = false }: ListViewProps) {
const expandedTasks = expanded.rows;
useEffect(() => {
- try {
- localStorage.setItem(
- expandedRowsStorageKey(projectId),
- JSON.stringify(expandedTasks),
- );
- } catch {
- // A private window or blocked site data only costs the restore.
- }
+ writeExpandedRows(projectId, expandedTasks);
}, [projectId, expandedTasks]);
const toggleTaskExpanded = useCallback((rowId: string) => {
diff --git a/apps/web/src/hooks/use-project-websocket.ts b/apps/web/src/hooks/use-project-websocket.ts
index c47adbee5..2b0eabe73 100644
--- a/apps/web/src/hooks/use-project-websocket.ts
+++ b/apps/web/src/hooks/use-project-websocket.ts
@@ -69,13 +69,21 @@ export function useProjectWebSocket(projectId: string) {
queryKey: ["tasks", message.projectId],
});
- // The list view reads relations per project. A relation changed by
- // another client, or a task moving in or out of the project,
- // changes which edges belong to it, and neither is covered by the
- // per-task keys below.
- queryClient.invalidateQueries({
- queryKey: ["task-relations", "project", message.projectId],
- });
+ // The list view reads relations per project, and the per-task keys
+ // below do not reach that query. Only the events that can change
+ // which edges belong to the project qualify: an edit, a label or a
+ // comment leaves the edge set alone and would cost every mounted
+ // list a refetch.
+ if (
+ message.type === "TASK_RELATION_UPDATED" ||
+ message.type === "TASK_CREATED" ||
+ message.type === "TASK_DELETED" ||
+ message.type === "TASK_MOVED"
+ ) {
+ queryClient.invalidateQueries({
+ queryKey: ["task-relations", "project", message.projectId],
+ });
+ }
if (message.type === "TASK_RELATION_UPDATED") {
if (message.sourceTaskId) {
diff --git a/apps/web/src/lib/expanded-rows-storage.test.ts b/apps/web/src/lib/expanded-rows-storage.test.ts
new file mode 100644
index 000000000..2769ad017
--- /dev/null
+++ b/apps/web/src/lib/expanded-rows-storage.test.ts
@@ -0,0 +1,70 @@
+import { afterEach, describe, expect, it, vi } from "vitest";
+import {
+ expandedRowsStorageKey,
+ readExpandedRows,
+ writeExpandedRows,
+} from "./expanded-rows-storage";
+
+afterEach(() => {
+ localStorage.clear();
+ vi.restoreAllMocks();
+});
+
+describe("readExpandedRows", () => {
+ it("returns an empty map when nothing is stored", () => {
+ expect(readExpandedRows("p1")).toEqual({});
+ });
+
+ it("round-trips what was written", () => {
+ writeExpandedRows("p1", { a: true, "a/b": true });
+
+ expect(readExpandedRows("p1")).toEqual({ a: true, "a/b": true });
+ });
+
+ it("keeps projects apart", () => {
+ writeExpandedRows("p1", { a: true });
+
+ expect(readExpandedRows("p2")).toEqual({});
+ });
+
+ // Each of these would previously have been cast straight to the map, and the
+ // first property read during render would throw and blank the list.
+ it.each([
+ ["null", "null"],
+ ["an array", "[1,2,3]"],
+ ["a string", '"hello"'],
+ ["a number", "42"],
+ ["malformed JSON", "{not json"],
+ ])("ignores %s", (_label, stored) => {
+ localStorage.setItem(expandedRowsStorageKey("p1"), stored);
+
+ expect(readExpandedRows("p1")).toEqual({});
+ });
+
+ it("drops non-true values rather than trusting the shape", () => {
+ localStorage.setItem(
+ expandedRowsStorageKey("p1"),
+ JSON.stringify({ a: true, b: false, c: "yes", d: null }),
+ );
+
+ expect(readExpandedRows("p1")).toEqual({ a: true });
+ });
+
+ it("survives storage that throws", () => {
+ vi.spyOn(Storage.prototype, "getItem").mockImplementation(() => {
+ throw new Error("blocked");
+ });
+
+ expect(readExpandedRows("p1")).toEqual({});
+ });
+});
+
+describe("writeExpandedRows", () => {
+ it("does not throw when storage refuses", () => {
+ vi.spyOn(Storage.prototype, "setItem").mockImplementation(() => {
+ throw new Error("quota");
+ });
+
+ expect(() => writeExpandedRows("p1", { a: true })).not.toThrow();
+ });
+});
diff --git a/apps/web/src/lib/expanded-rows-storage.ts b/apps/web/src/lib/expanded-rows-storage.ts
new file mode 100644
index 000000000..a251db25d
--- /dev/null
+++ b/apps/web/src/lib/expanded-rows-storage.ts
@@ -0,0 +1,49 @@
+/**
+ * Per-viewer, per-project record of which list-view rows are expanded.
+ *
+ * This is a convenience rather than state the app depends on: a value that
+ * cannot be read or written simply leaves every row collapsed.
+ */
+export function expandedRowsStorageKey(projectId: string) {
+ return `kaneo:list-view:expanded-subtasks:${projectId}`;
+}
+
+export function readExpandedRows(projectId: string): Record<string, boolean> {
+ try {
+ const stored = localStorage.getItem(expandedRowsStorageKey(projectId));
+ if (!stored) return {};
+
+ // A cast would let a stale or hand-edited value through: "null" parses
+ // fine and then throws on the first property read, blanking the list.
+ const parsed: unknown = JSON.parse(stored);
+ if (
+ typeof parsed !== "object" ||
+ parsed === null ||
+ Array.isArray(parsed)
+ ) {
+ return {};
+ }
+
+ const rows: Record<string, boolean> = {};
+ for (const [rowId, value] of Object.entries(parsed)) {
+ if (value === true) rows[rowId] = true;
+ }
+ return rows;
+ } catch {
+ return {};
+ }
+}
+
+export function writeExpandedRows(
+ projectId: string,
+ rows: Record<string, boolean>,
+) {
+ try {
+ localStorage.setItem(
+ expandedRowsStorageKey(projectId),
+ JSON.stringify(rows),
+ );
+ } catch {
+ // A private window or blocked site data only costs the restore.
+ }
+}