import { useState, FormEvent, useRef } from 'react'; import { Link } from 'react-router-dom'; import { useGetDocuments, useCreateDocument } from '../generated/anthoLumeAPIV1'; interface DocumentCardProps { doc: { id: string; title: string; author: string; created_at: string; deleted: boolean; words?: number; filepath?: string; percentage?: number; total_time_seconds?: number; }; } // Activity icon SVG function ActivityIcon() { return ( ); } // Download icon SVG function DownloadIcon({ disabled }: { disabled?: boolean }) { if (disabled) { return ( ); } return ( ); } function DocumentCard({ doc }: DocumentCardProps) { const percentage = doc.percentage || 0; const totalTimeSeconds = doc.total_time_seconds || 0; // Convert seconds to nice format (e.g., "2h 30m") const niceSeconds = (seconds: number): string => { const hours = Math.floor(seconds / 3600); const minutes = Math.floor((seconds % 3600) / 60); if (hours > 0) { return `${hours}h ${minutes}m`; } return `${minutes}m`; }; return (
{doc.title}

Title

{doc.title || "Unknown"}

Author

{doc.author || "Unknown"}

Progress

{percentage}%

Time Read

{niceSeconds(totalTimeSeconds)}

{doc.filepath ? ( ) : ( )}
); } // Search icon SVG function SearchIcon() { return ( ); } // Upload icon SVG function UploadIcon() { return ( ); } export default function DocumentsPage() { const [search, setSearch] = useState(''); const [page, setPage] = useState(1); const [limit] = useState(9); const [uploadMode, setUploadMode] = useState(false); const fileInputRef = useRef(null); const { data, isLoading, refetch } = useGetDocuments({ page, limit, search }); const createMutation = useCreateDocument(); const docs = data?.data?.documents; const previousPage = data?.data?.previous_page; const nextPage = data?.data?.next_page; const handleSubmit = (e: FormEvent) => { e.preventDefault(); refetch(); }; const handleFileChange = async (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (!file) return; if (!file.name.endsWith('.epub')) { alert('Please upload an EPUB file'); return; } try { await createMutation.mutateAsync({ data: { document_file: file, }, }); alert('Document uploaded successfully!'); setUploadMode(false); refetch(); } catch (error) { console.error('Upload failed:', error); alert('Failed to upload document'); } }; const handleCancelUpload = () => { setUploadMode(false); if (fileInputRef.current) { fileInputRef.current.value = ''; } }; if (isLoading) { return
Loading...
; } return (
{/* Search Form */}
setSearch(e.target.value)} className="flex-1 appearance-none rounded-none border border-gray-300 w-full py-2 px-2 bg-white text-gray-700 placeholder-gray-400 shadow-sm text-base focus:outline-none focus:ring-2 focus:ring-purple-600 focus:border-transparent" placeholder="Search Author / Title" name="search" />
{/* Document Grid */}
{docs?.map((doc: any) => ( ))}
{/* Pagination */}
{previousPage && previousPage > 0 && ( )} {nextPage && nextPage > 0 && ( )}
{/* Upload Button */}
setUploadMode(!uploadMode)} />
); }