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 (
Title
{doc.title || 'Unknown'}
Author
{doc.author || 'Unknown'}
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 */}
{/* Document Grid */}
{docs?.map((doc: any) => (
))}
{/* Pagination */}
{previousPage && previousPage > 0 && (
)}
{nextPage && nextPage > 0 && (
)}
{/* Upload Button */}
);
}