396 lines
14 KiB
React
396 lines
14 KiB
React
import { DocumentView } from "./DocumentView.jsx";
|
|
import { useEffect, useRef, useState } from "react";
|
|
import { Link } from "react-router-dom";
|
|
import { api, documentToText, downloadFile, fetchExternal, markDirty, textToDocument } from "../api.js";
|
|
import { stageFor } from "../sources/registry.jsx";
|
|
import "../sources/builtin-views.jsx";
|
|
import { NoteEditor } from "../notes.jsx";
|
|
import { moduleFor } from "../sources/registry-core.js";
|
|
import { Modal } from "./Modal.jsx";
|
|
|
|
function RegisteredNote(props) {
|
|
const Stage = stageFor("note");
|
|
return Stage ? <Stage {...props} /> : null;
|
|
}
|
|
|
|
export function ErrorNote({ error }) {
|
|
if (!error) return null;
|
|
return <p className="error">{error}</p>;
|
|
}
|
|
|
|
export function Chronology({ items }) {
|
|
if (!items?.length) return <p className="muted">Noch keine sichtbaren Ereignisse.</p>;
|
|
return (
|
|
<ol className="timeline">
|
|
{items.map((item) => (
|
|
<li key={item.id}>
|
|
<time>{item.occurred_on || item.occurred_at.slice(0, 10)}</time>
|
|
<div>
|
|
<strong>{item.label}</strong>
|
|
<span>{item.title}</span>
|
|
{item.occurred_at ? <small>Erfasst {item.occurred_at.replace("T", " ").replace("Z", " UTC")}</small> : null}
|
|
</div>
|
|
</li>
|
|
))}
|
|
</ol>
|
|
);
|
|
}
|
|
|
|
export function BindingList({ bindings, context, canEdit, isOwner, onChange }) {
|
|
if (!bindings.length) return <p className="muted">In diesem Kontext liegt noch nichts.</p>;
|
|
return (
|
|
<div className="stack">
|
|
{bindings.map((binding) => (
|
|
<Binding key={binding.id} binding={binding} context={context} canEdit={canEdit} isOwner={isOwner} onChange={onChange} />
|
|
))}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function Binding({ binding, context, canEdit, isOwner, onChange }) {
|
|
const element = binding.element;
|
|
const [text, setText] = useState(element.kind === "note" ? documentToText(element.document) : "");
|
|
const [note, setNote] = useState("");
|
|
const [targets, setTargets] = useState([]);
|
|
const [targetKey, setTargetKey] = useState("");
|
|
const [error, setError] = useState("");
|
|
const [notice, setNotice] = useState("");
|
|
const [previewUrl, setPreviewUrl] = useState("");
|
|
const [viewing, setViewing] = useState(false);
|
|
const [editingNote, setEditingNote] = useState(false);
|
|
const frameRef = useRef(null);
|
|
|
|
useEffect(() => () => {
|
|
if (previewUrl) URL.revokeObjectURL(previewUrl);
|
|
}, [previewUrl]);
|
|
|
|
async function viewOriginal() {
|
|
setError("");
|
|
setViewing(true);
|
|
try {
|
|
const blob = await fetchExternal(element.id, context);
|
|
if (previewUrl) URL.revokeObjectURL(previewUrl);
|
|
setPreviewUrl(URL.createObjectURL(blob));
|
|
} catch (err) {
|
|
setViewing(false);
|
|
setError(err.message);
|
|
}
|
|
}
|
|
|
|
async function saveNote(event) {
|
|
event.preventDefault();
|
|
setError("");
|
|
try {
|
|
const updated = await api(`/api/v1/notes/${element.id}`, {
|
|
method: "PATCH",
|
|
body: {
|
|
expected_version: element.version,
|
|
document: textToDocument(text),
|
|
...(context ? { context } : {}),
|
|
},
|
|
});
|
|
markDirty(false);
|
|
setEditingNote(false);
|
|
setNotice(updated.notice);
|
|
onChange();
|
|
} catch (err) {
|
|
if (err.status === 409) {
|
|
setError("Der Stand hat sich geändert. Der eingegebene Text bleibt stehen. Bitte neu laden.");
|
|
return;
|
|
}
|
|
setError(err.message);
|
|
}
|
|
}
|
|
|
|
async function annotate(event) {
|
|
event.preventDefault();
|
|
setError("");
|
|
const params = new URLSearchParams();
|
|
if (context) {
|
|
params.set("context_kind", context.kind);
|
|
params.set("context_id", context.id);
|
|
}
|
|
try {
|
|
await api(`/api/v1/bindings/${binding.id}/annotations?${params}`, { method: "POST", body: { body: note } });
|
|
setNote("");
|
|
markDirty(false);
|
|
onChange();
|
|
} catch (err) {
|
|
setError(err.message);
|
|
}
|
|
}
|
|
|
|
async function detach() {
|
|
if (!window.confirm("Nur diese Einbindung entfernen? Das Element und andere Kontexte bleiben erhalten.")) return;
|
|
const params = new URLSearchParams();
|
|
if (context) {
|
|
params.set("context_kind", context.kind);
|
|
params.set("context_id", context.id);
|
|
}
|
|
await api(`/api/v1/bindings/${binding.id}?${params}`, { method: "DELETE" });
|
|
onChange();
|
|
}
|
|
|
|
async function destroy() {
|
|
if (!window.confirm("Das Element überall löschen? Alle Einbindungen verschwinden. Eine Paperless-Quelle bleibt unangetastet.")) return;
|
|
await api(`/api/v1/elements/${element.id}/delete`, { method: "POST", body: { expected_version: element.version } });
|
|
onChange();
|
|
}
|
|
|
|
async function loadTargets() {
|
|
if (targets.length) return;
|
|
const data = await api("/api/v1/placement-targets");
|
|
setTargets(data.items);
|
|
}
|
|
|
|
async function bindElsewhere(event) {
|
|
event.preventDefault();
|
|
const target = targets.find((item) => `${item.kind}:${item.id}` === targetKey);
|
|
if (!target || !context) return;
|
|
const body = {
|
|
element_id: element.id,
|
|
source_context: context,
|
|
target: { kind: target.kind, id: target.id },
|
|
};
|
|
if (target.kind === "matter") {
|
|
const via = (target.contexts || []).find((item) => item.kind === "direct") || target.contexts?.[0];
|
|
if (via && via.kind !== "direct") body.target_context = { kind: via.kind, id: via.id };
|
|
}
|
|
setError("");
|
|
try {
|
|
await api("/api/v1/bindings", { method: "POST", body });
|
|
setTargetKey("");
|
|
onChange();
|
|
} catch (err) {
|
|
setError(err.message);
|
|
}
|
|
}
|
|
|
|
async function protect(event) {
|
|
const remove = event.target.checked;
|
|
await api("/api/v1/exceptions", {
|
|
method: "PUT",
|
|
body: {
|
|
resource_kind: "element",
|
|
resource_id: element.id,
|
|
context,
|
|
remove_read: false,
|
|
remove_edit: remove,
|
|
remove_share: false,
|
|
},
|
|
});
|
|
onChange();
|
|
}
|
|
|
|
const dates = element.dates || {};
|
|
|
|
return (
|
|
<article className="item">
|
|
<header className="item-head">
|
|
<div>
|
|
<p className="eyebrow">{kindLabel(element)}</p>
|
|
<h3>{element.title}</h3>
|
|
</div>
|
|
<div className="row">
|
|
{canEdit ? <button type="button" className="quiet" onClick={detach}>Aus diesem Kontext entfernen</button> : null}
|
|
{isOwner ? <button type="button" className="danger" onClick={destroy}>Überall löschen</button> : null}
|
|
</div>
|
|
</header>
|
|
<DateLine dates={dates} />
|
|
{element.kind === "note" ? <RegisteredNote element={element} context={context} binding={binding} onReload={onChange} /> : null}
|
|
{element.kind === "file" && element.file ? (
|
|
element.file.status === "active" ? (
|
|
<button type="button" onClick={() => downloadFile(element.id, context, element.file.filename)}>
|
|
{element.file.filename} herunterladen
|
|
</button>
|
|
) : (
|
|
<p className="error">Die Datei ist gerade nicht verfügbar.</p>
|
|
)
|
|
) : null}
|
|
{element.kind === "external_ref" && element.external ? (
|
|
<div className="ref">
|
|
<p>
|
|
{moduleFor(element.external.source)?.addLabel || moduleFor(element.external.source)?.title || "Externe Adresse"}
|
|
{element.external.detail?.correspondent ? ` · ${element.external.detail.correspondent}` : ""}
|
|
{element.external.detail?.message_from ? ` · ${element.external.detail.message_from}` : ""}
|
|
</p>
|
|
{moduleFor(element.external.source)?.representation && moduleFor(element.external.source).representation !== "email" ? (
|
|
<button type="button" onClick={viewOriginal}>Ansehen</button>
|
|
) : (
|
|
<span>Verknüpft, ohne die Quelle zu kopieren.</span>
|
|
)}
|
|
</div>
|
|
) : null}
|
|
<Modal
|
|
open={viewing}
|
|
wide
|
|
title={element.title}
|
|
onClose={() => setViewing(false)}
|
|
>
|
|
<div className="viewer">
|
|
{previewUrl ? <DocumentView frameRef={frameRef} className="" title={element.title} url={previewUrl} /> : <p className="muted">Dokument wird geladen …</p>}
|
|
<div className="row">
|
|
<button type="button" onClick={() => frameRef.current?.contentWindow?.print()} disabled={!previewUrl}>Drucken</button>
|
|
</div>
|
|
</div>
|
|
</Modal>
|
|
{binding.rights.share && context ? (
|
|
<form onSubmit={bindElsewhere} className="inline" onFocus={loadTargets}>
|
|
<select value={targetKey} onChange={(event) => setTargetKey(event.target.value)} onFocus={loadTargets}>
|
|
<option value="">In weiteren Kontext einbinden</option>
|
|
{targets
|
|
.filter((item) => !(item.kind === context.kind && item.id === context.id))
|
|
.map((item) => (
|
|
<option key={`${item.kind}:${item.id}`} value={`${item.kind}:${item.id}`}>
|
|
{item.kind === "case" ? "Akte" : "Vorgang"}: {item.title}
|
|
</option>
|
|
))}
|
|
</select>
|
|
<button type="submit" disabled={!targetKey}>Einbinden</button>
|
|
</form>
|
|
) : null}
|
|
{isOwner && context ? (
|
|
<label className="check">
|
|
<input type="checkbox" checked={Boolean(binding.exception?.remove_edit)} onChange={protect} />
|
|
Schreibschutz in diesem Kontext
|
|
</label>
|
|
) : null}
|
|
<div className="annotations">
|
|
<h4>Anmerkungen nur hier</h4>
|
|
{binding.annotations.length ? (
|
|
<ul>
|
|
{binding.annotations.map((item) => (
|
|
<li key={item.id}>
|
|
<strong>{item.author_display_name}</strong>
|
|
<span>{item.body}</span>
|
|
</li>
|
|
))}
|
|
</ul>
|
|
) : (
|
|
<p className="muted">Keine Anmerkung in diesem Kontext.</p>
|
|
)}
|
|
{canEdit ? (
|
|
<form onSubmit={annotate} className="inline">
|
|
<input
|
|
value={note}
|
|
onChange={(event) => {
|
|
setNote(event.target.value);
|
|
markDirty(true);
|
|
}}
|
|
placeholder="Anmerkung für diesen Kontext"
|
|
/>
|
|
<button type="submit">Anmerken</button>
|
|
</form>
|
|
) : null}
|
|
</div>
|
|
<ErrorNote error={error} />
|
|
</article>
|
|
);
|
|
}
|
|
|
|
function DateLine({ dates }) {
|
|
const parts = [
|
|
["Dokumentdatum", dates.document_on],
|
|
["Versand", dates.sent_on],
|
|
["Zugang", dates.received_on],
|
|
["Erfassung", dates.recorded_at ? dates.recorded_at.slice(0, 10) : null],
|
|
].filter(([, value]) => value);
|
|
if (!parts.length) return null;
|
|
return (
|
|
<dl className="dates">
|
|
{parts.map(([label, value]) => (
|
|
<div key={label}>
|
|
<dt>{label}</dt>
|
|
<dd>{value}</dd>
|
|
</div>
|
|
))}
|
|
</dl>
|
|
);
|
|
}
|
|
|
|
function kindLabel(element) {
|
|
const kind = element.kind;
|
|
if (kind === "note") return "Notiz";
|
|
if (kind === "file") return "Datei";
|
|
const module = moduleFor(element.external?.source);
|
|
if (module) return module.addLabel || module.title;
|
|
if (kind === "external_ref") return "Referenz";
|
|
return kind;
|
|
}
|
|
|
|
export function ShareBox({ path, grants, onChange, allowSources = false }) {
|
|
const [email, setEmail] = useState("");
|
|
const [canEdit, setCanEdit] = useState(true);
|
|
const [canShare, setCanShare] = useState(false);
|
|
const [useOwnerSources, setUseOwnerSources] = useState(false);
|
|
const [error, setError] = useState("");
|
|
|
|
async function submit(event) {
|
|
event.preventDefault();
|
|
setError("");
|
|
const body = { email, can_read: true, can_edit: canEdit, can_share: canShare };
|
|
if (allowSources) body.use_owner_sources = useOwnerSources;
|
|
try {
|
|
await api(path, { method: "POST", body });
|
|
setEmail("");
|
|
onChange();
|
|
} catch (err) {
|
|
setError(err.message);
|
|
}
|
|
}
|
|
|
|
async function revoke(id) {
|
|
await api(`/api/v1/grants/${id}`, { method: "DELETE" });
|
|
onChange();
|
|
}
|
|
|
|
return (
|
|
<section className="card">
|
|
<h2>Freigeben</h2>
|
|
<p className="muted">Bearbeiten und Weitergeben sind getrennt. Die Freigabe gilt für diesen Kontext, nicht für andere Akten.</p>
|
|
<form onSubmit={submit} className="stack">
|
|
<label>
|
|
E-Mail
|
|
<input type="email" value={email} onChange={(event) => setEmail(event.target.value)} required />
|
|
</label>
|
|
<label className="check"><input type="checkbox" checked={canEdit} onChange={(event) => setCanEdit(event.target.checked)} /> Bearbeiten</label>
|
|
<label className="check"><input type="checkbox" checked={canShare} onChange={(event) => setCanShare(event.target.checked)} /> Weitergeben</label>
|
|
{allowSources ? (
|
|
<label className="check">
|
|
<input type="checkbox" checked={useOwnerSources} onChange={(event) => setUseOwnerSources(event.target.checked)} />
|
|
Verknüpfte Originale über meine Zugänge zeigen
|
|
</label>
|
|
) : null}
|
|
<button type="submit">Freigeben</button>
|
|
<ErrorNote error={error} />
|
|
</form>
|
|
{grants?.length ? (
|
|
<ul className="people">
|
|
{grants.map((grant) => (
|
|
<li key={grant.id}>
|
|
<span>{grant.display_name} · {grant.email}</span>
|
|
<small>
|
|
{grant.can_edit ? "Bearbeiten" : "Lesen"}
|
|
{grant.can_share ? ", Weitergeben" : ""}
|
|
{grant.use_owner_sources ? ", Originale über meine Zugänge" : ""}
|
|
</small>
|
|
<button type="button" className="quiet" onClick={() => revoke(grant.id)}>Entziehen</button>
|
|
</li>
|
|
))}
|
|
</ul>
|
|
) : null}
|
|
</section>
|
|
);
|
|
}
|
|
|
|
export function RecordLink({ kind, id, title, context }) {
|
|
const params = new URLSearchParams();
|
|
if (context?.kind && context.kind !== "direct") {
|
|
params.set("context_kind", context.kind);
|
|
params.set("context_id", context.id);
|
|
}
|
|
const suffix = params.toString() ? `?${params}` : "";
|
|
const path = kind === "case" ? `/cases/${id}` : `/matters/${id}${suffix}`;
|
|
return <Link to={path}>{title}</Link>;
|
|
}
|