function MainComponent() { const [books, setBooks] = useState([]); const [selectedCategory, setSelectedCategory] = useState("all"); const [error, setError] = useState(null); const categories = [ "all", "ficção", "não-ficção", "educacional", "tecnologia", "literatura", "Relacionamento", "Financeiro", ]; const handleFileUpload = useCallback(async (e) => { const file = e.target.files[0]; if (!file) return; if (file.size > 500 * 1024 * 1024) { setError( "O arquivo é muito grande. Por favor, escolha um arquivo menor que 500MB." ); return; } try { const reader = new FileReader(); reader.onload = function (e) { const newBook = { id: Date.now(), title: file.name.replace(".pdf", ""), url: e.target.result, category: "all", upload_date: new Date().toLocaleDateString(), }; window.saveBook(newBook); setBooks((prev) => [...prev, newBook]); setError(null); }; reader.readAsDataURL(file); } catch (err) { setError("Erro ao fazer upload do arquivo. Tente novamente."); } }, []); const updateBookCategory = async (bookId, category) => { window.updateCategory(bookId, category); setBooks((prev) => prev.map((book) => (book.id === bookId ? { ...book, category } : book)) ); }; const filteredBooks = books.filter((book) => selectedCategory === "all" ? true : book.category === selectedCategory ); useEffect(() => { const bloggerScript = document.createElement("script"); bloggerScript.innerHTML = ` // Simulando banco de dados local usando localStorage if (!localStorage.getItem('books')) { localStorage.setItem('books', JSON.stringify([])); } // Função para salvar livros window.saveBook = function(book) { const books = JSON.parse(localStorage.getItem('books') || '[]'); books.push(book); localStorage.setItem('books', JSON.stringify(books)); } // Função para atualizar categoria window.updateCategory = function(id, category) { const books = JSON.parse(localStorage.getItem('books') || '[]'); const index = books.findIndex(b => b.id === id); if (index !== -1) { books[index].category = category; localStorage.setItem('books', JSON.stringify(books)); } } `; document.head.appendChild(bloggerScript); const books = JSON.parse(localStorage.getItem("books") || "[]"); setBooks(books); }, []); return (

📚 Biblioteca Digital

{error && (
{error}
)}
{filteredBooks.map((book) => (

{book.title}

Adicionado em: {book.uploadDate}

Ler Livro
))}
{books.length === 0(

Sua biblioteca está vazia. Adicione alguns livros!

)}
); }