feat: add navigation to book reader from series page - Add book click handler in PaginatedBookGrid - Simplify BookGrid component with direct navigation - Keep book status and metadata display - Ensure consistent navigation behavior across the app

This commit is contained in:
Julien Froidefond
2025-02-11 23:03:06 +01:00
parent ba12c87e57
commit 89a3491b0f
6 changed files with 194 additions and 120 deletions

View File

@@ -99,7 +99,7 @@ Créer une application web moderne avec Next.js permettant de lire des fichiers
- [x] Responsive design - [x] Responsive design
- [x] Page d'accueil - [x] Page d'accueil
- [x] Présentation des fonctionnalités principales - [x] Présentation des fonctionnalités principales
- [ ] Liste des collections récentes - [x] Liste des collections récentes
- [ ] Barre de recherche - [ ] Barre de recherche
- [ ] Filtres avancés - [ ] Filtres avancés
- [ ] Tri personnalisable - [ ] Tri personnalisable

View File

@@ -30,6 +30,7 @@ export default function BookPage({ params }: { params: { bookId: string } }) {
} }
const data = await response.json(); const data = await response.json();
setData(data); setData(data);
setIsReading(true);
} catch (error) { } catch (error) {
console.error("Erreur:", error); console.error("Erreur:", error);
setError(error instanceof Error ? error.message : "Une erreur est survenue"); setError(error instanceof Error ? error.message : "Une erreur est survenue");
@@ -41,12 +42,9 @@ export default function BookPage({ params }: { params: { bookId: string } }) {
fetchBookData(); fetchBookData();
}, [params.bookId]); }, [params.bookId]);
const handleStartReading = () => {
setIsReading(true);
};
const handleCloseReader = () => { const handleCloseReader = () => {
setIsReading(false); setIsReading(false);
router.back();
}; };
if (isLoading) { if (isLoading) {
@@ -76,8 +74,11 @@ export default function BookPage({ params }: { params: { bookId: string } }) {
const { book, pages } = data; const { book, pages } = data;
if (isReading) {
return <BookReader book={book} pages={pages} onClose={handleCloseReader} />;
}
return ( return (
<>
<div className="container py-8 space-y-8"> <div className="container py-8 space-y-8">
{/* En-tête du tome */} {/* En-tête du tome */}
<div className="flex flex-col md:flex-row gap-8"> <div className="flex flex-col md:flex-row gap-8">
@@ -141,20 +142,8 @@ export default function BookPage({ params }: { params: { bookId: string } }) {
</div> </div>
)} )}
</div> </div>
{/* Bouton de lecture */}
<button
onClick={handleStartReading}
className="inline-flex items-center justify-center rounded-md bg-primary px-4 py-2 text-sm font-medium text-primary-foreground ring-offset-background transition-colors hover:bg-primary/90 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2"
>
Commencer la lecture
</button>
</div> </div>
</div> </div>
</div> </div>
{/* Lecteur */}
{isReading && <BookReader book={book} pages={pages} onClose={handleCloseReader} />}
</>
); );
} }

View File

@@ -89,9 +89,18 @@ function MediaCard({ item, onClick }: MediaCardProps) {
? item.metadata.title ? item.metadata.title
: item.metadata.title || `Tome ${item.metadata.number}`; : item.metadata.title || `Tome ${item.metadata.number}`;
const handleClick = () => {
console.log("MediaCard - handleClick:", {
itemType: isSeries ? "series" : "book",
itemId: item.id,
itemTitle: title,
});
onClick?.();
};
return ( return (
<button <button
onClick={onClick} onClick={handleClick}
className="flex-shrink-0 w-[200px] group relative flex flex-col rounded-lg border bg-card text-card-foreground shadow-sm hover:bg-accent hover:text-accent-foreground transition-colors overflow-hidden" className="flex-shrink-0 w-[200px] group relative flex flex-col rounded-lg border bg-card text-card-foreground shadow-sm hover:bg-accent hover:text-accent-foreground transition-colors overflow-hidden"
> >
{/* Image de couverture */} {/* Image de couverture */}

View File

@@ -1,7 +1,14 @@
"use client"; "use client";
import { KomgaBook } from "@/types/komga"; import { KomgaBook } from "@/types/komga";
import { ChevronLeft, ChevronRight, ImageOff, Loader2 } from "lucide-react"; import {
ChevronLeft,
ChevronRight,
ImageOff,
Loader2,
LayoutTemplate,
SplitSquareVertical,
} from "lucide-react";
import Image from "next/image"; import Image from "next/image";
import { useEffect, useState, useCallback } from "react"; import { useEffect, useState, useCallback } from "react";
@@ -15,22 +22,39 @@ export function BookReader({ book, pages, onClose }: BookReaderProps) {
const [currentPage, setCurrentPage] = useState(1); const [currentPage, setCurrentPage] = useState(1);
const [isLoading, setIsLoading] = useState(true); const [isLoading, setIsLoading] = useState(true);
const [imageError, setImageError] = useState(false); const [imageError, setImageError] = useState(false);
const [isDoublePage, setIsDoublePage] = useState(false);
// Fonction pour déterminer si on doit afficher une ou deux pages
const shouldShowDoublePage = useCallback(
(pageNumber: number) => {
if (!isDoublePage) return false;
// Toujours afficher la première page seule (couverture)
if (pageNumber === 1) return false;
// Vérifier si on a une page suivante disponible
return pageNumber < pages.length;
},
[isDoublePage, pages.length]
);
const handlePreviousPage = useCallback(() => { const handlePreviousPage = useCallback(() => {
if (currentPage > 1) { if (currentPage > 1) {
setCurrentPage(currentPage - 1); // En mode double page, reculer de 2 pages sauf si on est sur la page 2
const newPage = isDoublePage && currentPage > 2 ? currentPage - 2 : currentPage - 1;
setCurrentPage(newPage);
setIsLoading(true); setIsLoading(true);
setImageError(false); setImageError(false);
} }
}, [currentPage]); }, [currentPage, isDoublePage]);
const handleNextPage = useCallback(() => { const handleNextPage = useCallback(() => {
if (currentPage < pages.length) { if (currentPage < pages.length) {
setCurrentPage(currentPage + 1); // En mode double page, avancer de 2 pages sauf si c'est la dernière paire
const newPage = isDoublePage ? Math.min(currentPage + 2, pages.length) : currentPage + 1;
setCurrentPage(newPage);
setIsLoading(true); setIsLoading(true);
setImageError(false); setImageError(false);
} }
}, [currentPage, pages.length]); }, [currentPage, pages.length, isDoublePage]);
useEffect(() => { useEffect(() => {
const handleKeyDown = (event: KeyboardEvent) => { const handleKeyDown = (event: KeyboardEvent) => {
@@ -50,19 +74,36 @@ export function BookReader({ book, pages, onClose }: BookReaderProps) {
return ( return (
<div className="fixed inset-0 bg-background/95 backdrop-blur-sm z-50"> <div className="fixed inset-0 bg-background/95 backdrop-blur-sm z-50">
<div className="relative h-full flex items-center justify-center"> <div className="relative h-full flex items-center justify-center">
{/* Bouton mode double page */}
<button
onClick={() => setIsDoublePage(!isDoublePage)}
className="absolute top-4 left-4 p-2 rounded-full bg-background/50 hover:bg-background/80 transition-colors"
aria-label={
isDoublePage ? "Désactiver le mode double page" : "Activer le mode double page"
}
>
{isDoublePage ? (
<LayoutTemplate className="h-6 w-6" />
) : (
<SplitSquareVertical className="h-6 w-6" />
)}
</button>
{/* Bouton précédent */} {/* Bouton précédent */}
{currentPage > 1 && ( {currentPage > 1 && (
<button <button
onClick={handlePreviousPage} onClick={handlePreviousPage}
className="absolute left-4 p-2 rounded-full bg-background/50 hover:bg-background/80 transition-colors" className="absolute left-4 top-1/2 -translate-y-1/2 p-2 rounded-full bg-background/50 hover:bg-background/80 transition-colors"
aria-label="Page précédente" aria-label="Page précédente"
> >
<ChevronLeft className="h-8 w-8" /> <ChevronLeft className="h-8 w-8" />
</button> </button>
)} )}
{/* Pages */}
<div className="relative h-full max-h-full w-auto max-w-full p-4 flex gap-2">
{/* Page courante */} {/* Page courante */}
<div className="relative h-full max-h-full w-auto max-w-full p-4"> <div className="relative h-full w-auto">
{isLoading && ( {isLoading && (
<div className="absolute inset-0 flex items-center justify-center"> <div className="absolute inset-0 flex items-center justify-center">
<Loader2 className="h-8 w-8 animate-spin" /> <Loader2 className="h-8 w-8 animate-spin" />
@@ -89,11 +130,27 @@ export function BookReader({ book, pages, onClose }: BookReaderProps) {
)} )}
</div> </div>
{/* Deuxième page en mode double page */}
{shouldShowDoublePage(currentPage) && (
<div className="relative h-full w-auto">
<Image
src={`/api/komga/books/${book.id}/pages/${currentPage + 1}`}
alt={`Page ${currentPage + 1}`}
className="h-full w-auto object-contain"
width={800}
height={1200}
priority
onError={() => setImageError(true)}
/>
</div>
)}
</div>
{/* Bouton suivant */} {/* Bouton suivant */}
{currentPage < pages.length && ( {currentPage < pages.length && (
<button <button
onClick={handleNextPage} onClick={handleNextPage}
className="absolute right-4 p-2 rounded-full bg-background/50 hover:bg-background/80 transition-colors" className="absolute right-4 top-1/2 -translate-y-1/2 p-2 rounded-full bg-background/50 hover:bg-background/80 transition-colors"
aria-label="Page suivante" aria-label="Page suivante"
> >
<ChevronRight className="h-8 w-8" /> <ChevronRight className="h-8 w-8" />
@@ -102,7 +159,8 @@ export function BookReader({ book, pages, onClose }: BookReaderProps) {
{/* Indicateur de page */} {/* Indicateur de page */}
<div className="absolute bottom-4 left-1/2 -translate-x-1/2 px-4 py-2 rounded-full bg-background/50 text-sm"> <div className="absolute bottom-4 left-1/2 -translate-x-1/2 px-4 py-2 rounded-full bg-background/50 text-sm">
Page {currentPage} / {pages.length} Page {currentPage}
{shouldShowDoublePage(currentPage) ? `-${currentPage + 1}` : ""} / {pages.length}
</div> </div>
{/* Bouton fermer */} {/* Bouton fermer */}

View File

@@ -7,7 +7,7 @@ import { useState } from "react";
interface BookGridProps { interface BookGridProps {
books: KomgaBook[]; books: KomgaBook[];
onBookClick?: (book: KomgaBook) => void; onBookClick: (book: KomgaBook) => void;
getBookThumbnailUrl: (bookId: string) => string; getBookThumbnailUrl: (bookId: string) => string;
} }
@@ -21,14 +21,26 @@ export function BookGrid({ books, onBookClick, getBookThumbnailUrl }: BookGridPr
} }
return ( return (
<div className="grid gap-4 sm:grid-cols-3 lg:grid-cols-6"> <div className="grid grid-cols-2 gap-4 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-6">
{books.map((book) => ( {books.map((book) => (
<BookCard <button
key={book.id} key={book.id}
book={book} onClick={() => onBookClick(book)}
onClick={() => onBookClick?.(book)} className="group relative aspect-[2/3] overflow-hidden rounded-lg bg-muted hover:opacity-80 transition-opacity"
getBookThumbnailUrl={getBookThumbnailUrl} >
<Image
src={getBookThumbnailUrl(book.id)}
alt={book.metadata.title}
fill
className="object-cover"
sizes="(min-width: 1024px) 16.66vw, (min-width: 768px) 25vw, (min-width: 640px) 33.33vw, 50vw"
/> />
<div className="absolute inset-x-0 bottom-0 bg-gradient-to-t from-black/60 to-transparent p-4">
<p className="text-sm font-medium text-white text-left line-clamp-2">
{book.metadata.title}
</p>
</div>
</button>
))} ))}
</div> </div>
); );

View File

@@ -15,7 +15,6 @@ interface PaginatedBookGridProps {
totalPages: number; totalPages: number;
totalElements: number; totalElements: number;
pageSize: number; pageSize: number;
onBookClick?: (book: KomgaBook) => void;
} }
export function PaginatedBookGrid({ export function PaginatedBookGrid({
@@ -25,7 +24,6 @@ export function PaginatedBookGrid({
totalPages, totalPages,
totalElements, totalElements,
pageSize, pageSize,
onBookClick,
}: PaginatedBookGridProps) { }: PaginatedBookGridProps) {
const router = useRouter(); const router = useRouter();
const pathname = usePathname(); const pathname = usePathname();
@@ -66,6 +64,14 @@ export function PaginatedBookGrid({
router.push(`${pathname}?${params.toString()}`); router.push(`${pathname}?${params.toString()}`);
}; };
const handleBookClick = (book: KomgaBook) => {
console.log("PaginatedBookGrid - handleBookClick:", {
bookId: book.id,
bookTitle: book.metadata.title,
});
router.push(`/books/${book.id}`);
};
// Calcul des indices de début et de fin pour l'affichage // Calcul des indices de début et de fin pour l'affichage
const startIndex = (currentPage - 1) * pageSize + 1; const startIndex = (currentPage - 1) * pageSize + 1;
const endIndex = Math.min(currentPage * pageSize, totalElements); const endIndex = Math.min(currentPage * pageSize, totalElements);
@@ -117,7 +123,7 @@ export function PaginatedBookGrid({
> >
<BookGrid <BookGrid
books={books} books={books}
onBookClick={onBookClick} onBookClick={handleBookClick}
getBookThumbnailUrl={getBookThumbnailUrl} getBookThumbnailUrl={getBookThumbnailUrl}
/> />
</div> </div>