feat: local store read progress for later sync
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
import { KomgaBook } from "@/types/komga";
|
||||
import { BookReader } from "./BookReader";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { ClientOfflineBookService } from "@/lib/services/client-offlinebook.service";
|
||||
|
||||
interface ClientBookWrapperProps {
|
||||
book: KomgaBook;
|
||||
@@ -12,10 +13,11 @@ interface ClientBookWrapperProps {
|
||||
export function ClientBookWrapper({ book, pages }: ClientBookWrapperProps) {
|
||||
const router = useRouter();
|
||||
|
||||
const handleCloseReader = () => {
|
||||
const handleCloseReader = (currentPage: number) => {
|
||||
fetch(`/api/komga/cache/clear/${book.libraryId}/${book.seriesId}`, {
|
||||
method: "POST",
|
||||
});
|
||||
ClientOfflineBookService.setCurrentPage(book, currentPage);
|
||||
router.back();
|
||||
};
|
||||
|
||||
|
||||
@@ -101,7 +101,7 @@ export const ControlButtons = ({
|
||||
<button
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
onClose();
|
||||
onClose(currentPage);
|
||||
}}
|
||||
className={cn(
|
||||
"absolute top-4 right-4 p-2 rounded-full bg-background/50 hover:bg-background/80 transition-all duration-300 z-30",
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
import { useState, useCallback, useEffect, useRef } from "react";
|
||||
import { KomgaBook } from "@/types/komga";
|
||||
import { ClientOfflineBookService } from "@/lib/services/client-offlinebook.service";
|
||||
|
||||
interface UsePageNavigationProps {
|
||||
book: KomgaBook;
|
||||
pages: number[];
|
||||
isDoublePage: boolean;
|
||||
onClose?: () => void;
|
||||
onClose?: (currentPage: number) => void;
|
||||
direction: "ltr" | "rtl";
|
||||
}
|
||||
|
||||
@@ -16,7 +17,7 @@ export const usePageNavigation = ({
|
||||
onClose,
|
||||
direction,
|
||||
}: UsePageNavigationProps) => {
|
||||
const [currentPage, setCurrentPage] = useState(book.readProgress?.page || 1);
|
||||
const [currentPage, setCurrentPage] = useState(ClientOfflineBookService.getCurrentPage(book));
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
const [secondPageLoading, setSecondPageLoading] = useState(true);
|
||||
const [zoomLevel, setZoomLevel] = useState(1);
|
||||
@@ -37,6 +38,7 @@ export const usePageNavigation = ({
|
||||
const syncReadProgress = useCallback(
|
||||
async (page: number) => {
|
||||
try {
|
||||
ClientOfflineBookService.setCurrentPage(book, page);
|
||||
const completed = page === pages.length;
|
||||
await fetch(`/api/komga/books/${book.id}/read-progress`, {
|
||||
method: "PATCH",
|
||||
@@ -239,7 +241,7 @@ export const usePageNavigation = ({
|
||||
}
|
||||
} else if (e.key === "Escape" && onClose) {
|
||||
e.preventDefault();
|
||||
onClose();
|
||||
onClose(currentPage);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -269,6 +271,7 @@ export const usePageNavigation = ({
|
||||
if (timeoutRef.current) {
|
||||
clearTimeout(timeoutRef.current);
|
||||
syncReadProgress(currentPageRef.current);
|
||||
ClientOfflineBookService.removeCurrentPage(book);
|
||||
}
|
||||
};
|
||||
}, [syncReadProgress]);
|
||||
|
||||
@@ -12,7 +12,7 @@ export interface PageCache {
|
||||
export interface BookReaderProps {
|
||||
book: KomgaBook;
|
||||
pages: number[];
|
||||
onClose?: () => void;
|
||||
onClose?: (currentPage: number) => void;
|
||||
}
|
||||
|
||||
export interface ThumbnailProps {
|
||||
@@ -39,7 +39,7 @@ export interface ControlButtonsProps {
|
||||
onPreviousPage: () => void;
|
||||
onNextPage: () => void;
|
||||
onPageChange: (page: number) => void;
|
||||
onClose?: () => void;
|
||||
onClose?: (currentPage: number) => void;
|
||||
currentPage: number;
|
||||
totalPages: number;
|
||||
isDoublePage: boolean;
|
||||
|
||||
Reference in New Issue
Block a user