From 444ebf80071c083588c79e31f18d98eb740b2564 Mon Sep 17 00:00:00 2001 From: schroda <50052685+schroda@users.noreply.github.com> Date: Mon, 12 Feb 2024 21:45:50 +0100 Subject: [PATCH] Fix/reader outdated chapter page count (#593) * Always fetch pages for not downloaded chapter The pages of non downloaded chapters have to be re-fetched since the local data might be out of date which would result in page fetch failures * Correctly check for loading state of chapter For the first render of e.g. the next chapter, the "arePagesLoaded" flag was still true from the previous chapter which caused the loading state to incorrectly be false. This caused pages to get requested which on server side potentially pointed to invalid urls and thus the requests failed. * Use updated chapter in case page count changed --- src/screens/Reader.tsx | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/src/screens/Reader.tsx b/src/screens/Reader.tsx index f0c4a003..c7f1d76b 100644 --- a/src/screens/Reader.tsx +++ b/src/screens/Reader.tsx @@ -128,10 +128,8 @@ export function Reader() { Number(chapterIndex) === loadedChapter.current?.sourceOrder && loadedChapter.current?.pageCount !== -1; const manga = data?.manga ?? initialManga; - const { data: chapterData, loading: isChapterLoading } = requestManager.useGetMangaChapter(mangaId, chapterIndex, { - skip: isChapterLoaded, - }); - const [arePagesUpdated, setArePagesUpdated] = useState(false); + const { data: chapterData, loading: isChapterLoading } = requestManager.useGetMangaChapter(mangaId, chapterIndex); + const arePagesUpdatedRef = useRef(false); const { data: settingsData } = requestManager.useGetServerSettings(); const isDownloadAheadEnabled = !!settingsData?.settings.autoDownloadAheadLimit; @@ -141,12 +139,12 @@ export function Reader() { const isSameAsLoadedChapter = isAChapterLoaded && isChapterLoaded; if (isSameAsLoadedChapter) { - return loadedChapter.current; + const didPageCountChange = + chapterData?.chapter && loadedChapter.current?.pageCount !== chapterData.chapter.pageCount; + return didPageCountChange ? chapterData!.chapter : loadedChapter.current; } - if (arePagesUpdated) { - setArePagesUpdated(false); - } + arePagesUpdatedRef.current = false; if (chapterData?.chapter) { return chapterData.chapter; @@ -160,18 +158,17 @@ export function Reader() { const [fetchPages] = requestManager.useGetChapterPagesFetch(chapter.id); useEffect(() => { - const reCheckPages = !chapter.isDownloaded || chapter.pageCount === -1; - const shouldFetchPages = !isChapterLoading && reCheckPages; + const shouldFetchPages = !isChapterLoading && !chapter.isDownloaded; if (shouldFetchPages) { - fetchPages().then(() => setArePagesUpdated(true)); - } - - if (!reCheckPages && !arePagesUpdated) { - setArePagesUpdated(true); + fetchPages().then(() => { + arePagesUpdatedRef.current = true; + }); + } else { + arePagesUpdatedRef.current = true; } }, [chapter.id]); - const isLoading = isChapterLoading || !arePagesUpdated; + const isLoading = isChapterLoading || !arePagesUpdatedRef.current; const [wasLastPageReadSet, setWasLastPageReadSet] = useState(false); const [curPage, setCurPage] = useState(0); const isLastPage = curPage === chapter.pageCount - 1;