261 lines
9.0 KiB
React
261 lines
9.0 KiB
React
import { useEffect, useRef, useState } from "react";
|
|
import { api, downloadPost } from "../api.js";
|
|
import { symbolUrl } from "../content-symbol.mjs";
|
|
|
|
function leafIds(node, into = []) {
|
|
if (!node) return into;
|
|
if (node.kind === "binding" || node.kind === "annotation" || node.kind === "attachment" || node.kind === "dispatch_item" || node.kind === "evidence") into.push(node.id);
|
|
(node.children || []).forEach((child) => leafIds(child, into));
|
|
return into;
|
|
}
|
|
|
|
function checkState(node, selected) {
|
|
const ids = leafIds(node);
|
|
if (!ids.length) return "off";
|
|
const count = ids.filter((id) => selected.has(id)).length;
|
|
if (count === 0) return "off";
|
|
if (count === ids.length) return "on";
|
|
return "mixed";
|
|
}
|
|
|
|
function withNode(node, selected, on) {
|
|
const next = new Set(selected);
|
|
leafIds(node).forEach((id) => {
|
|
if (on) next.add(id);
|
|
else next.delete(id);
|
|
});
|
|
return next;
|
|
}
|
|
|
|
function initialSelection(node) {
|
|
const next = new Set();
|
|
function visit(current) {
|
|
if ((current.kind === "binding" || current.kind === "annotation" || current.kind === "attachment" || current.kind === "dispatch_item" || current.kind === "evidence") && current.exportable) {
|
|
next.add(current.id);
|
|
}
|
|
(current.children || []).forEach(visit);
|
|
}
|
|
visit(node);
|
|
return next;
|
|
}
|
|
|
|
function CheckBox({ state, disabled, onChange, label }) {
|
|
const ref = useRef(null);
|
|
useEffect(() => {
|
|
if (ref.current) ref.current.indeterminate = state === "mixed";
|
|
}, [state]);
|
|
return (
|
|
<input
|
|
ref={ref}
|
|
type="checkbox"
|
|
checked={state === "on"}
|
|
disabled={disabled}
|
|
aria-checked={state === "mixed" ? "mixed" : state === "on"}
|
|
aria-label={label}
|
|
onChange={onChange}
|
|
/>
|
|
);
|
|
}
|
|
|
|
function ContentMark({ name }) {
|
|
return (
|
|
<span className="export-tree__mark" aria-hidden="true">
|
|
<img className="type-glyph type-glyph--light" src={symbolUrl("light", name)} width="16" height="16" alt="" />
|
|
<img className="type-glyph type-glyph--dark" src={symbolUrl("dark", name)} width="16" height="16" alt="" />
|
|
</span>
|
|
);
|
|
}
|
|
|
|
function symbolFor(node) {
|
|
if (node.kind === "chapter") return "chapter";
|
|
if (node.kind === "matter") return node.label === "Teilvorgang" ? "submatter" : "matter";
|
|
if (node.kind === "annotation" || node.content_kind === "note") return "note";
|
|
if (node.content_kind === "email") return "email";
|
|
if (node.content_kind === "paperless") return "source-archive";
|
|
if (node.content_kind === "url") return "link";
|
|
return "file";
|
|
}
|
|
|
|
function TreeBranch({ node, selected, onToggle }) {
|
|
const state = checkState(node, selected);
|
|
const ids = leafIds(node);
|
|
const branch = node.kind === "matter" || node.kind === "chapter";
|
|
return (
|
|
<li>
|
|
<label className={branch ? "export-tree__row is-branch" : "export-tree__row"}>
|
|
<CheckBox
|
|
state={state}
|
|
disabled={!ids.length}
|
|
label={node.label ? `${node.label}: ${node.title}` : node.title}
|
|
onChange={() => onToggle(node, state !== "on")}
|
|
/>
|
|
<ContentMark name={symbolFor(node)} />
|
|
<span className="export-tree__text">
|
|
<span className="export-tree__line">
|
|
<span className="export-tree__title">{node.title}</span>
|
|
{node.label ? <span className="export-tree__kind">{node.label}</span> : null}
|
|
</span>
|
|
{node.reason ? <span className="export-reason">{node.reason}</span> : null}
|
|
</span>
|
|
</label>
|
|
{node.children?.length ? (
|
|
<ul>
|
|
{node.children.map((child) => (
|
|
<TreeBranch key={child.id} node={child} selected={selected} onToggle={onToggle} />
|
|
))}
|
|
</ul>
|
|
) : null}
|
|
</li>
|
|
);
|
|
}
|
|
|
|
export function ExportPackage({ matterId, context, onClose }) {
|
|
const [tree, setTree] = useState(null);
|
|
const [selected, setSelected] = useState(() => new Set());
|
|
const [review, setReview] = useState(null);
|
|
const [assignments, setAssignments] = useState({});
|
|
const [error, setError] = useState("");
|
|
const [pending, setPending] = useState(false);
|
|
const [downloaded, setDownloaded] = useState(false);
|
|
const query = context?.kind && context?.id ? `?context_kind=${context.kind}&context_id=${context.id}` : "";
|
|
|
|
useEffect(() => {
|
|
let alive = true;
|
|
api(`/api/v1/matters/${matterId}/export-tree${query}`).then((payload) => {
|
|
if (!alive) return;
|
|
setTree(payload.tree);
|
|
setSelected(initialSelection(payload.tree));
|
|
}).catch((err) => {
|
|
if (alive) setError(err.message);
|
|
});
|
|
return () => {
|
|
alive = false;
|
|
};
|
|
}, [matterId, query]);
|
|
|
|
function toggle(node, on) {
|
|
setSelected((current) => withNode(node, current, on));
|
|
setReview(null);
|
|
setDownloaded(false);
|
|
}
|
|
|
|
async function examine() {
|
|
setError("");
|
|
setPending(true);
|
|
setDownloaded(false);
|
|
try {
|
|
const payload = await api(`/api/v1/matters/${matterId}/export/review`, {
|
|
method: "POST",
|
|
body: {
|
|
context: context || null,
|
|
selected: [...selected],
|
|
assignments,
|
|
},
|
|
});
|
|
setReview(payload);
|
|
} catch (err) {
|
|
setError(err.message);
|
|
} finally {
|
|
setPending(false);
|
|
}
|
|
}
|
|
|
|
async function download() {
|
|
setError("");
|
|
setPending(true);
|
|
try {
|
|
await downloadPost(`/api/v1/matters/${matterId}/export`, {
|
|
context: context || null,
|
|
selected: [...selected],
|
|
assignments,
|
|
}, "uebergabe.zip");
|
|
setDownloaded(true);
|
|
} catch (err) {
|
|
setError(err.message);
|
|
} finally {
|
|
setPending(false);
|
|
}
|
|
}
|
|
|
|
const ambiguous = (review?.issues || []).filter((issue) => issue.choices?.length);
|
|
|
|
return (
|
|
<div className="stack export-package">
|
|
<p className="export-lead">
|
|
Die Auswahl gilt für diese Ausgabe. Ein bereits heruntergeladenes Paket verändert sich nicht,
|
|
wenn danach Inhalte geändert oder ergänzt werden.
|
|
</p>
|
|
{error ? <p className="export-reason">{error}</p> : null}
|
|
{!tree ? <p className="muted">Auswahl wird geladen …</p> : null}
|
|
{tree && !review ? (
|
|
<>
|
|
<ul className="export-tree" role="group" aria-label="Exportauswahl">
|
|
<TreeBranch node={tree} selected={selected} onToggle={toggle} />
|
|
</ul>
|
|
<div className="export-actions">
|
|
<button type="button" onClick={examine} disabled={pending || selected.size === 0}>
|
|
{pending ? "Prüfung läuft …" : "Auswahl prüfen"}
|
|
</button>
|
|
<button type="button" className="quiet" onClick={onClose}>Abbrechen</button>
|
|
</div>
|
|
</>
|
|
) : null}
|
|
{review ? (
|
|
<>
|
|
<div className="export-review">
|
|
{review.issues?.length ? review.issues.map((issue) => (
|
|
<p key={`${issue.node_id}-${issue.code}`} className="export-reason">
|
|
{issue.title}: {issue.message}
|
|
</p>
|
|
)) : <p>Die Auswahl ist vollständig und kann ausgegeben werden.</p>}
|
|
{review.notices?.map((notice) => (
|
|
<p key={`${notice.node_id}-${notice.message}`} className="export-notice">{notice.message}</p>
|
|
))}
|
|
{ambiguous.map((issue) => (
|
|
<label key={`${issue.node_id}-choice`}>
|
|
Zugang für {issue.title}
|
|
<select
|
|
value={assignments[issue.node_id] || ""}
|
|
onChange={(event) => {
|
|
setAssignments((current) => ({ ...current, [issue.node_id]: event.target.value }));
|
|
setReview(null);
|
|
}}
|
|
>
|
|
<option value="">Zugang wählen</option>
|
|
{issue.choices.map((choice) => (
|
|
<option key={choice.id} value={choice.id}>{choice.name}</option>
|
|
))}
|
|
</select>
|
|
</label>
|
|
))}
|
|
<ul className="export-entries">
|
|
{(review.entries || []).map((entry) => (
|
|
<li key={entry.node_id}>
|
|
<span className="export-tree__line">
|
|
<span className="export-tree__title">{entry.title}</span>
|
|
{entry.kind_label ? <span className="export-tree__kind">{entry.kind_label}</span> : null}
|
|
</span>
|
|
{entry.path ? <span className="export-tree__kind">{entry.path}</span> : null}
|
|
</li>
|
|
))}
|
|
</ul>
|
|
</div>
|
|
{downloaded ? (
|
|
<p className="muted">Das Paket wurde heruntergeladen. Es hält den Stand dieses Zeitpunkts fest.</p>
|
|
) : null}
|
|
<div className="export-actions">
|
|
<button type="button" className="quiet" onClick={() => setReview(null)}>Zurück zur Auswahl</button>
|
|
{review.ready ? (
|
|
<button type="button" onClick={download} disabled={pending}>
|
|
{pending ? "Paket wird erzeugt …" : "Paket herunterladen"}
|
|
</button>
|
|
) : (
|
|
<button type="button" onClick={examine} disabled={pending}>Erneut prüfen</button>
|
|
)}
|
|
</div>
|
|
</>
|
|
) : null}
|
|
</div>
|
|
);
|
|
}
|