let csrf = ""; let scope = null; let epoch = 0; let dirty = false; export function sessionState() { return { csrf, scope, epoch }; } export function applySession(data) { csrf = data.csrf_token || ""; scope = data.scope || null; epoch = data.epoch || 0; dirty = false; return data; } export function clearSession() { csrf = ""; scope = null; epoch = 0; dirty = false; } export function markDirty(value) { dirty = value; } export function isDirty() { return dirty; } function scopeHeaders() { if (!scope) return {}; return { "X-Scope-Kind": scope.kind, "X-Scope-Id": scope.id }; } export async function api(path, { method = "GET", body, form } = {}) { const started = epoch; const headers = { ...scopeHeaders() }; if (csrf && method !== "GET" && method !== "HEAD") { headers["X-CSRF-Token"] = csrf; } let payload; if (form) { payload = form; } else if (body !== undefined) { headers["Content-Type"] = "application/json"; payload = JSON.stringify(body); } const response = await fetch(path, { method, headers, body: payload, credentials: "include" }); if (started !== epoch) { const error = new Error("Der Datenraum wurde gewechselt."); error.status = 0; throw error; } if (response.status === 204) return null; const text = await response.text(); const data = text ? JSON.parse(text) : null; if (!response.ok) { const error = new Error(data?.detail || "Die Anfrage ist fehlgeschlagen."); error.status = response.status; error.code = data?.code; if (response.status === 401) clearSession(); throw error; } return data; } function mailQuery(context) { const params = new URLSearchParams(); if (context?.kind && context?.id) { params.set("context_kind", context.kind); params.set("context_id", context.id); } return params; } export async function fetchMailPart(elementId, context, suffix) { const response = await fetch(`/api/v1/external-refs/${elementId}/mail/${suffix}?${mailQuery(context)}`, { credentials: "include", headers: scopeHeaders(), }); if (!response.ok) { const data = await response.json().catch(() => null); const fallback = suffix === "original" ? "Die vollständige E-Mail ist nicht verfügbar." : "Die Anlage ist nicht verfügbar."; throw new Error(data?.detail || fallback); } return response.blob(); } export async function downloadMailPart(elementId, context, suffix, filename) { const blob = await fetchMailPart(elementId, context, suffix); const url = URL.createObjectURL(blob); const link = document.createElement("a"); link.href = url; link.download = filename || "nachricht.eml"; document.body.appendChild(link); link.click(); link.remove(); setTimeout(() => URL.revokeObjectURL(url), 60000); } export async function fetchPaperless(elementId, context) { const params = new URLSearchParams(); if (context?.kind && context?.id) { params.set("context_kind", context.kind); params.set("context_id", context.id); } const response = await fetch(`/api/v1/external-refs/${elementId}/content?${params}`, { credentials: "include", headers: scopeHeaders(), }); if (!response.ok) { const data = await response.json().catch(() => null); throw new Error(data?.detail || "Das Paperless-Dokument ist nicht verfügbar."); } return response.blob(); } export async function fetchFile(elementId, context) { const params = new URLSearchParams(); if (context?.kind && context?.id) { params.set("context_kind", context.kind); params.set("context_id", context.id); } const response = await fetch(`/api/v1/files/${elementId}/content?${params}`, { credentials: "include", headers: scopeHeaders(), }); if (!response.ok) throw new Error("Die Datei ist nicht verfügbar."); return response.blob(); } export async function downloadFile(elementId, context, filename) { const params = new URLSearchParams(); if (context?.kind && context?.id) { params.set("context_kind", context.kind); params.set("context_id", context.id); } const response = await fetch(`/api/v1/files/${elementId}/content?${params}`, { credentials: "include", headers: scopeHeaders(), }); if (!response.ok) { throw new Error("Die Datei ist nicht verfügbar."); } const blob = await response.blob(); const url = URL.createObjectURL(blob); const link = document.createElement("a"); link.href = url; link.download = filename || "datei"; document.body.appendChild(link); link.click(); link.remove(); URL.revokeObjectURL(url); } export async function downloadPost(path, body, fallbackName) { const headers = { ...scopeHeaders(), "Content-Type": "application/json" }; if (csrf) headers["X-CSRF-Token"] = csrf; const response = await fetch(path, { method: "POST", headers, body: JSON.stringify(body), credentials: "include", }); if (!response.ok) { const data = await response.json().catch(() => null); const error = new Error(data?.detail || "Die Anfrage ist fehlgeschlagen."); error.status = response.status; throw error; } const blob = await response.blob(); const disposition = response.headers.get("Content-Disposition") || ""; const extended = disposition.match(/filename\*=UTF-8''([^;]+)/i); const plain = disposition.match(/filename="([^"]+)"/); const filename = extended ? decodeURIComponent(extended[1]) : (plain && plain[1]) || fallbackName || "uebergabe.zip"; const url = URL.createObjectURL(blob); const link = document.createElement("a"); link.href = url; link.download = filename; document.body.appendChild(link); link.click(); link.remove(); URL.revokeObjectURL(url); } export function textToDocument(text) { return { version: 1, blocks: [{ type: "markdown", text: text || "" }] }; } export function documentToText(document) { return (document?.blocks || []).map((block) => block.text || "").join("\n"); }