import { useState, useEffect, useRef } from 'react'; import { Send } from 'lucide-react'; import axios from 'axios'; import { API_URL, WS_URL } from '../config'; export default function Console({ serverName, token }) { const [logs, setLogs] = useState([]); const [command, setCommand] = useState(''); const logsEndRef = useRef(null); const wsRef = useRef(null); useEffect(() => { setLogs([]); const ws = new WebSocket(`${WS_URL}/ws/servers/${serverName}/console`); ws.onopen = () => { console.log('WebSocket подключен'); }; ws.onmessage = (event) => { setLogs((prev) => [...prev, event.data]); }; ws.onerror = (error) => { console.error('WebSocket ошибка:', error); }; wsRef.current = ws; return () => { ws.close(); }; }, [serverName]); useEffect(() => { logsEndRef.current?.scrollIntoView({ behavior: 'smooth' }); }, [logs]); const sendCommand = async (e) => { e.preventDefault(); if (!command.trim()) return; try { await axios.post( `${API_URL}/api/servers/${serverName}/command`, { command: command.trim() }, { headers: { Authorization: `Bearer ${token}` } } ); setCommand(''); } catch (error) { console.error('Ошибка отправки команды:', error); alert(error.response?.data?.detail || 'Ошибка отправки команды'); } }; // Функция для раскраски логов const colorizeLog = (log) => { // INFO - зеленый if (log.includes('[INFO]') || log.includes('Done (')) { return {log}; } // WARN - желтый if (log.includes('[WARN]') || log.includes('WARNING')) { return {log}; } // ERROR - красный if (log.includes('[ERROR]') || log.includes('Exception')) { return {log}; } // Время - серый if (log.match(/^\[\d{2}:\d{2}:\d{2}\]/)) { const time = log.match(/^\[\d{2}:\d{2}:\d{2}\]/)[0]; const rest = log.substring(time.length); return ( <> {time} {rest} > ); } // Обычный текст return {log}; }; return (