import { useEffect, useRef, useState } from "react"; import { Link } from "react-router-dom"; import { api, fetchFile, fetchPaperless } from "../api.js"; import { Modal } from "./Modal.jsx"; import { ErrorNote } from "./widgets.jsx"; export function formatDate(value) { if (!value) return "Ohne Datum"; const [year, month, day] = String(value).slice(0, 10).split("-"); if (!year || !month || !day) return String(value); return `${day}.${month}.${year}`; } export function formatStamp(value) { if (!value) return ""; const [day, time = ""] = String(value).split("T"); return `${formatDate(day)} ${time.replace("Z", " UTC").slice(0, 8)}`.trim(); } export function selectionOf(params) { return { scope: params.get("bereich") || "all", chapterId: params.get("kapitel") || "", }; } export function writeSelection(params, setParams, scope, chapterId) { const next = new URLSearchParams(params); if (!scope || scope === "all") next.delete("bereich"); else next.set("bereich", scope); if (chapterId) next.set("kapitel", chapterId); else next.delete("kapitel"); setParams(next, { replace: true }); } export function writeChapter(params, setParams, chapterId) { const next = new URLSearchParams(params); if (chapterId) next.set("kapitel", chapterId); else next.delete("kapitel"); setParams(next, { replace: true }); } export function withSelection(href, params) { const url = new URL(href, "http://tsumugu.local"); for (const key of ["bereich", "kapitel"]) { const value = params.get(key); if (value && !url.searchParams.has(key)) url.searchParams.set(key, value); } const search = url.searchParams.toString(); return `${url.pathname}${search ? `?${search}` : ""}`; } export function TargetLine({ label, chapters = [], chapterId = "", chapterTitle = "" }) { if (!label) return null; const chapter = chapters.find((item) => item.id === chapterId); const namedChapter = chapter?.title || chapterTitle; return (

Ziel: {label} {namedChapter ? <> · Kapitel {namedChapter} : null}

); } export function kindLabel(kind) { if (kind === "note") return "Notiz"; if (kind === "file") return "Datei"; if (kind === "external_ref") return "Paperless"; return "Unterlage"; } export function fachDate(element) { return element?.dates?.document_on || null; } export function Trail({ items }) { if (!items?.length) return null; return ( ); } export function nestRecords(items, parentKey) { const ids = new Set((items || []).map((item) => item.id)); const children = new Map(); const roots = []; for (const item of items || []) { const parentId = item[parentKey]; if (parentId && ids.has(parentId)) { const list = children.get(parentId) || []; list.push(item); children.set(parentId, list); } else { roots.push(item); } } return { roots, children }; } function TreeBranch({ item, children, hrefFor, nested }) { const kids = children.get(item.id) || []; return (
  • {item.kind_label || "Eintrag"} {item.title} {!nested && item.parent_title ? in {item.parent_title} : null} {item.status === "open" ? "Offen" : item.status === "closed" ? "Geschlossen" : ""} {kids.length ? ( ) : null}
  • ); } export function RecordTree({ items, parentKey, hrefFor }) { const { roots, children } = nestRecords(items, parentKey); if (!roots.length) return null; return ( ); } function sectionsOf(chapters, bindings) { const buckets = new Map(); const loose = []; for (const binding of bindings || []) { if (binding.chapter_id && chapters.some((chapter) => chapter.id === binding.chapter_id)) { const list = buckets.get(binding.chapter_id) || []; list.push(binding); buckets.set(binding.chapter_id, list); } else { loose.push(binding); } } const sections = (chapters || []).map((chapter) => ({ id: chapter.id, title: chapter.title, documentOn: chapter.document_on || "", items: buckets.get(chapter.id) || [], })); if (sections.length && loose.length) { sections.push({ id: "loose", title: "Ohne Kapitel", documentOn: "", items: loose }); } else if (!sections.length) { sections.push({ id: "loose", title: "", documentOn: "", items: loose }); } return sections; } export function matterOpenHref(item) { const contexts = item?.contexts || []; if (contexts.some((entry) => entry.kind === "direct")) return `/matters/${item.id}`; const context = contexts.find((entry) => entry.kind === "case"); if (!context) return `/matters/${item.id}`; return `/matters/${item.id}?context_kind=case&context_id=${context.id}`; } export function Placements({ items }) { if (!items?.length) return null; const current = items.find((item) => item.current); return (

    In Akte: {items.map((item, index) => ( {index ? : null} {item.label.replaceAll("Unterakte ", "").replaceAll("Akte ", "")} ))} {current ? · aktueller Handlungskontext : · Handlungskontext direkt, solange keine Akte gewählt ist}

    ); } export function ObjectMenu({ view, onView, children }) { const [open, setOpen] = useState(false); function choose(next) { setOpen(false); onView(next); } return ( <> setOpen(false)}>
    setOpen(false)}> {children}
    ); } export function WorkSwitch({ view, onView }) { return (
    {view !== "contents" && view !== "work" ? ( ) : null}
    ); } function sourceLabel(element) { if (element.kind === "note") return "Notiz"; if (element.kind === "external_ref") return element.external?.source === "paperless" ? "Paperless" : "Referenz"; if (element.file?.content_type === "application/pdf") return "PDF"; if (element.file?.content_type?.startsWith("image/")) return "Bild"; if (element.kind === "file") return "Datei"; return kindLabel(element.kind); } function blurb(element) { return element.summary || element.excerpt || ""; } function Thumb({ element, context }) { const [url, setUrl] = useState(""); const [media, setMedia] = useState(""); const [failed, setFailed] = useState(false); useEffect(() => { if (!element.preview?.available) return undefined; let alive = true; let current = ""; const load = element.preview.source === "paperless" ? fetchPaperless(element.id, context) : fetchFile(element.id, context); load.then((blob) => { if (!alive) return; current = URL.createObjectURL(blob); setUrl(current); if (blob.type.startsWith("image/")) setMedia("image"); else if (blob.type.includes("pdf")) setMedia("pdf"); else setMedia("other"); }).catch(() => { if (alive) setFailed(true); }); return () => { alive = false; if (current) URL.revokeObjectURL(current); }; }, [element.id, element.preview?.available, element.preview?.source, context?.kind, context?.id]); if (element.kind === "note") return Notiz; if (url && media === "image") return ; if (url && media === "pdf") return