import { useContext, useEffect, useRef, useState } from "react"; import { Link, useNavigate, useParams, useSearchParams } from "react-router-dom"; import { api, markDirty, textToDocument } from "../api.js"; import { SessionContext } from "../session.js"; import { Modal } from "./Modal.jsx"; import { MailPicker, PaperlessPicker } from "./Sources.jsx"; import { ErrorNote, ShareBox } from "./widgets.jsx"; import { ContentBoard, ObjectMenu, OrderChoice, RecordTree, SurfaceView, TargetLine, Trail, WorkLists, WorkSwitch, saveChapterOrder, writeSelection } from "./content.jsx"; import { ReadingPane } from "./ReadingPane.jsx"; import { StructureNavigator, Workspace, rememberScroll, restoreScroll, useWorkspaceLayout } from "./workspace.jsx"; import { NavIcon } from "./icons.jsx"; export function CaseList() { const { session } = useContext(SessionContext); const [items, setItems] = useState([]); const [title, setTitle] = useState(""); const [roomId, setRoomId] = useState(session.rooms[0]?.id || ""); const [roomFilter, setRoomFilter] = useState(""); const [error, setError] = useState(""); const [creating, setCreating] = useState(false); async function load() { const data = await api("/api/v1/cases"); setItems(data.items); } useEffect(() => { load().catch((err) => setError(err.message)); }, [session.scope.id]); async function create(event) { event.preventDefault(); setError(""); try { await api("/api/v1/cases", { method: "POST", body: { title, home_scope_id: roomId } }); setTitle(""); markDirty(false); setCreating(false); await load(); } catch (err) { setError(err.message); } } return (

Akten

Eine Akte sammelt die Vorgänge. Der Datenraum an der Akte bestimmt, wer sie sieht.

{session.rooms.map((room) => ( ))}
{items.filter((item) => !roomFilter || item.home_scope_id === roomFilter).length ? ( !roomFilter || item.home_scope_id === roomFilter)} parentKey="parent_case_id" hrefFor={(item) => `/cases/${item.id}`} /> ) : (

Noch keine Akte. Lege die erste an.

)} setCreating(false)}>
); } function OutlineLink({ kind, title, to }) { return ( {kind} {title} ); } function matterChapters(record, matterId) { return (record.contained || []).find((item) => item.matter_id === matterId)?.chapters || []; } function ChapterLinks({ chapters, scopeId, scope, chapterId, onChapter }) { if (!chapters.length) return null; return ( ); } function MatterBranch({ matter, caseId, scope, chapterId, record, onScope, onChapter }) { const selected = scope === matter.id && !chapterId; return (
  • Öffnen
    {matter.children?.length ? ( ) : null}
  • ); } function CaseOutline({ record, caseId, scope, chapterId, onScope, onChapter }) { const hasParent = Boolean(record.parent_case_id && record.parent_title); return ( ); } function matterOrigin(group) { const kind = group.kind_label === "Teilvorgang" ? "Teilvorgang" : "Vorgang"; const parent = group.parent_title ? ` · Vorgang ${group.parent_title}` : ""; return `Aus ${kind} ${group.title}${parent}`; } function chapterOrigin(base, chapters, chapterId) { const title = (chapters || []).find((chapter) => chapter.id === chapterId)?.title; return title ? `${base} · Kapitel ${title}` : base; } function mergedCaseItems(record, direction, caseId) { const rows = [ ...(record.bindings || []).map((binding) => ({ ...binding, origin: chapterOrigin("Direkt in dieser Akte", record.chapters, binding.chapter_id), readerHref: `/cases/${caseId}?eintrag=${binding.id}${binding.chapter_id ? `&kapitel=${binding.chapter_id}` : ""}`, })), ...(record.contained || []).flatMap((group) => (group.bindings || []).map((binding) => ({ ...binding, origin: chapterOrigin(matterOrigin(group), group.chapters, binding.chapter_id), readerHref: `/matters/${group.matter_id}?context_kind=case&context_id=${caseId}&eintrag=${binding.id}${binding.chapter_id ? `&kapitel=${binding.chapter_id}` : ""}`, }))), ]; const counts = {}; for (const row of rows) counts[row.element.id] = (counts[row.element.id] || 0) + 1; const marked = rows.map((row) => ({ ...row, shared: counts[row.element.id] > 1 })); const dated = marked.filter((row) => row.element.dates?.document_on); const undated = marked.filter((row) => !row.element.dates?.document_on); dated.sort((a, b) => a.element.dates.document_on.localeCompare(b.element.dates.document_on) || a.id.localeCompare(b.id)); if (direction === "desc") dated.reverse(); undated.sort((a, b) => a.id.localeCompare(b.id)); return [...dated, ...undated]; } function matterNode(matter, caseId) { const query = `?context_kind=case&context_id=${caseId}`; return { id: matter.id, title: matter.title, kind: matter.kind_label || "Vorgang", href: `/matters/${matter.id}${query}`, current: false, children: (matter.children || []).map((child) => matterNode(child, caseId)), }; } function caseNodes(record, parentRecord) { const self = { id: record.id, title: record.title, kind: record.kind_label || "Akte", href: null, current: true, children: [ ...(record.subcases || []).map((item) => ({ id: item.id, title: item.title, kind: "Unterakte", href: `/cases/${item.id}`, current: false, children: [], })), ...(record.matters || []).map((matter) => matterNode(matter, record.id)), ], }; if (!parentRecord) return [self]; const siblings = (parentRecord.subcases || []).map((item) => (item.id === record.id ? self : { id: item.id, title: item.title, kind: "Unterakte", href: `/cases/${item.id}`, current: false, children: [], })); return [{ id: parentRecord.id, title: parentRecord.title, kind: "Akte", href: `/cases/${parentRecord.id}`, current: false, children: siblings.length ? siblings : [self], }]; } export function CaseDetail() { const { id } = useParams(); const navigate = useNavigate(); const [params, setParams] = useSearchParams(); const rawScope = params.get("bereich") || ""; const overview = rawScope === "all" || rawScope === "ueberblick"; const chapterId = overview ? "" : (params.get("kapitel") || ""); const entryId = params.get("eintrag") || ""; const [record, setRecord] = useState(null); const [parentTree, setParentTree] = useState(null); const [error, setError] = useState(""); const [modal, setModal] = useState(""); const [view, setView] = useState("contents"); const [dateDir, setDateDir] = useState("asc"); const { width, structureOpen, setStructureOpen } = useWorkspaceLayout(); const listRef = useRef(null); const pendingChapter = useRef(""); const context = { kind: "case", id }; async function load() { const detail = await api(`/api/v1/cases/${id}`); pendingChapter.current = ""; setRecord(detail); return detail; } useEffect(() => { let alive = true; setRecord(null); setError(""); setView("contents"); setModal(""); setDateDir("asc"); setStructureOpen(false); setParentTree(null); api(`/api/v1/cases/${id}`).then((detail) => { if (!alive) return; pendingChapter.current = ""; setRecord(detail); }).catch((err) => { if (alive) setError(err.message); }); return () => { alive = false; }; }, [id]); useEffect(() => { if (!record || record.id !== id || !record.parent_case_id) return undefined; let alive = true; api(`/api/v1/cases/${record.parent_case_id}`).then((parent) => { if (alive) setParentTree(parent); }).catch(() => { if (alive) setParentTree(null); }); return () => { alive = false; }; }, [record, id]); useEffect(() => { if (!record || record.id !== id) return; const matterId = rawScope && !["all", "ueberblick", "direkt", "direct"].includes(rawScope) ? rawScope : ""; if (matterId && (record.contained || []).some((item) => item.matter_id === matterId)) { const next = new URLSearchParams(); next.set("context_kind", "case"); next.set("context_id", id); const chapter = params.get("kapitel"); if (chapter) next.set("kapitel", chapter); navigate(`/matters/${matterId}?${next.toString()}`, { replace: true }); return; } if (!overview && chapterId && !(record.chapters || []).some((chapter) => chapter.id === chapterId)) { if (pendingChapter.current === chapterId) return; writeSelection(params, setParams, "direct", ""); } }, [record, id, rawScope, overview, chapterId, params, setParams, navigate]); useEffect(() => { if (entryId) return; restoreScroll(listRef.current, `tsumugu-scroll:case:${id}:${chapterId}:${overview ? "over" : "direct"}`); }, [entryId, id, chapterId, overview, record]); function chooseChapter(next) { const updated = new URLSearchParams(params); updated.delete("bereich"); updated.delete("eintrag"); if (next) updated.set("kapitel", next); else updated.delete("kapitel"); setParams(updated); setView("contents"); setStructureOpen(false); } function showOverview() { const updated = new URLSearchParams(params); updated.set("bereich", "ueberblick"); updated.delete("kapitel"); updated.delete("eintrag"); setParams(updated); setView("contents"); setStructureOpen(false); } function openEntry(binding) { rememberScroll(listRef.current, `tsumugu-scroll:case:${id}:${chapterId}:direct`); const updated = new URLSearchParams(params); updated.delete("bereich"); updated.set("eintrag", binding.id); setParams(updated); setView("contents"); } function closeEntry() { const updated = new URLSearchParams(params); updated.delete("eintrag"); setParams(updated, { replace: true }); } async function moveChapter(chapterIdToMove, delta) { const ids = (record?.chapters || []).map((chapter) => chapter.id); const index = ids.indexOf(chapterIdToMove); const target = index + delta; if (!record || index < 0 || target < 0 || target >= ids.length) return; const [moved] = ids.splice(index, 1); ids.splice(target, 0, moved); try { await saveChapterOrder({ containerKind: "case", containerId: id, record, ids }); await load(); } catch (err) { setError(err.message); } } if (error && !record) return
    ; if (!record) return

    Akte wird geladen …

    ; const canEdit = Boolean(record.rights?.edit); const targetLabel = `${record.kind_label || "Akte"} ${record.title}`; const activeChapter = (record.chapters || []).find((chapter) => chapter.id === chapterId) || null; const chapters = (record.chapters || []).map((chapter) => ({ ...chapter, count: (record.bindings || []).filter((item) => item.chapter_id === chapter.id).length, })); const merged = mergedCaseItems(record, dateDir, id); const contexts = [ ...(parentTree ? [{ id: parentTree.id, label: parentTree.title }] : []), { id: record.id, label: record.title }, ]; const entry = overview ? null : ((record.bindings || []).find((item) => item.id === entryId) || null); const hasContained = Boolean(record.contained?.length); return ( <> { if (next !== id) navigate(`/cases/${next}`); }) : null} nodes={caseNodes(record, parentTree)} allLabel="Direkt in dieser Akte" chapters={chapters} chapterId={overview ? "" : chapterId} allCount={(record.bindings || []).length} overview={hasContained ? { active: overview, count: merged.length, onSelect: showOverview } : null} chapterHeading={`Kapitel in ${record.title}`} onChapter={chooseChapter} onAddChapter={canEdit ? () => setModal("chapter") : null} canAddChapter={canEdit} onMoveChapter={moveChapter} canReorder={canEdit && chapters.length > 1} links={[]} onNavigate={() => setStructureOpen(false)} onClose={width < 1500 ? () => setStructureOpen(false) : null} /> )} reader={entryId && !overview ? ( ) : null} >

    {record.title}

    {record.status === "open" ? "Offen" : "Geschlossen"}
    {width < 1500 ? : null} {canEdit ? ( ) : null} {record.rights.edit ? : null} {record.rights.edit ? : null} {record.rights.edit ? : null} {record.rights.share || record.is_owner ? : null}
    {!canEdit ?

    In {targetLabel} fehlt das Bearbeitungsrecht.

    : null} {view === "contents" && overview ? (

    Überblick über direkte Inhalte und Inhalte der zugeordneten Vorgänge. Herkunft steht an jeder Zeile. Neue Inhalte und die eigene Reihenfolge gelten in der Akte oder im geöffneten Vorgang.

    setDateDir(next)} /> binding.readerHref} />
    ) : null} {view === "contents" && !overview ? ( { const search = new URLSearchParams(params); search.delete("bereich"); search.set("eintrag", binding.id); const text = search.toString(); return text ? `/cases/${id}?${text}` : `/cases/${id}`; }} /> ) : null} {view === "work" ? : null} {view === "course" || view === "audit" ? ( ) : null} {view === "links" ? (

    Zuordnungen

    Unterakten, Vorgänge und Freigaben dieses Objekts.

    {record.rights.edit ? : null} {record.rights.edit ? : null} {record.rights.edit ? : null}
    ) : null} {canEdit && view === "contents" && !overview ? ( ) : null}
    setModal("")}> { setModal(""); load(); }} /> setModal("")}> { setModal(""); load(); }} /> setModal("")}>

    Dieser Inhalt liegt direkt in der Akte. Er erscheint unter „Direkt in dieser Akte“ und im Überblick, nicht in einem Vorgang.

    setModal("")}> { setModal(""); load(); }} /> setModal("")}> { setModal(""); load(); }} /> setModal("")}> { setModal(""); load(); }} /> setModal("")}> { setModal(""); load(); }} /> setModal("")}> { setModal(""); load(); }} /> setModal("")}> { setModal(""); if (created?.id) { pendingChapter.current = created.id; writeSelection(params, setParams, "direct", created.id); } load(); }} /> setModal("")}> ); } export function ChapterBoard({ chapters, bindings, onOpen }) { const loose = bindings.filter((item) => !item.chapter_id); return (
    {chapters.map((chapter) => (

    {chapter.title}

    item.chapter_id === chapter.id)} onOpen={onOpen} />
    ))} {loose.length || !chapters.length ? (
    {chapters.length ?

    Ohne Kapitel

    : null}
    ) : null}
    ); } function BindingTiles({ items, onOpen, empty }) { if (!items.length) return

    {empty || "In diesem Kapitel liegt noch nichts."}

    ; return (
    {items.map((binding) => ( ))}
    ); } function sourceKind(element) { if (element.kind === "note") return "Notiz"; if (element.kind === "file") return "Datei"; if (element.external?.source === "paperless") return "Paperless"; if (element.external?.source === "imap") return "E-Mail"; return "Referenz"; } function NewMatter({ caseId, homeScopeId, onChange }) { const [title, setTitle] = useState(""); const [error, setError] = useState(""); const [pending, setPending] = useState(false); async function submit(event) { event.preventDefault(); if (pending) return; setError(""); setPending(true); try { const matter = await api("/api/v1/matters", { method: "POST", body: { title, ...(homeScopeId ? { home_scope_id: homeScopeId } : {}) } }); await api(`/api/v1/matters/${matter.id}/case-links`, { method: "POST", body: { case_id: caseId } }); setTitle(""); markDirty(false); onChange(); } catch (err) { setError(err.message); } finally { setPending(false); } } return (

    Vorgang anlegen und zuordnen

    ); } function AssignMatter({ caseId, onChange }) { const [matters, setMatters] = useState([]); const [matterId, setMatterId] = useState(""); const [error, setError] = useState(""); useEffect(() => { api("/api/v1/matters").then((data) => setMatters(data.items)).catch((err) => setError(err.message)); }, []); async function submit(event) { event.preventDefault(); setError(""); try { await api(`/api/v1/matters/${matterId}/case-links`, { method: "POST", body: { case_id: caseId } }); onChange(); } catch (err) { setError(err.message); } } return (
    ); } function NewSubcase({ parentId, onChange }) { const [title, setTitle] = useState(""); const [rightsMode, setRightsMode] = useState("inherit_parent"); async function submit(event) { event.preventDefault(); await api("/api/v1/cases", { method: "POST", body: { title, parent_case_id: parentId, rights_mode: rightsMode } }); setTitle(""); onChange(); } return (

    Unterakte

    setTitle(event.target.value)} placeholder="Titel der Unterakte" required />
    ); } function caseTrail(record, caseId, group, scope, chapter) { const extended = Boolean(group) || (scope === "direct" && chapter); const items = (record.trail || []).map((item, index, all) => ( extended && index === all.length - 1 ? { ...item, href: `/cases/${caseId}` } : item )); if (group) { items.push({ label: `${group.kind_label || "Vorgang"} ${group.title}`, href: chapter ? `/cases/${caseId}?bereich=${group.matter_id}` : null, }); } if (chapter && (group || scope === "direct")) items.push({ label: `Kapitel ${chapter.title}`, href: null }); return items; } export function ChapterForm({ path, onChange }) { const [title, setTitle] = useState(""); const [dated, setDated] = useState(""); const [error, setError] = useState(""); async function submit(event) { event.preventDefault(); setError(""); try { const created = await api(path, { method: "POST", body: { title, ...(dated ? { document_on: dated } : {}) } }); setTitle(""); setDated(""); onChange(created); } catch (err) { setError(err.message); } } return (
    ); } export function ContentForms({ context, placement, chapters, onChange }) { return (
    ); } function chapterSelect(chapters, value, setValue) { if (!chapters?.length) return null; return ( ); } export function NoteForm({ context, placement, chapters = [], initialChapter = "", targetLabel = "", onChange }) { const [title, setTitle] = useState(""); const [text, setText] = useState(""); const [documentOn, setDocumentOn] = useState(""); const [chapterId, setChapterId] = useState(initialChapter); const [error, setError] = useState(""); const [pending, setPending] = useState(false); useEffect(() => { setChapterId(initialChapter || ""); }, [initialChapter]); async function submit(event) { event.preventDefault(); if (pending) return; setError(""); setPending(true); try { await api("/api/v1/notes", { method: "POST", body: { title, document: textToDocument(text), context, ...(placement ? { placement } : {}), ...(chapterId ? { chapter_id: chapterId } : {}), ...(documentOn ? { dates: { document_on: documentOn } } : {}), }, }); setTitle(""); setText(""); setDocumentOn(""); markDirty(false); onChange(); } catch (err) { setError(err.message); } finally { setPending(false); } } return (

    Notiz