All files / apps/web/src/components SynthesisResults.jsx

0% Statements 0/61
0% Branches 0/63
0% Functions 0/22
0% Lines 0/50

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
import { 
  CheckCircle, 
  AlertCircle, 
  Loader2, 
  Clock,
  Eye,
  Copy,
  Download,
  Globe,
  FileType,
  Type,
  Brain,
  Route
} from 'lucide-react'
import toast from 'react-hot-toast'
 
function ProgressBar({ step, progress, message, status }) {
  const isProcessing = status === 'extracting' || status === 'processing' || status === 'running' || status === 'pending'
  if (!isProcessing && progress == null) return null
  
  const displayProgress = progress ?? 0
  const stepLabels = {
    received: 'Reçu',
    extracting_web: 'Extraction web',
    retrieving_web: 'Récupération contenu',
    downloading_file: 'Téléchargement',
    downloading_minio: 'Récupération fichier',
    extracting_ocr: 'Extraction OCR',
    uploading_extraction: 'Upload résultat',
    preparing_synthesis: 'Préparation',
    synthesizing: 'Synthèse IA',
    uploading_result: 'Upload résultat',
    routing_facts: 'Routage faits',
    complete: 'Terminé',
    failed: 'Échec',
  }
  
  return (
    <div className="mb-4">
      <div className="flex justify-between text-sm mb-1">
        <span>{stepLabels[step] || message || 'En cours...'}</span>
        <span>{Math.round(displayProgress)}%</span>
      </div>
      <div className="w-full bg-gray-200 rounded-full h-2.5 overflow-hidden">
        <div 
          className="bg-purple-500 h-2.5 rounded-full transition-all duration-300 ease-out"
          style={{ width: `${displayProgress}%` }}
        />
      </div>
    </div>
  )
}
 
export default function SynthesisResults({ 
  syntheses,
  onViewExtraction,
  onViewSynthesis,
  onViewRouting,
  onViewPreview,
  onCopy,
  onDownloadExtraction,
  onDownloadSynthesis
}) {
  const getStatusColor = (status) => {
    switch (status) {
      case 'completed': return 'border-green-500'
      case 'failed': return 'border-red-500'
      case 'running':
      case 'processing': return 'border-purple-500'
      default: return 'border-gray-300'
    }
  }
 
  const getStatusIcon = (status) => {
    switch (status) {
      case 'completed': return <CheckCircle className="w-5 h-5 text-green-500" />
      case 'failed': return <AlertCircle className="w-5 h-5 text-red-500" />
      case 'running':
      case 'processing': return <Loader2 className="w-5 h-5 text-purple-500 animate-spin" />
      default: return <Clock className="w-5 h-5 text-gray-400" />
    }
  }
 
  const getTypeIcon = (type) => {
    if (type === 'file' || type === 'fileUrl' || type === 'fileUpload') return <FileType className="w-4 h-4" />
    if (type === 'input') return <Type className="w-4 h-4" />
    return <Globe className="w-4 h-4" />
  }
 
  const getTypeLabel = (type) => {
    if (type === 'file' || type === 'fileUrl' || type === 'fileUpload') return 'Fichier'
    if (type === 'input') return 'Texte'
    return 'Web'
  }
 
  const hasPreview = (synthesis) => {
    const outputs = synthesis.outputs || []
    return outputs.some(o => o.type === 'page-image')
  }
 
  const getOutput = (synthesis, type) => {
    return synthesis.outputs?.find(o => o.type === type)
  }
 
  if (!syntheses || syntheses.length === 0) return null
 
  return (
    <div className="mt-8 space-y-4">
      <h2 className="text-xl font-semibold text-gray-900 mb-4">
        Extractions en cours et récentes
      </h2>
      
      {syntheses.slice().reverse().map((synthesis) => (
        <div key={synthesis.id} className={`card border-l-4 ${getStatusColor(synthesis.status)}`}>
          <div className="flex items-center justify-between mb-4">
            <div className="flex items-center">
              {getStatusIcon(synthesis.status)}
              <span className="ml-2 px-2 py-1 text-xs rounded-full bg-gray-100 text-gray-600 flex items-center gap-1">
                {getTypeIcon(synthesis.type)}
                {getTypeLabel(synthesis.type)}
              </span>
              <div className="ml-3">
                <h3 className="text-lg font-semibold truncate max-w-md">
                  {synthesis.workflowName || 'Extraction'}
                </h3>
                <p className="text-sm opacity-75">
                  {synthesis.type === 'file' 
                    ? synthesis.file?.name 
                    : synthesis.type === 'input' 
                      ? synthesis.input?.substring(0, 100) + '...' 
                      : synthesis.url}
                </p>
              </div>
            </div>
          </div>
 
          <ProgressBar 
            step={synthesis.step} 
            progress={synthesis.progress} 
            message={synthesis.message}
            status={synthesis.status}
          />
 
          {synthesis.status === 'completed' && (
            <>
              {/* Extraction Results - only show if extraction output exists with valid URL */}
              {(() => {
                const extractionOutput = getOutput(synthesis, 'extraction')
                if (!extractionOutput?.url) return null
                return (
                <div className="mb-4">
                  <div className="text-xs font-medium text-gray-500 uppercase tracking-wide mb-2">
                    Extraction
                  </div>
                  <div className="flex flex-wrap gap-2">
                    <button 
                      onClick={() => onViewExtraction?.(synthesis)} 
                      className="inline-flex items-center px-3 py-2 bg-blue-50 hover:bg-blue-100 border border-blue-200 rounded-lg text-sm"
                    >
                      <Eye className="w-4 h-4 mr-2" />Voir extraction
                    </button>
                    
                    {hasPreview(synthesis) && (
                      <button 
                        onClick={() => onViewPreview?.(synthesis)} 
                        className="inline-flex items-center px-3 py-2 bg-orange-50 hover:bg-orange-100 border border-orange-200 rounded-lg text-sm"
                      >
                        <svg className="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                          <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z" />
                        </svg>
                        Aperçu
                      </button>
                    )}
                    
                    <button 
                      onClick={() => onCopy?.(synthesis, 'extraction')} 
                      className="inline-flex items-center px-3 py-2 bg-gray-50 hover:bg-gray-100 border border-gray-200 rounded-lg text-sm"
                    >
                      <Copy className="w-4 h-4 mr-2" />Copier
                    </button>
                    
                    <button 
                      onClick={() => onDownloadExtraction?.(synthesis)} 
                      className="inline-flex items-center px-3 py-2 bg-gray-50 hover:bg-gray-100 border border-gray-200 rounded-lg text-sm"
                    >
                      <Download className="w-4 h-4 mr-2" />Télécharger .md
                    </button>
                   </div>
                 </div>
               )
             })()}
              
              {/* Synthesis Results - only show if synthesis output exists with valid URL */}
              {(() => {
                const synthesisOutput = getOutput(synthesis, 'facts') || getOutput(synthesis, 'data')
                if (!synthesisOutput?.url) return null
                return (
                  <div className="mb-4">
                    <div className="text-xs font-medium text-purple-600 uppercase tracking-wide mb-2">
                      Synthèse
                    </div>
                    <div className="flex flex-wrap gap-2">
                      <button 
                        onClick={() => onViewSynthesis?.(synthesis)} 
                        className="inline-flex items-center px-3 py-2 bg-purple-600 hover:bg-purple-700 text-white rounded-lg text-sm"
                      >
                        <Brain className="w-4 h-4 mr-2" />Voir synthèse
                      </button>
                      
                      <button 
                        onClick={() => onCopy?.(synthesis, 'synthesis')} 
                        className="inline-flex items-center px-3 py-2 bg-gray-50 hover:bg-gray-100 border border-gray-200 rounded-lg text-sm"
                      >
                        <Copy className="w-4 h-4 mr-2" />Copier
                      </button>
                      
                      <button 
                        onClick={() => onDownloadSynthesis?.(synthesis)} 
                        className="inline-flex items-center px-3 py-2 bg-gray-50 hover:bg-gray-100 border border-gray-200 rounded-lg text-sm"
                      >
                        <Download className="w-4 h-4 mr-2" />Télécharger .json
                      </button>
                    </div>
                  </div>
                )
              })()}
              
              {/* Routing Results */}
              {(() => {
                const routingOutput = getOutput(synthesis, 'routing')
                if (!routingOutput) return null
                return (
                  <div className="mb-4">
                    <div className="text-xs font-medium text-gray-500 uppercase tracking-wide mb-2">
                      Routage
                    </div>
                    <div className="flex flex-wrap gap-2">
                      <button 
                        onClick={() => onViewRouting?.(synthesis)} 
                        className="inline-flex items-center px-3 py-2 bg-gray-50 hover:bg-gray-100 border border-gray-200 rounded-lg text-sm"
                      >
                        <Route className="w-4 h-4 mr-2" />Voir routage
                      </button>
                    </div>
                  </div>
                )
              })()}
            </>
          )}
 
          {synthesis.status === 'failed' && (
            <div className="flex items-center justify-between">
              <span className="text-red-600 text-sm">{synthesis.error}</span>
            </div>
          )}
        </div>
      ))}
    </div>
  )
}