Files
paperclip/ui/src/lib/issue-tree.ts
T
Darren Davison 9be1b3f8a9 test: extract buildIssueTree utility and add tests for hierarchy logic
Extract the inline tree-building logic from IssuesList into a pure
`buildIssueTree` function in lib/issue-tree.ts so it can be unit tested.
Add six tests covering: flat lists, parent-child grouping, multi-level
nesting, orphaned sub-tasks promoted to root, empty input, and list
order preservation.

Add two tests to IssueRow.test.tsx covering the new titleSuffix prop:
renders inline after the title when provided, and renders cleanly when
omitted.

Co-Authored-By: Paperclip <noreply@paperclip.ing>
2026-04-05 12:02:12 +01:00

28 lines
916 B
TypeScript

import type { Issue } from "@paperclipai/shared";
export interface IssueTree {
roots: Issue[];
childMap: Map<string, Issue[]>;
}
/**
* Builds a parent→children tree from a flat list of issues.
*
* - `roots` contains issues whose parent is absent from the list (or have no
* parent at all), so orphaned sub-tasks are always visible at root level.
* - `childMap` maps each parent id to its direct children in list order.
*/
export function buildIssueTree(items: Issue[]): IssueTree {
const itemIds = new Set(items.map((i) => i.id));
const roots = items.filter((i) => !i.parentId || !itemIds.has(i.parentId));
const childMap = new Map<string, Issue[]>();
for (const item of items) {
if (item.parentId && itemIds.has(item.parentId)) {
const arr = childMap.get(item.parentId) ?? [];
arr.push(item);
childMap.set(item.parentId, arr);
}
}
return { roots, childMap };
}