feat: update PhotoswipeReader to maintain current page reference and sync read progress on cleanup

This commit is contained in:
Julien Froidefond
2025-10-17 22:22:55 +02:00
parent 4672532a3a
commit adddac83b0

View File

@@ -36,6 +36,12 @@ export function PhotoswipeReader({ book, pages, onClose, nextBook }: BookReaderP
const touchStartXRef = useRef<number | null>(null); const touchStartXRef = useRef<number | null>(null);
const touchStartYRef = useRef<number | null>(null); const touchStartYRef = useRef<number | null>(null);
const isPinchingRef = useRef(false); const isPinchingRef = useRef(false);
const currentPageRef = useRef(currentPage);
// Garder currentPage à jour dans la ref pour le cleanup
useEffect(() => {
currentPageRef.current = currentPage;
}, [currentPage]);
// Auto double page en paysage // Auto double page en paysage
useEffect(() => { useEffect(() => {
@@ -85,10 +91,13 @@ export function PhotoswipeReader({ book, pages, onClose, nextBook }: BookReaderP
(page: number) => { (page: number) => {
if (page >= 1 && page <= pages.length) { if (page >= 1 && page <= pages.length) {
setCurrentPage(page); setCurrentPage(page);
// Mettre à jour le localStorage immédiatement
ClientOfflineBookService.setCurrentPage(book, page);
// Débouncer seulement l'API Komga
debouncedSync(page); debouncedSync(page);
} }
}, },
[pages.length, debouncedSync] [pages.length, debouncedSync, book]
); );
const handlePreviousPage = useCallback(() => { const handlePreviousPage = useCallback(() => {
@@ -209,16 +218,16 @@ export function PhotoswipeReader({ book, pages, onClose, nextBook }: BookReaderP
}; };
}, [handleNextPage, handlePreviousPage, handleTouchStart, handleTouchEnd, onClose, isRTL, currentPage]); }, [handleNextPage, handlePreviousPage, handleTouchStart, handleTouchEnd, onClose, isRTL, currentPage]);
// Cleanup // Cleanup - Sync final sans debounce
useEffect(() => { useEffect(() => {
return () => { return () => {
if (syncTimeoutRef.current) { if (syncTimeoutRef.current) {
clearTimeout(syncTimeoutRef.current); clearTimeout(syncTimeoutRef.current);
syncReadProgress(currentPage);
} }
ClientOfflineBookService.removeCurrentPage(book); // Sync immédiatement au cleanup avec la VRAIE valeur actuelle
syncReadProgress(currentPageRef.current);
}; };
}, [syncReadProgress, book, currentPage]); }, [syncReadProgress]);
const getPageUrl = useCallback((pageNum: number) => `/api/komga/books/${book.id}/pages/${pageNum}`, [book.id]); const getPageUrl = useCallback((pageNum: number) => `/api/komga/books/${book.id}/pages/${pageNum}`, [book.id]);