"use client" import { useEffect, useRef, useState } from "react" /** * OwnerPanel — Kernfeature 5 (E-V3): Owner-Eingabekanaele. * 1) Ideen-Formular -> add_backlog_card (ursprung=owner) * 2) "Neuer Value Stream" -> CEO-Task (type=build, P2) * 3) CEO-Chat mit Threads (type=chat, thread_id) — Chat verdraengt nie Task-Flow. * Fail loud: Fehler des board-service/Spool als Klartext im Panel. */ type Thread = { thread_id: string topic: string messages: Array<{ task_id: string; from: string; to: string; body: string; created: number }> } export default function OwnerPanel() { const [threads, setThreads] = useState([]) const [vsList, setVsList] = useState([]) const [activeThread, setActiveThread] = useState(null) const [feedback, setFeedback] = useState<{ ok: boolean; msg: string } | null>(null) const [error, setError] = useState(null) // Idee-Formular const [ideaVs, setIdeaVs] = useState("") const [ideaTitle, setIdeaTitle] = useState("") const [ideaWert, setIdeaWert] = useState("7") const [ideaMach, setIdeaMach] = useState("7") // VS-Formular const [vsTopic, setVsTopic] = useState("") const [vsZiel, setVsZiel] = useState("") // Chat const [chatMsg, setChatMsg] = useState("") const chatBottomRef = useRef(null) const loadThreads = () => { fetch("/api/fleet/owner/chat", { cache: "no-store" }) .then(async (res) => { if (!res.ok) throw new Error((await res.json()).error || `HTTP ${res.status}`) return res.json() }) .then((d: { threads: Thread[] }) => { setThreads(d.threads) setError(null) }) .catch((e: Error) => setError(e.message)) } const loadVs = () => { fetch("/api/fleet/kanban", { cache: "no-store" }) .then(async (res) => { if (!res.ok) throw new Error((await res.json()).error || `HTTP ${res.status}`) return res.json() }) .then((d: { value_streams: string[] }) => setVsList(d.value_streams)) .catch((e: Error) => setError(e.message)) } useEffect(() => { loadThreads() loadVs() const timer = setInterval(() => { loadThreads() }, 5000) return () => clearInterval(timer) }, []) useEffect(() => { chatBottomRef.current?.scrollIntoView({ behavior: "smooth" }) }, [threads, activeThread]) const post = (url: string, payload: unknown, okMsg: (d: unknown) => string) => { setFeedback(null) fetch(url, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(payload), }) .then(async (res) => { const body = await res.json() if (!res.ok) throw new Error(body.error || `HTTP ${res.status}`) return body }) .then((d) => setFeedback({ ok: true, msg: okMsg(d) })) .catch((e: Error) => setFeedback({ ok: false, msg: e.message })) } const submitIdea = () => { if (!ideaVs || !ideaTitle.trim()) { setFeedback({ ok: false, msg: "VS und Titel sind Pflicht" }) return } const id = `owner-${Date.now().toString(36)}` post( "/api/fleet/owner/idea", { vs: ideaVs, karte_id: id, titel: ideaTitle, wert: Number(ideaWert), machbarkeit: Number(ideaMach) }, () => `Idee als Backlog-Karte '${id}' angelegt (Neu)` ) setIdeaTitle("") } const submitNewVs = () => { if (!vsTopic.trim()) { setFeedback({ ok: false, msg: "Topic ist Pflicht" }) return } post( "/api/fleet/owner/new-vs", { topic: vsTopic, ziel: vsZiel }, (d) => `VS-Gruendung als CEO-Task eingelegt (task_id ${(d as { task?: { task_id?: string } }).task?.task_id?.slice(0, 8) || "ok"})` ) setVsTopic("") setVsZiel("") } const startThread = () => { const tid = `owner-chat-${Date.now().toString(36)}` setActiveThread(tid) setThreads((prev) => [ ...prev, { thread_id: tid, topic: `Owner-Chat ${new Date().toLocaleString("de-DE")}`, messages: [] }, ]) } const sendChat = () => { if (!activeThread || !chatMsg.trim()) return post( "/api/fleet/owner/chat", { thread_id: activeThread, topic: threads.find((t) => t.thread_id === activeThread)?.topic, message: chatMsg }, () => "gesendet" ) setChatMsg("") setTimeout(loadThreads, 800) } const current = threads.find((t) => t.thread_id === activeThread) return (

OWNER KANAELE

{feedback && ( {feedback.msg} )}
{error && (

{error}

)}

Idee ins VS-Backlog

setIdeaTitle(e.target.value)} placeholder="Titel der Idee" className="w-full bg-[var(--background-tertiary)] text-xs font-mono px-2 py-1 rounded-sm" />

Neuer Value Stream

setVsTopic(e.target.value)} placeholder="Topic (z.B. 'Imkerei-Website 2.0')" className="w-full bg-[var(--background-tertiary)] text-xs font-mono px-2 py-1 rounded-sm" /> setVsZiel(e.target.value)} placeholder="Ziel (optional)" className="w-full bg-[var(--background-tertiary)] text-xs font-mono px-2 py-1 rounded-sm" />
{threads.map((t) => ( ))}
{current ? ( <>
{current.topic || current.thread_id}
{current.messages.map((m) => (
{m.from}
{m.body}
))}
setChatMsg(e.target.value)} onKeyDown={(e) => e.key === "Enter" && sendChat()} placeholder="Nachricht an CEO..." className="flex-1 bg-[var(--background-tertiary)] text-xs font-mono px-2 py-1 rounded-sm" />
) : (

Thread auswaehlen oder neu anlegen

)}
) }