feat: mark as read
This commit is contained in:
@@ -3,6 +3,8 @@
|
|||||||
import { KomgaBook } from "@/types/komga";
|
import { KomgaBook } from "@/types/komga";
|
||||||
import { formatDate } from "@/lib/utils";
|
import { formatDate } from "@/lib/utils";
|
||||||
import { Cover } from "@/components/ui/cover";
|
import { Cover } from "@/components/ui/cover";
|
||||||
|
import { MarkAsReadButton } from "@/components/ui/mark-as-read-button";
|
||||||
|
import { useState } from "react";
|
||||||
|
|
||||||
interface BookGridProps {
|
interface BookGridProps {
|
||||||
books: KomgaBook[];
|
books: KomgaBook[];
|
||||||
@@ -40,7 +42,9 @@ const getReadingStatusInfo = (book: KomgaBook) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export function BookGrid({ books, onBookClick }: BookGridProps) {
|
export function BookGrid({ books, onBookClick }: BookGridProps) {
|
||||||
if (!books.length) {
|
const [localBooks, setLocalBooks] = useState(books);
|
||||||
|
|
||||||
|
if (!localBooks.length) {
|
||||||
return (
|
return (
|
||||||
<div className="text-center p-8">
|
<div className="text-center p-8">
|
||||||
<p className="text-muted-foreground">Aucun tome disponible</p>
|
<p className="text-muted-foreground">Aucun tome disponible</p>
|
||||||
@@ -48,33 +52,77 @@ export function BookGrid({ books, onBookClick }: BookGridProps) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const handleMarkAsRead = (bookId: string) => {
|
||||||
|
setLocalBooks((prevBooks) =>
|
||||||
|
prevBooks.map((book) =>
|
||||||
|
book.id === bookId
|
||||||
|
? {
|
||||||
|
...book,
|
||||||
|
readProgress: {
|
||||||
|
...(book.readProgress || {}),
|
||||||
|
completed: true,
|
||||||
|
readDate: new Date().toISOString(),
|
||||||
|
page: book.media.pagesCount,
|
||||||
|
created: book.readProgress?.created || new Date().toISOString(),
|
||||||
|
lastModified: new Date().toISOString(),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
: book
|
||||||
|
)
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="grid grid-cols-2 gap-4 sm:grid-cols-3 md:grid-cols-4 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) => {
|
{localBooks.map((book) => {
|
||||||
const statusInfo = getReadingStatusInfo(book);
|
const statusInfo = getReadingStatusInfo(book);
|
||||||
|
const isRead = book.readProgress?.completed || false;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<button
|
<div
|
||||||
key={book.id}
|
key={book.id}
|
||||||
onClick={() => onBookClick(book)}
|
className="group relative aspect-[2/3] overflow-hidden rounded-lg bg-muted"
|
||||||
className="group relative aspect-[2/3] overflow-hidden rounded-lg bg-muted hover:opacity-100 transition-all"
|
|
||||||
>
|
>
|
||||||
<Cover
|
<button
|
||||||
type="book"
|
onClick={() => onBookClick(book)}
|
||||||
id={book.id}
|
className="w-full h-full hover:opacity-100 transition-all"
|
||||||
alt={`Couverture de ${book.metadata.title || `Tome ${book.metadata.number}`}`}
|
>
|
||||||
isCompleted={book.readProgress?.completed}
|
<Cover
|
||||||
/>
|
type="book"
|
||||||
<div className="absolute inset-x-0 bottom-0 bg-gradient-to-t from-black/60 to-transparent p-4 space-y-2 translate-y-full group-hover:translate-y-0 transition-transform duration-200">
|
id={book.id}
|
||||||
<p className="text-sm font-medium text-white text-left line-clamp-2">
|
alt={`Couverture de ${book.metadata.title || `Tome ${book.metadata.number}`}`}
|
||||||
{book.metadata.title || `Tome ${book.metadata.number}`}
|
isCompleted={isRead}
|
||||||
</p>
|
/>
|
||||||
<div className="flex items-center gap-2">
|
</button>
|
||||||
<span className={`px-2 py-0.5 rounded-full text-xs ${statusInfo.className}`}>
|
|
||||||
{statusInfo.label}
|
{/* Overlay avec les contrôles */}
|
||||||
</span>
|
<div className="absolute inset-0 pointer-events-none">
|
||||||
|
{/* Bouton Marquer comme lu en haut à droite avec un petit décalage */}
|
||||||
|
{!isRead && (
|
||||||
|
<div className="absolute top-2 right-2 pointer-events-auto">
|
||||||
|
<MarkAsReadButton
|
||||||
|
bookId={book.id}
|
||||||
|
pagesCount={book.media.pagesCount}
|
||||||
|
isRead={isRead}
|
||||||
|
onSuccess={() => handleMarkAsRead(book.id)}
|
||||||
|
className="bg-white/90 hover:bg-white text-black shadow-sm"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Informations en bas - visible au survol uniquement */}
|
||||||
|
<div className="absolute inset-x-0 bottom-0 bg-gradient-to-t from-black/60 to-transparent p-4 space-y-2 translate-y-full group-hover:translate-y-0 transition-transform duration-200">
|
||||||
|
<p className="text-sm font-medium text-white text-left line-clamp-2">
|
||||||
|
{book.metadata.title || `Tome ${book.metadata.number}`}
|
||||||
|
</p>
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<span className={`px-2 py-0.5 rounded-full text-xs ${statusInfo.className}`}>
|
||||||
|
{statusInfo.label}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</button>
|
</div>
|
||||||
);
|
);
|
||||||
})}
|
})}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
65
src/components/ui/mark-as-read-button.tsx
Normal file
65
src/components/ui/mark-as-read-button.tsx
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { CheckCircle2 } from "lucide-react";
|
||||||
|
import { Button } from "./button";
|
||||||
|
import { useToast } from "./use-toast";
|
||||||
|
|
||||||
|
interface MarkAsReadButtonProps {
|
||||||
|
bookId: string;
|
||||||
|
pagesCount: number;
|
||||||
|
isRead?: boolean;
|
||||||
|
onSuccess?: () => void;
|
||||||
|
className?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function MarkAsReadButton({
|
||||||
|
bookId,
|
||||||
|
pagesCount,
|
||||||
|
isRead = false,
|
||||||
|
onSuccess,
|
||||||
|
className,
|
||||||
|
}: MarkAsReadButtonProps) {
|
||||||
|
const { toast } = useToast();
|
||||||
|
|
||||||
|
const handleMarkAsRead = async (e: React.MouseEvent) => {
|
||||||
|
e.stopPropagation(); // Empêcher la propagation au parent
|
||||||
|
try {
|
||||||
|
const response = await fetch(`/api/komga/books/${bookId}/read-progress`, {
|
||||||
|
method: "PATCH",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ page: pagesCount, completed: true }),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error("Erreur lors de la mise à jour");
|
||||||
|
}
|
||||||
|
|
||||||
|
toast({
|
||||||
|
title: "Succès",
|
||||||
|
description: "Le tome a été marqué comme lu",
|
||||||
|
});
|
||||||
|
onSuccess?.();
|
||||||
|
} catch (error) {
|
||||||
|
toast({
|
||||||
|
title: "Erreur",
|
||||||
|
description: "Impossible de marquer le tome comme lu",
|
||||||
|
variant: "destructive",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
size="icon"
|
||||||
|
onClick={handleMarkAsRead}
|
||||||
|
className={`h-8 w-8 p-0 rounded-br-lg rounded-tl-lg ${className}`}
|
||||||
|
disabled={isRead}
|
||||||
|
aria-label="Marquer comme lu"
|
||||||
|
>
|
||||||
|
<CheckCircle2 className="h-5 w-5" />
|
||||||
|
</Button>
|
||||||
|
);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user