import React from "react"; import { useEffect, useRef, useState } from "react"; export default function AIAssistantDrawer({ open, onClose, computers, scope, setScope }) { const [messages, setMessages] = useState([ { role: "assistant", text: "Постави прашање за Sysmon, процеси, мрежа. Можеш да избереш компјутер (scope) горе." }, ]); const [input, setInput] = useState(""); const [sending, setSending] = useState(false); const bottomRef = useRef(null); useEffect(() => { if (bottomRef.current) bottomRef.current.scrollIntoView({ behavior: "smooth" }); }, [messages, open]); async function send() { const q = input.trim(); if (!q || sending) return; setInput(""); setMessages((m) => [...m, { role: "user", text: q }]); setSending(true); try { const r = await fetch("/api/chat", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ question: q, computer_name: scope || null }), }); const data = await r.json(); setMessages((m) => [...m, { role: "assistant", text: data.answer || "(no answer)" }]); } catch (e) { setMessages((m) => [...m, { role: "assistant", text: `Грешка: ${String(e.message || e)}` }]); } finally { setSending(false); } } if (!open) return null; return ( <>