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
Schon vorhanden: {matches.map(describeMatch).join(", ")}.
; } // 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 ({error}
: null} > ) : null; return { place, panel }; }