import { fetchBooks, searchBooks, fetchLibraries, BookDto, LibraryDto, getBookCoverUrl } from "../../lib/api"; import { BooksGrid, EmptyState } from "../components/BookCard"; import { Card, Button, FormField, FormInput, FormSelect, FormRow, CursorPagination } 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 cursor = typeof searchParamsAwaited.cursor === "string" ? searchParamsAwaited.cursor : undefined; const limit = typeof searchParamsAwaited.limit === "string" ? parseInt(searchParamsAwaited.limit) : 20; 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, limit).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 avec pagination const booksPage = await fetchBooks(libraryId, undefined, cursor, limit).catch(() => ({ items: [] as BookDto[], next_cursor: null, prev_cursor: null })); books = booksPage.items; nextCursor = booksPage.next_cursor; // Note: L'API ne supporte pas encore prev_cursor, on gère ça côté UI } const displayBooks = (searchResults || books).map(book => ({ ...book, coverUrl: getBookCoverUrl(book.id) })); const hasNextPage = !!nextCursor; const hasPrevPage = !!cursor; // Si on a un cursor, on peut revenir en arrière (simplifié) return ( <>
Found {totalHits} result{totalHits !== 1 ? 's' : ''} for "{searchQuery}"
)} {/* Grille de livres */} {displayBooks.length > 0 ? ( <>