Initial commit

This commit is contained in:
Orionis312050 committed 2026-04-16 14:18:56 +02:00
commit e6e07dc5c2
30 files changed
+5948

No files matched your search

+144
View File
@@ -0,0 +1,144 @@
import React, { useEffect, useState, useRef } from 'react';
import { io, Socket } from 'socket.io-client';
import { Download, Sparkles } from 'lucide-react';
import type { LogService } from '../types';
export default function LogTerminal({ service, initialLogs = [] }: {
service: LogService,
initialLogs?: { id: string, line: string, isError: boolean }[]
}) {
const [logs, setLogs] = useState<{ id: string, line: string, isError: boolean }[]>(initialLogs);
const [isPaused, setIsPaused] = useState(false);
const isPausedRef = useRef(false); // Ref pour éviter les fermetures (closures) obsolètes
const pendingLogsRef = useRef<{ id: string, line: string, isError: boolean }[]>([]);
const socketRef = useRef<Socket | null>(null);
// Synchronisation de la ref avec l'état
useEffect(() => {
isPausedRef.current = isPaused;
}, [isPaused]);
useEffect(() => {
if (!socketRef.current) {
socketRef.current = io('http://localhost:3001', {
withCredentials: true
});
}
// On ne vide les logs que si le service change, et on injecte les logs initiaux
setLogs(initialLogs);
pendingLogsRef.current = [];
const handleLogLine = (data: { serviceId: number, line: string }) => {
if (data.serviceId === service.id) {
const isError = service.alertKeyword
? data.line.includes(service.alertKeyword)
: (data.line.includes('ERROR') || data.line.includes('Exception'));
const newLog = { id: Date.now() + Math.random().toString(), line: data.line, isError };
if (!isPausedRef.current) {
setLogs(prev => [...prev, newLog].slice(-300));
} else {
pendingLogsRef.current = [...pendingLogsRef.current, newLog].slice(-300);
}
}
};
socketRef.current.on('logLine', handleLogLine);
return () => {
socketRef.current?.off('logLine', handleLogLine);
};
}, [service.id]); // DÉPENDANCE UNIQUEMENT SUR L'ID DU SERVICE
const terminalContentRef = useRef<HTMLDivElement>(null);
const togglePause = () => {
if (isPaused) {
setLogs(prev => [...prev, ...pendingLogsRef.current].slice(-300));
pendingLogsRef.current = [];
}
setIsPaused(!isPaused);
};
useEffect(() => {
if (!isPaused && terminalContentRef.current) {
// Défilement interne du conteneur sans affecter la page entière
terminalContentRef.current.scrollTop = terminalContentRef.current.scrollHeight;
}
}, [logs, isPaused]);
const handleExport = () => {
if (logs.length === 0) return;
// Header CSV
const csvContent = [
"ID,Status,Log Line",
...logs.map(log => `${log.id},${log.isError ? 'ERROR' : 'INFO'},"${log.line.replace(/"/g, '""')}"`)
].join("\n");
const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.setAttribute("href", url);
link.setAttribute("download", `logs_${service.name}_${new Date().toISOString().slice(0,10)}.csv`);
link.style.visibility = 'hidden';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
};
const handleAnalyzeLog = (line: string) => {
// Émission d'un événement global pour la sidebar
window.dispatchEvent(new CustomEvent('analyze-log', {
detail: { line, serviceName: service.name }
}));
};
return (
<div className={`terminal-container ${isPaused ? 'paused' : 'live'}`}>
<div className="terminal-controls">
<div style={{ display: 'flex', gap: '0.75rem' }}>
<button className={`btn-terminal ${isPaused ? 'btn-resume' : 'btn-pause'}`} onClick={togglePause}>
{isPaused ? '▶ Reprendre' : '⏸ Pauser'}
{isPaused && pendingLogsRef.current.length > 0 && (
<span className="pending-badge">{pendingLogsRef.current.length}</span>
)}
</button>
<button className="btn-terminal btn-outline-sm" onClick={handleExport} title="Exporter en CSV">
<Download size={14} /> Exporter
</button>
</div>
<span className="live-indicator">
<span className={`status-dot ${isPaused ? 'dot-red' : 'dot-green'}`}></span>
{isPaused ? 'TERMINAL EN PAUSE' : 'LOGS EN DIRECT'}
</span>
</div>
<div className="terminal-content" ref={terminalContentRef}>
{logs.length === 0 ? (
<div style={{ color: 'var(--text-muted)', display: 'flex', height: '100%', alignItems: 'center', justifyContent: 'center' }}>
En attente de nouveaux logs pour '{service.name}'...
</div>
) : (
logs.map(log => (
<div key={log.id} className="log-line-wrapper">
<div className="log-line">
<span className={`log-text ${log.isError ? 'log-error' : ''}`}>
{log.line}
</span>
</div>
<button
className="btn-analyze-log"
onClick={() => handleAnalyzeLog(log.line)}
title="Analyser avec l'IA"
>
<Sparkles size={12} />
</button>
</div>
))
)}
</div>
</div>
);
}