import { fetchBooks, searchBooks, fetchLibraries, BookDto, LibraryDto, getBookCoverUrl } from "../../lib/api"; import { BooksGrid, EmptyState } from "../components/BookCard"; import { Card, Button, FormField, FormInput, FormSelect, FormRow } from "../components/ui"; import Link from "next/link"; export const dynamic = "force-dynamic"; export default async function BooksPage({ searchParams }: { searchParams: Promise<{ [key: string]: string | string[] | undefined }>; }) { const searchParamsAwaited = await searchParams; const libraryId = typeof searchParamsAwaited.library === "string" ? searchParamsAwaited.library : undefined; const searchQuery = typeof searchParamsAwaited.q === "string" ? searchParamsAwaited.q : ""; const [libraries] = await Promise.all([ fetchLibraries().catch(() => [] as LibraryDto[]) ]); let books: BookDto[] = []; let nextCursor: string | null = null; let searchResults: BookDto[] | null = null; let totalHits: number | null = null; if (searchQuery) { // Mode recherche const searchResponse = await searchBooks(searchQuery, libraryId).catch(() => null); if (searchResponse) { searchResults = searchResponse.hits.map(hit => ({ id: hit.id, library_id: hit.library_id, kind: hit.kind, title: hit.title, author: hit.author, series: hit.series, volume: hit.volume, language: hit.language, page_count: null, file_path: null, file_format: null, file_parse_status: null, updated_at: "" })); totalHits = searchResponse.estimated_total_hits; } } else { // Mode liste const booksPage = await fetchBooks(libraryId).catch(() => ({ items: [] as BookDto[], next_cursor: null })); books = booksPage.items; nextCursor = booksPage.next_cursor; } const displayBooks = (searchResults || books).map(book => ({ ...book, coverUrl: getBookCoverUrl(book.id) })); return ( <>
Found {totalHits} result{totalHits !== 1 ? 's' : ''} for "{searchQuery}"
)} {/* Grille de livres */} {displayBooks.length > 0 ? ( <>