import React, { useState, useEffect } from 'react'; import ReactMarkdown from 'react-markdown'; import { X, Download, Loader, AlertCircle, FileText, File } from 'lucide-react'; const API_BASE = process.env.REACT_APP_API_BASE || 'http://localhost:3001/api'; export default function KnowledgeBaseViewer({ article, onClose }) { const [content, setContent] = useState(''); const [loading, setLoading] = useState(true); const [error, setError] = useState(''); useEffect(() => { fetchArticleContent(); // eslint-disable-next-line react-hooks/exhaustive-deps }, [article.id]); const fetchArticleContent = async () => { setLoading(true); setError(''); try { const response = await fetch(`${API_BASE}/knowledge-base/${article.id}/content`, { credentials: 'include' }); if (!response.ok) throw new Error('Failed to fetch article content'); const text = await response.text(); setContent(text); } catch (err) { console.error('Error fetching article content:', err); setError(err.message); } finally { setLoading(false); } }; const handleDownload = async () => { try { const response = await fetch(`${API_BASE}/knowledge-base/${article.id}/download`, { credentials: 'include' }); if (!response.ok) throw new Error('Download failed'); const blob = await response.blob(); const url = window.URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = article.file_name; document.body.appendChild(a); a.click(); window.URL.revokeObjectURL(url); document.body.removeChild(a); } catch (err) { console.error('Error downloading file:', err); setError('Failed to download file'); } }; const isMarkdown = article.file_name?.endsWith('.md'); const isText = article.file_name?.endsWith('.txt'); const isPDF = article.file_name?.endsWith('.pdf'); const isImage = /\.(jpg|jpeg|png|gif|bmp)$/i.test(article.file_name || ''); const getCategoryColor = (cat) => { const colors = { 'General': '#94A3B8', 'Policy': '#0EA5E9', 'Procedure': '#10B981', 'Guide': '#F59E0B', 'Reference': '#8B5CF6' }; return colors[cat] || '#94A3B8'; }; const formatDate = (dateString) => { return new Date(dateString).toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' }); }; return (
{article.description}
)}Loading document...
Failed to Load Document
{error}
{content}
)}
{/* PDF */}
{isPDF && (
Preview not available for this file type.