import React, { useState, useRef, useEffect } from 'react'; export default function TerminalPanel() { const [history, setHistory] = useState([ { type: 'system', text: '⚡ Interactive Terminal Session Connected' }, { type: 'system', text: 'Type `help` or execute shell commands directly.' } ]); const [input, setInput] = useState(''); const [executing, setExecuting] = useState(false); const terminalEndRef = useRef(null); useEffect(() => { terminalEndRef.current?.scrollIntoView({ behavior: 'smooth' }); }, [history]); const handleCommandSubmit = async (e) => { e.preventDefault(); const cmd = input.trim(); if (!cmd || executing) return; setHistory((prev) => [...prev, { type: 'input', text: `$ ${cmd}` }]); setInput(''); setExecuting(true); try { const response = await fetch('/api/terminal', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ command: cmd }) }); if (!response.ok) { throw new Error(`HTTP error status: ${response.status}`); } const data = await response.json(); const outputText = data.output || data.result || 'Command executed with no output.'; setHistory((prev) => [...prev, { type: 'output', text: outputText }]); } catch (err) { setHistory((prev) => [ ...prev, { type: 'error', text: `Command execution failed: ${err.message}` } ]); } finally { setExecuting(false); } }; return (