"use client" import { useState, useEffect } from "react" import { BookCard } from "./book-card" interface Book { id: string title: string author: string coverUrl: string classification: string subClassification: string } export function LibrarySection() { const [books, setBooks] = useState([]) const [error, setError] = useState(null) useEffect(() => { fetch("/api/library") .then((response) => { if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`) } return response.json() }) .then((data) => { if (!data.books || !Array.isArray(data.books)) { throw new Error("Invalid data structure") } setBooks(data.books) }) .catch((error) => { console.error("Error loading library data:", error) setError(`Failed to load library data: ${error.message}`) }) }, []) if (error) { return
{error}
} if (books.length === 0) { return
Loading library data...
} const groupedBooks = books.reduce( (acc, book) => { if (book && book.classification) { if (!acc[book.classification]) { acc[book.classification] = [] } acc[book.classification].push(book) } return acc }, {} as Record, ) return (
{Object.entries(groupedBooks).map(([classification, books]) => (

{getClassificationName(classification)}

{books.map((book) => ( ))}
))}
) } function getClassificationName(classification: string): string { const classifications: Record = { A: "General Works", B: "Philosophy, Psychology, Religion", C: "Auxiliary Sciences of History", D: "World History", E: "History of the Americas", F: "History of the Americas", G: "Geography, Anthropology, Recreation", H: "Social Sciences", J: "Political Science", K: "Law", L: "Education", M: "Music", N: "Fine Arts", P: "Language and Literature", Q: "Science", R: "Medicine", S: "Agriculture", T: "Technology", U: "Military Science", V: "Naval Science", Z: "Bibliography, Library Science, Information Resources", } return classifications[classification] || "Unknown Classification" }