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): address review of the subtask list view

Juancab7aa4c593e
diff --git a/apps/web/src/components/list-view/index.tsx b/apps/web/src/components/list-view/index.tsx
index e78825f48..d20a93c66 100644
--- a/apps/web/src/components/list-view/index.tsx
+++ b/apps/web/src/components/list-view/index.tsx
@@ -40,6 +40,19 @@ 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;
@@ -99,30 +112,36 @@ function ListView({ project, disableDragDrop = false }: ListViewProps) {
// Per viewer and per project, and only a convenience: a row that cannot be
// restored simply starts collapsed.
- const expandedStorageKey = `kaneo:list-view:expanded-subtasks:${project?.id ?? ""}`;
- const [expandedTasks, setExpandedTasks] = useState<Record<string, boolean>>(
- () => {
- try {
- const stored = localStorage.getItem(expandedStorageKey);
- return stored ? (JSON.parse(stored) as Record<string, boolean>) : {};
- } catch {
- return {};
- }
- },
- );
+ const projectId = project?.id ?? "";
+ const [expanded, setExpanded] = useState(() => ({
+ projectId,
+ rows: readExpandedRows(projectId),
+ }));
+
+ // The board route swaps this component's project rather than remounting it,
+ // so a lazy initializer would keep the previous project's map and then save
+ // it under the new project's key.
+ if (expanded.projectId !== projectId) {
+ setExpanded({ projectId, rows: readExpandedRows(projectId) });
+ }
+
+ const expandedTasks = expanded.rows;
useEffect(() => {
try {
- localStorage.setItem(expandedStorageKey, JSON.stringify(expandedTasks));
+ localStorage.setItem(
+ expandedRowsStorageKey(projectId),
+ JSON.stringify(expandedTasks),
+ );
} catch {
// A private window or blocked site data only costs the restore.
}
- }, [expandedStorageKey, expandedTasks]);
+ }, [projectId, expandedTasks]);
const toggleTaskExpanded = useCallback((rowId: string) => {
- setExpandedTasks((previous) => ({
+ setExpanded((previous) => ({
...previous,
- [rowId]: !previous[rowId],
+ rows: { ...previous.rows, [rowId]: !previous.rows[rowId] },
}));
}, []);
@@ -352,8 +371,14 @@ function ListView({ project, disableDragDrop = false }: ListViewProps) {
tasks: column.tasks,
children: subtaskChildren,
tasksById,
- isExpanded: (rowId) =>
- Boolean(expandedTasks[rowId]) && activeId !== rowId,
+ isExpanded: (rowId) => {
+ if (!expandedTasks[rowId]) return false;
+ // activeId is the dragged row's id, which is a bare task id because
+ // only top-level rows drag. The same task can also be rendered at
+ // "parent/child", and those occurrences have to collapse too.
+ const taskId = rowId.slice(rowId.lastIndexOf("/") + 1);
+ return activeId !== taskId;
+ },
});
return (
diff --git a/apps/web/src/components/list-view/task-row.tsx b/apps/web/src/components/list-view/task-row.tsx
index 37cb80069..f338a0b80 100644
--- a/apps/web/src/components/list-view/task-row.tsx
+++ b/apps/web/src/components/list-view/task-row.tsx
@@ -238,6 +238,9 @@ function TaskRow({
e.stopPropagation();
onToggleExpanded?.();
}}
+ // The row handles Enter by opening the task, and a button's
+ // Enter keydown bubbles there even though its click does not.
+ onKeyDown={(e) => e.stopPropagation()}
aria-expanded={isExpanded}
aria-label={
isExpanded
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 444ae0358..609126890 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
@@ -10,8 +10,9 @@ function useDeleteTaskRelation(taskId: string) {
queryClient.invalidateQueries({
queryKey: ["task-relations", taskId],
});
- // The other end of the relation and the project-scoped list the list
- // view reads are both stale now, and neither is keyed by this task.
+ // Both the other end of the relation and the project-scoped query the
+ // list view reads are stale now, and neither is keyed by this task.
+ // Invalidating the prefix covers every project a mounted view holds.
queryClient.invalidateQueries({
queryKey: ["task-relations", "project"],
});
diff --git a/apps/web/src/hooks/use-project-websocket.ts b/apps/web/src/hooks/use-project-websocket.ts
index 8b41317ac..c47adbee5 100644
--- a/apps/web/src/hooks/use-project-websocket.ts
+++ b/apps/web/src/hooks/use-project-websocket.ts
@@ -69,6 +69,14 @@ 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],
+ });
+
if (message.type === "TASK_RELATION_UPDATED") {
if (message.sourceTaskId) {
queryClient.invalidateQueries({
diff --git a/apps/web/src/lib/subtask-tree.test.ts b/apps/web/src/lib/subtask-tree.test.ts
index a98ac057e..7bb649e3f 100644
--- a/apps/web/src/lib/subtask-tree.test.ts
+++ b/apps/web/src/lib/subtask-tree.test.ts
@@ -147,6 +147,59 @@ describe("flattenSubtaskRows", () => {
expect(new Set(ids).size).toBe(ids.length);
});
+ it("caps nesting on a layered graph with shared descendants", () => {
+ // Every task on a level parents every task on the next. Expanding each
+ // occurrence would enumerate simple paths: this shape produced 16,356 rows
+ // before expansion was bounded, with no cycle anywhere in it.
+ const LEVELS = 12;
+ const WIDTH = 2;
+ const tasks: T[] = [];
+ const relations: TaskRelationEdge[] = [];
+
+ for (let level = 0; level < LEVELS; level += 1) {
+ for (let index = 0; index < WIDTH; index += 1) {
+ tasks.push(task(`l${level}n${index}`));
+ }
+ }
+ for (let level = 0; level < LEVELS - 1; level += 1) {
+ for (let from = 0; from < WIDTH; from += 1) {
+ for (let to = 0; to < WIDTH; to += 1) {
+ relations.push(subtask(`l${level}n${from}`, `l${level + 1}n${to}`));
+ }
+ }
+ }
+
+ const rows = flattenSubtaskRows({
+ tasks,
+ children: buildSubtaskChildren(relations),
+ tasksById: new Map(tasks.map((entry) => [entry.id, entry])),
+ isExpanded: () => true,
+ maxNestedRows: 200,
+ });
+
+ // Unbounded, this shape reaches 16,356 rows with no cycle in it. Reaching
+ // it in the app would need a viewer to expand that many rows by hand, but
+ // the walk itself has to terminate regardless of who calls it.
+ const nested = rows.filter((row) => row.depth > 0);
+ expect(nested.length).toBeLessThan(400);
+ expect(rows.filter((row) => row.depth === 0)).toHaveLength(tasks.length);
+ });
+
+ it("leaves every task a top-level row even when nesting is capped", () => {
+ const tasks = [task("a"), task("b"), task("c")];
+ const rows = flattenSubtaskRows({
+ tasks,
+ children: buildSubtaskChildren([subtask("a", "b"), subtask("b", "c")]),
+ tasksById: new Map(tasks.map((entry) => [entry.id, entry])),
+ isExpanded: () => true,
+ maxNestedRows: 0,
+ });
+
+ // Status grouping and the per-column counts depend on these rows, so the
+ // cap drops nesting and never a task's own row.
+ expect(rows.map((row) => row.rowId)).toEqual(["a", "b", "c"]);
+ });
+
it("ignores a child the view is not rendering", () => {
// A subtask in another project is not returned by the endpoint, so a
// chevron would open onto nothing.
diff --git a/apps/web/src/lib/subtask-tree.ts b/apps/web/src/lib/subtask-tree.ts
index 21a67b6de..62e71df50 100644
--- a/apps/web/src/lib/subtask-tree.ts
+++ b/apps/web/src/lib/subtask-tree.ts
@@ -46,6 +46,18 @@ export function buildSubtaskChildren(
return children;
}
+/**
+ * A task may have several parents, so expanding every occurrence enumerates
+ * simple paths rather than tasks and can grow exponentially: twelve layers of
+ * two tasks each, 24 tasks and 44 edges, reach 16,356 rows. Reaching that
+ * needs a viewer to expand exponentially many rows by hand, since every row
+ * starts collapsed and nested rows carry their own path ids rather than
+ * inheriting their task's state. The cap is a backstop for the shapes that
+ * would otherwise freeze the tab, not the mechanism that keeps the common
+ * case small.
+ */
+const MAX_NESTED_ROWS = 1000;
+
/**
* Expands a column's tasks into rows, repeating each subtask beneath its
* parent when that parent is expanded.
@@ -59,13 +71,16 @@ export function flattenSubtaskRows<T extends { id: string }>({
children,
tasksById,
isExpanded,
+ maxNestedRows = MAX_NESTED_ROWS,
}: {
tasks: readonly T[];
children: Map<string, string[]>;
tasksById: Map<string, T>;
isExpanded: (rowId: string) => boolean;
+ maxNestedRows?: number;
}): SubtaskRow<T>[] {
const rows: SubtaskRow<T>[] = [];
+ let nested = 0;
const walk = (
task: T,
@@ -87,7 +102,9 @@ export function flattenSubtaskRows<T extends { id: string }>({
rows.push({ task, depth, rowId, childCount: childTasks.length });
+ if (depth > 0) nested += 1;
if (childTasks.length === 0 || !isExpanded(rowId)) return;
+ if (nested >= maxNestedRows) return;
const nextAncestors = new Set(ancestors);
nextAncestors.add(task.id);
@@ -96,6 +113,9 @@ export function flattenSubtaskRows<T extends { id: string }>({
}
};
+ // Top-level rows are always emitted; the cap governs nesting only, because
+ // the status grouping and the per-column counts depend on every task in the
+ // column having a row.
for (const task of tasks) {
walk(task, 0, new Set(), "");
}