"use client"
import { useEffect, useState } from "react"
/**
* OwnerApprovals — NAR-Freigaben (Kernfeature 5 / NAR-Verfahren §1.3).
* Liste der Owner-Inbox-Tasks (an tower-owner), Detail-Ansicht je Antrag mit
* geparsten Pflichtfeldern (Antragsteller / Ziel / Protokoll / Zweck / Dauer /
* Alternativen) + CISO-Bewertung, Approve/Decline mit Notizfeld.
* Bescheid geht via board-service decide_owner_task an den Antragsteller.
* Poll 5 s, fail loud.
*/
type OwnerTask = {
task_id: string
from: string
type: string
priority: string
status: string
created?: number
topic?: string
body?: string
}
type ParsedNar = {
antragsteller: string
ziel: string
protokoll: string
zweck: string
dauer: string
alternativen: string
bewertung: string
empfehlung: string
}
function section(body: string, startMark: string, endMarks: string[]): string {
const s = body.indexOf(startMark)
if (s === -1) return ""
let end = body.length
for (const m of endMarks) {
const i = body.indexOf(m, s + startMark.length)
if (i !== -1 && i < end) end = i
}
return body.slice(s, end).trim()
}
function field(src: string, label: string): string {
const re = new RegExp(`${label}:\\s*(.*)`)
const m = src.match(re)
return (m ? m[1] : "").trim()
}
function parseNar(body: string, from: string): ParsedNar {
const antrag = section(body, "Antragsteller:", [
"\nZiel:", "\nProtokoll:", "\nZweck:", "\nDauer:", "\nAlternativen:",
])
const ziel = section(body, "Ziel:", ["\nProtokoll:", "\nZweck:", "\nDauer:", "\nAlternativen:"])
const protokoll = section(body, "Protokoll:", ["\nZweck:", "\nDauer:", "\nAlternativen:"])
const zweck = section(body, "Zweck:", ["\nDauer:", "\nAlternativen:"])
const dauer = section(body, "Dauer:", ["\nAlternativen:"])
const alternativen = section(body, "Alternativen:", ["\n---"])
const bewertung = section(body, "--- CISO-BEWERTUNG ---", ["\n--- EMPFEHLUNG"])
const empfehlung = body.includes("--- EMPFEHLUNG")
? body.slice(body.indexOf("--- EMPFEHLUNG")).trim()
: ""
return {
antragsteller: field(antrag, "Antragsteller") || from,
ziel: field(ziel, "Ziel"),
protokoll: field(protokoll, "Protokoll"),
zweck: zweck.replace(/^Zweck:\s*/, "").trim(),
dauer: field(dauer, "Dauer"),
alternativen: alternativen.replace(/^Alternativen:\s*/, "").trim(),
bewertung,
empfehlung,
}
}
const InfoRow = ({ label, value }: { label: string; value: string }) => (
{label}
{value || "—"}
)
export default function OwnerApprovals() {
const [tasks, setTasks] = useState([])
const [error, setError] = useState(null)
const [selected, setSelected] = useState(null)
const [note, setNote] = useState("")
const [busy, setBusy] = useState(false)
const [feedback, setFeedback] = useState<{ ok: boolean; msg: string } | null>(null)
const load = () => {
fetch("/api/fleet/owner/inbox", { cache: "no-store" })
.then(async (res) => {
if (!res.ok) throw new Error((await res.json()).error || `HTTP ${res.status}`)
return res.json()
})
.then((d: { tasks: OwnerTask[] }) => {
setTasks(d.tasks || [])
setError(null)
})
.catch((e: Error) => setError(e.message))
}
useEffect(() => {
load()
const timer = setInterval(load, 5000)
return () => clearInterval(timer)
}, [])
const decide = (decision: "approve" | "decline") => {
if (!selected || busy) return
setBusy(true)
setFeedback(null)
fetch("/api/fleet/owner/decide", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ task_id: selected, decision, note }),
})
.then(async (res) => {
const body = await res.json()
if (!res.ok) throw new Error(body.error || `HTTP ${res.status}`)
return body
})
.then((d) => {
const ft = d.folge_task?.task?.task_id || d.folge_task?.task_id || "?"
setFeedback({
ok: true,
msg: `${decision === "approve" ? "Freigegeben" : "Abgelehnt"} — Bescheid-Task ${String(ft).slice(0, 8)} an ${d.antragsteller}`,
})
setNote("")
setSelected(null)
setTimeout(load, 500)
})
.catch((e: Error) => setFeedback({ ok: false, msg: e.message }))
.finally(() => setBusy(false))
}
const open = tasks.filter((t) => t.status === "submitted")
const done = tasks.filter((t) => t.status !== "submitted")
const current = tasks.find((t) => t.task_id === selected)
const nar = current ? parseNar(current.body || "", current.from) : null
const row = (t: OwnerTask, isDone: boolean) => (
)
return (
FREIGABEN
{open.length} offen · {done.length} entschieden
{feedback && (
{feedback.msg}
)}
{error && (
)}
Offene Anträge
{open.length === 0 ? (
Keine offenen Anträge
) : (
open.map((t) => row(t, false))
)}
{done.length > 0 && (
Entscheidungs-Historie
)}
{done.map((t) => row(t, true))}
{current && nar ? (
{current.type} · {current.priority}
{current.topic}
{current.status !== "submitted" && (
{current.status}
)}
Wer braucht Zugriff worauf?
Wofür / Wie lange?
{nar.bewertung && (
CISO-Bewertung
{nar.bewertung}
)}
{nar.empfehlung && (
Empfehlung des CISO
{nar.empfehlung}
)}
{current.status === "submitted" ? (
) : (
Entscheidung getroffen — Bescheid ist an den Antragsteller gegangen.
)}
) : (
Antrag auswählen
)}
)
}