Files
tsumugu/frontend/src/pages/chapters.js
T
LarsandCursor 3c190c87b4
Deploy Development / deploy (push) Successful in 28s
Test Suite / pytest-backend (push) Successful in 1m37s
Test Suite / smoke-dev (push) Successful in 0s
Test Suite / frontend-build (push) Successful in 9s
Halte Anlagen und Versand als eigene Verwendung fest, damit die uebermittelte Fassung und ihre Nachweise spaeter nicht still mit der Arbeitsfassung verschmelzen.
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-18 09:57:59 +02:00

100 lines
3.3 KiB
JavaScript

export function chapterPath(chapters, chapterId) {
const byId = new Map((chapters || []).map((chapter) => [chapter.id, chapter]));
const names = [];
const seen = new Set();
let current = byId.get(chapterId);
while (current && !seen.has(current.id)) {
seen.add(current.id);
names.unshift(current.title);
current = current.parent_chapter_id ? byId.get(current.parent_chapter_id) : null;
}
return names;
}
export function chapterPathLabel(chapters, chapterId) {
return chapterPath(chapters, chapterId).join(" / ");
}
export function siblingChapterIds(chapters, chapterId) {
const current = (chapters || []).find((chapter) => chapter.id === chapterId);
const parent = current?.parent_chapter_id || null;
return (chapters || [])
.filter((chapter) => (chapter.parent_chapter_id || null) === parent)
.map((chapter) => chapter.id);
}
export function descendantChapterIds(chapters, chapterId) {
const children = (chapters || []).filter((chapter) => chapter.parent_chapter_id === chapterId);
return children.flatMap((chapter) => [chapter.id, ...descendantChapterIds(chapters, chapter.id)]);
}
export function buildChapterSections(chapters, bindings) {
const buckets = new Map();
const loose = [];
const known = new Set((chapters || []).map((chapter) => chapter.id));
for (const binding of bindings || []) {
if (binding.chapter_id && known.has(binding.chapter_id)) {
const list = buckets.get(binding.chapter_id) || [];
list.push(binding);
buckets.set(binding.chapter_id, list);
} else {
loose.push(binding);
}
}
const nodes = new Map();
for (const chapter of chapters || []) {
nodes.set(chapter.id, {
id: chapter.id,
title: chapter.title,
documentOn: chapter.document_on || "",
parentId: chapter.parent_chapter_id || "",
items: buckets.get(chapter.id) || [],
children: [],
});
}
const roots = [];
for (const chapter of chapters || []) {
const node = nodes.get(chapter.id);
const parent = chapter.parent_chapter_id ? nodes.get(chapter.parent_chapter_id) : null;
if (parent) parent.children.push(node);
else roots.push(node);
}
return { roots, loose };
}
export function trailWithChapter(items, chapters, chapterId, backHref) {
if (!chapterId) return items;
const path = chapterPath(chapters, chapterId);
if (!path.length) return items || [];
const base = (items || []).map((item, index, all) => (
index === all.length - 1 && !item.href ? { ...item, href: backHref } : item
));
return [...base, { label: path.join(" / "), href: null }];
}
export function countLabel(direct, nested) {
const here = Number(direct) || 0;
const below = Number(nested) || 0;
if (here && below) return `${here} direkt · ${below} in Unterkapiteln`;
if (!here && below) return `${below} in Unterkapiteln`;
return String(here);
}
export function sectionCounts(section) {
const direct = (section?.items || []).length;
const nested = (section?.children || []).reduce((sum, child) => {
const inner = sectionCounts(child);
return sum + inner.direct + inner.nested;
}, 0);
return { direct, nested };
}
export function findSection(nodes, id) {
for (const node of nodes || []) {
if (node.id === id) return node;
const found = findSection(node.children, id);
if (found) return found;
}
return null;
}