Deploy Development / deploy (push) Successful in 47s
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
122 lines
4.7 KiB
React
122 lines
4.7 KiB
React
import { useState } from "react";
|
|
import { api } from "../api.js";
|
|
|
|
// 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 })}
|
|
/>
|
|
{error ? <p className="error" role="alert">{error}</p> : null}
|
|
</>
|
|
) : null;
|
|
return { place, panel };
|
|
}
|