All files / apps/web/src/frontoffice/components FactsViewerModal.jsx

0% Statements 0/29
0% Branches 0/27
0% Functions 0/8
0% Lines 0/23

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                                                                                                                                                                                                                 
import { useState, useEffect } from 'react'
import { Brain, Copy, X } from 'lucide-react'
import toast from 'react-hot-toast'
 
function FactsViewerModal({ isOpen, onClose, content, title }) {
  const [facts, setFacts] = useState([])
  
  useEffect(() => {
    if (!isOpen) return
    
    const handleEscape = (e) => {
      if (e.key === 'Escape') onClose()
    }
    window.addEventListener('keydown', handleEscape)
    return () => window.removeEventListener('keydown', handleEscape)
  }, [isOpen, onClose])
  
  useEffect(() => {
    if (!content || !isOpen) return
    
    try {
      const data = JSON.parse(content)
      const factsList = data.facts || data.data || []
      // En mode data, data.data peut être un objet (ex: { services: [...], title }).
      // On l'affiche comme une entrée unique pour éviter un crash sur .map.
      setFacts(Array.isArray(factsList) ? factsList : [factsList])
    } catch (e) {
      console.error('Failed to parse facts:', e)
      setFacts([])
    }
  }, [content, isOpen])
  
  if (!isOpen || !content) return null
  
  const copyAllFacts = () => {
    const text = facts.map(f => typeof f === 'string' ? f : JSON.stringify(f)).join('\n')
    navigator.clipboard.writeText(text)
    toast.success('Faits copiés !')
  }
  
  return (
    <div className="fixed inset-0 z-50 flex items-center justify-center bg-black/60 backdrop-blur-sm p-4">
      <div className="bg-white rounded-xl shadow-2xl max-w-3xl w-full max-h-[85vh] overflow-hidden flex flex-col">
        {/* Header */}
        <div className="flex items-center justify-between p-4 border-b">
          <div className="flex items-center gap-3">
            <Brain className="w-5 h-5 text-gray-600" />
            <div>
              <h3 className="text-lg font-semibold text-gray-900">{title || 'Faits'}</h3>
              <p className="text-sm text-gray-500">
                {facts.length} fait{facts.length !== 1 ? 's' : ''}
              </p>
            </div>
          </div>
          <div className="flex items-center gap-2">
            <button 
              onClick={copyAllFacts}
              className="px-3 py-2 text-sm text-gray-600 hover:bg-gray-100 rounded-lg transition-colors flex items-center gap-1"
            >
              <Copy className="w-4 h-4" />
              Copier
            </button>
            <button 
              onClick={onClose}
              className="p-2 hover:bg-gray-100 rounded-full transition-colors"
            >
              <X className="w-5 h-5 text-gray-500" />
            </button>
          </div>
        </div>
        
        {/* Facts List */}
        <div className="flex-1 overflow-auto p-6">
          {facts.length === 0 ? (
            <div className="text-center text-gray-500 py-8">
              Aucun fait à afficher
            </div>
          ) : (
            <ul className="space-y-2">
              {facts.map((fact, index) => {
                const factText = typeof fact === 'string' ? fact : JSON.stringify(fact)
                return (
                  <li 
                    key={index}
                    className="text-gray-700 py-2 border-b border-gray-100 last:border-0"
                  >
                    {factText}
                  </li>
                )
              })}
            </ul>
          )}
        </div>
        
        {/* Footer */}
        <div className="p-3 bg-gray-50 border-t text-center text-sm text-gray-500">
          Appuyez sur Echap pour fermer
        </div>
      </div>
    </div>
  )
}
 
export default FactsViewerModal