import { useState, FormEvent, useRef } from 'react'; import { Link } from 'react-router-dom'; import { useGetDocuments, useCreateDocument } from '../generated/anthoLumeAPIV1'; import { Activity, Download, Search, Upload } from 'lucide-react'; import { Button } from '../components/Button'; import { useToasts } from '../components/ToastContext'; interface DocumentCardProps { doc: { id: string; title: string; author: string; created_at: string; deleted: boolean; words?: number; filepath?: string; percentage?: number; total_time_seconds?: number; }; } 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 ? ( ) : ( )}
); } 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 { showInfo, showWarning, showError } = useToasts(); 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')) { showWarning('Please upload an EPUB file'); return; } try { await createMutation.mutateAsync({ data: { document_file: file, }, }); showInfo('Document uploaded successfully!'); setUploadMode(false); refetch(); } catch (error: any) { showError('Failed to upload document: ' + error.message); } }; const handleCancelUpload = () => { setUploadMode(false); if (fileInputRef.current) { fileInputRef.current.value = ''; } }; if (isLoading) { return
Loading...
; } return (
{/* Search Form */}
setSearch(e.target.value)} className="w-full flex-1 appearance-none rounded-none border border-gray-300 bg-white p-2 text-base text-gray-700 shadow-sm placeholder:text-gray-400 focus:border-transparent focus:outline-none focus:ring-2 focus:ring-purple-600" placeholder="Search Author / Title" name="search" />
{/* Document Grid */}
{docs?.map((doc: any) => ( ))}
{/* Pagination */}
{previousPage && previousPage > 0 && ( )} {nextPage && nextPage > 0 && ( )}
{/* Upload Button */}
setUploadMode(!uploadMode)} />
); }