import { useState, useEffect } from 'react'; import { useAuth } from '../../hooks/useAuth'; import { getSharedDocuments } from '../../services/api'; import { format } from 'date-fns'; import { FiFolder, FiFile, FiDownload, FiChevronRight, FiLoader } from 'react-icons/fi'; import { downloadDocument } from '../../services/api'; function Dashboard() { const [documents, setDocuments] = useState({}); const [expandedFolders, setExpandedFolders] = useState({}); const [loading, setLoading] = useState(true); const [error, setError] = useState(''); const { user } = useAuth(); useEffect(() => { const fetchDocuments = async () => { try { if (!user?.id) return; const response = await getSharedDocuments(user.id); // Group the documents by company and date const groupedDocs = groupDocumentsByCompanyAndDate(response.data); setDocuments(groupedDocs); } catch (err) { console.error('Error fetching documents:', err); setError('Failed to fetch documents'); } finally { setLoading(false); } }; fetchDocuments(); }, [user]); const groupDocumentsByCompanyAndDate = (docs) => { if (!Array.isArray(docs)) { console.error('Expected array of documents, received:', docs); return {}; } return docs.reduce((acc, doc) => { const folderName = `${doc.sharedWith?.name || 'Unknown'}-${format(new Date(doc.createdAt), 'yyyy-MM-dd')}`; if (!acc[folderName]) { acc[folderName] = []; } acc[folderName].push(doc); return acc; }, {}); }; const handleDownload = async (s3Key, fileName) => { try { await downloadDocument(s3Key); } catch (err) { console.error('Error downloading document:', err); alert('Failed to download document. Please try again.'); } }; const toggleFolder = (folderName) => { setExpandedFolders(prev => ({ ...prev, [folderName]: !prev[folderName] })); }; if (loading) { return (
Loading documents...
); } if (error) { return (
{error}
); } return (

Your Documents

Access and manage your shared documents

{Object.entries(documents).length > 0 ? ( Object.entries(documents).map(([folderName, docs]) => (
{expandedFolders[folderName] && (
{docs.map((doc) => (
{doc.title}
))}
)}
)) ) : (

No documents available

)}
); } export default Dashboard;