import React, { useState, useEffect } from 'react'; import { Button } from './ui/button'; import axios from 'axios'; // Fallback data in case of API failure const fallbackData = { keyTopics: ['Document analysis unavailable'], entities: ['Try refreshing the page'], wordCloudData: [ { text: 'Document', value: 50 }, { text: 'Summary', value: 40 }, { text: 'Unavailable', value: 30 }, ], documentStructure: [ { title: 'Document structure unavailable', subsections: ['Please refresh the page or try a different document'] } ] }; const DocumentSummary = ({ fileName, sessionId, userId }) => { const [activeTab, setActiveTab] = useState('overview'); const [summaryData, setSummaryData] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { const fetchSummary = async () => { if (!sessionId) return; try { setLoading(true); setError(''); // Add user ID to the request if available const response = await axios.post('/document-summary', { session_id: sessionId, user_id: userId }); setSummaryData(response.data); setLoading(false); } catch (error) { console.error('Error fetching document summary:', error); setError('Failed to load document summary'); setLoading(false); } }; fetchSummary(); }, [sessionId, userId]); if (loading) { return (

📊 Document Summary Dashboard

Generating document insights...
); } if (error && !summaryData) { return (

📊 Document Summary Dashboard

⚠️ {error}
); } if (!summaryData) { return (

📊 Document Summary Dashboard

No document summary available
); } return (

📊 Document Summary Dashboard

{activeTab === 'overview' && (

Key Topics

{summaryData.keyTopics.map((topic, idx) => ( {topic} ))}

Key Entities

{summaryData.entities.map((entity, idx) => ( {entity} ))}
)} {activeTab === 'wordcloud' && (
{summaryData.wordCloudData.map((word, idx) => { // Calculate font size based on value (scale from 14 to 36) const fontSize = 14 + (word.value - 16) * (22 / 48); const opacity = 0.5 + (word.value / 64) * 0.5; return ( {word.text} ); })}

Word size represents frequency in the document. Click on any word to search for it.

)} {activeTab === 'structure' && (
    {summaryData.documentStructure.map((section, idx) => (
  • {section.title}
    {section.subsections.length > 0 && (
      {section.subsections.map((subsection, subIdx) => (
    • {subsection}
    • ))}
    )}
  • ))}

Document structure extracted from headings and content organization.

)}
); }; export default DocumentSummary;