Files
openshell_controller/app/components/fleet/OwnerPanel.tsx
T

311 lines
11 KiB
TypeScript

"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<Thread[]>([])
const [vsList, setVsList] = useState<string[]>([])
const [activeThread, setActiveThread] = useState<string | null>(null)
const [feedback, setFeedback] = useState<{ ok: boolean; msg: string } | null>(null)
const [error, setError] = useState<string | null>(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<HTMLDivElement>(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 (
<div className="space-y-4">
<div className="panel p-4 flex items-center gap-4">
<h1 className="text-lg font-semibold text-[var(--nvidia-green)] uppercase tracking-wider mr-auto">
OWNER KANAELE
</h1>
{feedback && (
<span
className="text-xs font-mono"
style={{ color: feedback.ok ? "var(--status-running)" : "var(--status-stopped)" }}
data-testid="fleet-owner-feedback"
>
{feedback.msg}
</span>
)}
</div>
{error && (
<div className="panel p-4 border-l-4 border-[var(--status-stopped)]">
<p className="text-xs font-mono text-[var(--status-stopped)] break-all">{error}</p>
</div>
)}
<div className="grid grid-cols-2 gap-4">
<div className="panel p-4 space-y-3" data-testid="fleet-owner-idea-form">
<h2 className="text-xs uppercase tracking-wider text-[var(--foreground-dim)]">Idee ins VS-Backlog</h2>
<select
value={ideaVs}
onChange={(e) => setIdeaVs(e.target.value)}
className="w-full bg-[var(--background-tertiary)] text-xs font-mono px-2 py-1 rounded-sm"
>
<option value="">Value Stream...</option>
{vsList.map((v) => (
<option key={v} value={v}>
{v}
</option>
))}
</select>
<input
value={ideaTitle}
onChange={(e) => 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"
/>
<div className="flex gap-2">
<label className="text-[10px] text-[var(--foreground-dim)] flex items-center gap-1">
Wert
<input
type="number"
min={1}
max={10}
value={ideaWert}
onChange={(e) => setIdeaWert(e.target.value)}
className="w-14 bg-[var(--background-tertiary)] text-xs font-mono px-2 py-1 rounded-sm"
/>
</label>
<label className="text-[10px] text-[var(--foreground-dim)] flex items-center gap-1">
Machbarkeit
<input
type="number"
min={1}
max={10}
value={ideaMach}
onChange={(e) => setIdeaMach(e.target.value)}
className="w-14 bg-[var(--background-tertiary)] text-xs font-mono px-2 py-1 rounded-sm"
/>
</label>
<button
onClick={submitIdea}
className="ml-auto px-3 py-1 text-[10px] uppercase tracking-wider rounded-sm bg-[var(--nvidia-green)] text-black font-semibold"
data-testid="fleet-owner-idea-submit"
>
Anlegen
</button>
</div>
</div>
<div className="panel p-4 space-y-3" data-testid="fleet-owner-newvs-form">
<h2 className="text-xs uppercase tracking-wider text-[var(--foreground-dim)]">Neuer Value Stream</h2>
<input
value={vsTopic}
onChange={(e) => 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"
/>
<input
value={vsZiel}
onChange={(e) => setVsZiel(e.target.value)}
placeholder="Ziel (optional)"
className="w-full bg-[var(--background-tertiary)] text-xs font-mono px-2 py-1 rounded-sm"
/>
<button
onClick={submitNewVs}
className="px-3 py-1 text-[10px] uppercase tracking-wider rounded-sm bg-[var(--nvidia-green)] text-black font-semibold"
data-testid="fleet-owner-newvs-submit"
>
CEO-Task anlegen
</button>
</div>
</div>
<div className="grid grid-cols-[280px_1fr] gap-4">
<div className="panel p-3 space-y-2" data-testid="fleet-owner-thread-list">
<button
onClick={startThread}
className="w-full px-3 py-1 text-[10px] uppercase tracking-wider rounded-sm bg-[var(--nvidia-green)] text-black font-semibold"
>
Neuer Chat-Thread
</button>
{threads.map((t) => (
<button
key={t.thread_id}
onClick={() => setActiveThread(t.thread_id)}
className={`w-full text-left px-2 py-1 rounded-sm text-xs font-mono truncate ${
activeThread === t.thread_id ? "bg-[var(--background-panel)]" : "hover:bg-[var(--background-tertiary)]"
}`}
>
{t.topic || t.thread_id}
</button>
))}
</div>
<div className="panel p-4 flex flex-col min-h-[360px]" data-testid="fleet-owner-chat-panel">
{current ? (
<>
<div className="text-[10px] uppercase tracking-wider text-[var(--foreground-dim)] mb-3">
{current.topic || current.thread_id}
</div>
<div className="flex-1 overflow-y-auto space-y-2">
{current.messages.map((m) => (
<div
key={m.task_id}
className="flex"
data-fleet-chat-message={m.from}
style={{ justifyContent: m.from === "tower-owner" ? "flex-end" : "flex-start" }}
>
<div
className="max-w-[70%] p-2 rounded-sm text-xs font-mono"
style={{
background:
m.from === "tower-owner" ? "var(--background-panel)" : "var(--background-tertiary)",
}}
>
<div className="text-[9px] text-[var(--foreground-dim)] mb-1">{m.from}</div>
{m.body}
</div>
</div>
))}
<div ref={chatBottomRef} />
</div>
<div className="flex gap-2 mt-3">
<input
value={chatMsg}
onChange={(e) => 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"
/>
<button
onClick={sendChat}
className="px-3 py-1 text-[10px] uppercase tracking-wider rounded-sm bg-[var(--nvidia-green)] text-black font-semibold"
data-testid="fleet-owner-chat-send"
>
Senden
</button>
</div>
</>
) : (
<p className="text-xs text-[var(--foreground-dim)] font-mono m-auto">
Thread auswaehlen oder neu anlegen
</p>
)}
</div>
</div>
</div>
)
}