Warne vor bereits eingefügten Quelldokumenten und biete Nicht einfügen, erneut einfügen oder zusätzliches Einbinden an.
Deploy Development / deploy (push) Successful in 44s

Prüfstufen hier, darin, Umfeld stehen als Kontoeinstellung bereit. Der Server antwortet bei einem sicheren Treffer mit 409 und bindet auf Wunsch das vorhandene Element zusätzlich ein.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
StommerandClaude Sonnet 5.5 committed 2026-10-25 10:06:52 +01:00
1 parent a6dc24d8fe
commit f925661937
19 files changed
+748 -133

No files matched your search

+122
View File
@@ -0,0 +1,122 @@
import { useState } from "react";
import { api } from "../api.js";
import { ErrorNote } from "../pages/widgets.jsx";
// A document that is already present around the target. The server decides; this only asks and explains.
export function presentKey(source, connectorId, externalId) {
return `${source}:${connectorId || ""}:${externalId}`;
}
export function describeMatch(match) {
const title = match.container?.title;
const maybe = match.certain ? "" : "möglicherweise ";
if (match.where === "here") return `${maybe}schon hier${match.chapter ? ` (Kapitel „${match.chapter}“)` : ""}`;
if (match.where === "inside") return `${maybe}schon ${title ? `in „${title}“` : "in einem zugehörigen Teil"}`;
const kind = match.container?.kind === "case" ? "in der Akte" : "im Vorgang";
return `${maybe}schon ${kind}${title ? ` „${title}“` : ""}`;
}
export function bindable(matches) {
return (matches || []).find((item) => item.certain && item.can_bind && item.where !== "here") || null;
}
// entries: [{ source, external_id, connector_id }]. Answers a Map of presentKey -> matches, only for documents that are present.
// A failed lookup answers nothing: the warning is a help, the server still protects on insert.
export async function lookupPresent(context, placement, entries) {
const found = new Map();
if (!entries.length) return found;
try {
const data = await api("/api/v1/external-refs/existing", {
method: "POST",
body: {
context,
...(placement ? { placement } : {}),
items: entries.map((item) => ({ source: item.source, external_id: item.external_id, ...(item.connector_id ? { connector_id: item.connector_id } : {}) })),
},
});
for (const entry of data.items || []) {
if (entry.matches?.length) found.set(presentKey(entry.source, entry.connector_id, entry.external_id), entry.matches);
}
} catch {
return found;
}
return found;
}
export function PresentNote({ matches }) {
if (!matches?.length) return null;
return <p className="duplicate-note" role="status">Schon vorhanden: {matches.map(describeMatch).join(", ")}.</p>;
}
// Choices when a document is already present: do not insert, insert again, or bind the existing element as well.
export function DuplicatePanel({ matches, busy, onCancel, onInsert, onBind }) {
const existing = bindable(matches);
return (
<div className="card stack duplicate-panel" role="alert">
<strong>Dieses Dokument ist schon vorhanden.</strong>
<ul>
{matches.map((item) => <li key={item.element_id}>{item.title}: {describeMatch(item)}</li>)}
</ul>
<div className="row">
<button type="button" className="quiet" disabled={busy} onClick={onCancel}>Nicht einfügen</button>
<button type="button" className="quiet" disabled={busy} onClick={onInsert}>Trotzdem erneut einfügen</button>
{existing ? <button type="button" disabled={busy} onClick={() => onBind(existing.element_id)}>Vorhandenes zusätzlich einbinden</button> : null}
</div>
</div>
);
}
// One document at a time. `place(body)` posts it. If the server finds it already present, `panel` offers the choices
// and `onPlaced` runs once the document is in, whichever way.
export function useExternalPlacement({ context, placement, onPlaced }) {
const [conflict, setConflict] = useState(null);
const [busy, setBusy] = useState(false);
const [error, setError] = useState("");
async function send(body) {
await api("/api/v1/external-refs", { method: "POST", body });
setConflict(null);
await onPlaced?.();
}
async function place(body) {
setError("");
try {
await send(body);
} catch (err) {
if (err.code !== "already_present") throw err;
const entry = { source: body.source, external_id: body.external_id, connector_id: body.detail?.connector_id };
const found = await lookupPresent(context, placement, [entry]);
const matches = found.get(presentKey(entry.source, entry.connector_id, entry.external_id)) || [];
if (!matches.length) throw err;
setConflict({ body, matches });
}
}
async function resolve(extra) {
if (!conflict) return;
setBusy(true);
setError("");
try {
await send({ ...conflict.body, ...extra });
} catch (err) {
setError(err.message);
} finally {
setBusy(false);
}
}
const panel = conflict ? (
<>
<DuplicatePanel
matches={conflict.matches}
busy={busy}
onCancel={() => setConflict(null)}
onInsert={() => resolve({ duplicate: "insert" })}
onBind={(elementId) => resolve({ bind_element_id: elementId })}
/>
<ErrorNote error={error} />
</>
) : null;
return { place, panel };
}