import { useState } from "react" import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query" import { api } from "../lib/api" import { EmptyState } from "../components/EmptyState" import { LoadingSpinner } from "../components/LoadingSpinner" export default function Documents() { const queryClient = useQueryClient() const [uploading, setUploading] = useState(false) const { data: documents, isLoading, isError } = useQuery({ queryKey: ["documents"], queryFn: () => api.listDocuments() }) const uploadMutation = useMutation({ mutationFn: (file: File) => api.uploadDocument(file), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["documents"] }) setUploading(false) }, onError: () => { setUploading(false) alert("Upload failed") } }) const deleteMutation = useMutation({ mutationFn: (id: string) => api.deleteDocument(id), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["documents"] }) } }) const handleFileUpload = async (e: React.ChangeEvent) => { const file = e.target.files?.[0] if (!file) return setUploading(true) uploadMutation.mutate(file) } const formatSize = (bytes: number) => { if (bytes === 0) return "0 Bytes" const k = 1024 const sizes = ["Bytes", "KB", "MB", "GB"] const i = Math.floor(Math.log(bytes) / Math.log(k)) return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + " " + sizes[i] } if (isError) return
Error loading documents.
return (

Documents

Store and manage your project files

Upload a new document

PDF, Images, or Text files

{isLoading ? ( ) : documents && documents.length > 0 ? ( documents.map((doc) => ( )) ) : ( )}
Filename Size Date Actions
{doc.filename} {formatSize(doc.size)} {new Date(doc.createdAt).toLocaleDateString()}
) }