'use client'; 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 (
{/* Terminal Bar Header */}
Terminal
{/* Terminal History Display */}
{history.map((item, index) => (
{item.type === 'input' && {item.text}} {item.type === 'output' && {item.text}} {item.type === 'system' && {item.text}} {item.type === 'error' && {item.text}}
))} {executing &&
Executing command...
}
{/* Command Line Input Prompt */}
$ setInput(e.target.value)} disabled={executing} placeholder="Run command..." className="flex-1 bg-transparent text-white outline-none font-mono text-xs" />
); }