import React, { useState } from 'react'; import { AlertCircle, Clock, Database, FileX, Loader2, CheckCircle2, Hash, Copy, Download, Check } from 'lucide-react'; const MAX_DISPLAY_ROWS = 1000; export default function ResultTable({ result, loading, pollingProgress, onClear }) { const [copied, setCopied] = useState(false); // Normalize data let columns = []; let rows = []; if (result && !result.error) { if (result.rows && result.columns) { columns = result.columns; rows = result.rows; } else if (Array.isArray(result.data) && result.data.length > 0) { columns = Object.keys(result.data[0]); rows = result.data; } else if (Array.isArray(result) && result.length > 0) { columns = Object.keys(result[0]); rows = result; } } // Function to convert data to CSV const generateCSV = (cols, dataRows) => { const header = cols.join(','); const csvRows = dataRows.map(row => { return cols.map(col => { let val = Array.isArray(row) ? row[cols.indexOf(col)] : row[col]; if (val === null || val === undefined) val = ''; val = String(val).replace(/"/g, '""'); if (val.includes(',') || val.includes('"') || val.includes('\n')) { val = `"${val}"`; } return val; }).join(','); }); return [header, ...csvRows].join('\n'); }; // Download file const handleDownload = (content, filename, type = 'text/csv') => { const blob = new Blob([content], { type: `${type};charset=utf-8;` }); const url = URL.createObjectURL(blob); const link = document.createElement('a'); link.setAttribute('href', url); link.setAttribute('download', filename); document.body.appendChild(link); link.click(); document.body.removeChild(link); URL.revokeObjectURL(url); }; // Loading state if (loading) { return (

{pollingProgress ? 'Esperando respuesta del agente remoto...' : 'Enviando comando...'}

{pollingProgress && (

Polling: {pollingProgress.attempts} / {pollingProgress.maxAttempts}

)}
); } // No result yet if (!result) { return (

Los resultados aparecerán aquí

); } // Error state if (result.error) { return (

Error en la ejecución

{result.error} {result.command_id && (

Command ID: #{result.command_id}

)}
); } // Success message (no data rows) if (rows.length === 0 && !result.error) { if (result.command_id || result.message) { return (

{result.message || 'Consulta ejecutada correctamente'}

{result.downloadUrl && (
Descargar Respaldo Ahora

El archivo se encuentra disponible en el servidor FTP especificado.

)} {result.command_id && !result.downloadUrl && (

Command ID: {result.command_id}

)}
); } return (

La consulta no devolvió resultados.

); } // Large dataset display if (rows.length > MAX_DISPLAY_ROWS) { return (

Resultado muy grande ({rows.length} filas)

Para mantener el rendimiento del navegador, los resultados que superan las {MAX_DISPLAY_ROWS} filas no se muestran en pantalla y se descargan automáticamente como CSV.

{result.command_id && (

Command ID: #{result.command_id}

)}
); } // Final safety check for file content const isFileContent = rows.length === 1 && columns.length === 1 && ( columns[0]?.toUpperCase() === 'CONTENT' || columns[0]?.toUpperCase() === 'TEXT' || columns[0]?.toUpperCase() === 'FILE' || (result?.command_id && String(result.command_id).startsWith('FILE-')) ); if (isFileContent) { return (

¡Archivo procesado con éxito!

El contenido del archivo #{result.command_id} ha sido recibido y la descarga debería haber iniciado automáticamente.

); } // Data table display - STABLE standard view return (
{/* Stats Header */}
{result.command_id && ( {result.command_id} )} {result.stats?.row_count !== undefined && ( {result.stats.row_count} filas )} {result.stats?.duration && ( {result.stats.duration} )}
{/* Standard Table View */}
{columns.map((col, idx) => ( ))} {rows.map((row, rIdx) => ( {columns.map((col, cIdx) => { let val = Array.isArray(row) ? row[cIdx] : row[col]; return ( ); })} ))}
#{col}
{rIdx + 1} {val === null ? ( NULL ) : typeof val === 'object' ? ( {JSON.stringify(val)} ) : ( String(val).length > 200 ? String(val).substring(0, 200) + '...' : String(val) )}
); }