2021-03-26 04:17:02 +04:30
|
|
|
/*
|
|
|
|
|
* Copyright (C) Contributors to the Suwayomi project
|
|
|
|
|
*
|
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
2021-01-26 23:32:12 +03:30
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
2023-05-18 13:17:41 +02:00
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
2021-01-26 23:32:12 +03:30
|
|
|
|
2021-09-09 17:51:22 +04:30
|
|
|
import CircularProgress from '@mui/material/CircularProgress';
|
2023-09-30 16:55:35 +02:00
|
|
|
import { useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react';
|
2023-06-04 21:30:15 +02:00
|
|
|
import { useLocation, useNavigate, useParams } from 'react-router-dom';
|
2023-06-04 21:08:39 +02:00
|
|
|
import { Box } from '@mui/material';
|
2023-06-05 01:42:39 +02:00
|
|
|
import { useTranslation } from 'react-i18next';
|
2024-02-18 15:20:30 +01:00
|
|
|
import { AllowedMetadataValueTypes, ChapterOffset, IReaderSettings, ReaderType, TChapter, TManga } from '@/typings';
|
2023-10-28 00:32:02 +02:00
|
|
|
import { requestManager } from '@/lib/requests/RequestManager.ts';
|
2023-01-06 17:34:16 +01:00
|
|
|
import {
|
|
|
|
|
checkAndHandleMissingStoredReaderSettings,
|
|
|
|
|
getReaderSettingsFor,
|
|
|
|
|
useDefaultReaderSettings,
|
2023-06-05 01:42:39 +02:00
|
|
|
} from '@/util/readerSettings';
|
|
|
|
|
import { requestUpdateMangaMetadata } from '@/util/metadata';
|
2023-10-28 00:32:02 +02:00
|
|
|
import { HorizontalPager } from '@/components/reader/pager/HorizontalPager';
|
|
|
|
|
import { PageNumber } from '@/components/reader/PageNumber';
|
|
|
|
|
import { PagedPager } from '@/components/reader/pager/PagedPager';
|
|
|
|
|
import { DoublePagedPager } from '@/components/reader/pager/DoublePagedPager';
|
|
|
|
|
import { VerticalPager } from '@/components/reader/pager/VerticalPager';
|
|
|
|
|
import { ReaderNavBar } from '@/components/navbar/ReaderNavBar';
|
|
|
|
|
import { makeToast } from '@/components/util/Toast';
|
|
|
|
|
import { NavBarContext } from '@/components/context/NavbarContext.tsx';
|
2023-12-30 14:30:04 +01:00
|
|
|
import { useDebounce } from '@/util/useDebounce.ts';
|
2023-11-19 21:17:35 +01:00
|
|
|
import { UpdateChapterPatchInput } from '@/lib/graphql/generated/graphql.ts';
|
|
|
|
|
import { useMetadataServerSettings } from '@/util/metadataServerSettings.ts';
|
2023-12-31 01:42:54 +01:00
|
|
|
import { defaultPromiseErrorHandler } from '@/util/defaultPromiseErrorHandler.ts';
|
2024-02-16 19:16:18 +01:00
|
|
|
import { Chapters } from '@/lib/data/Chapters.ts';
|
2023-04-05 13:37:12 +02:00
|
|
|
|
2021-05-15 23:22:37 +04:30
|
|
|
const getReaderComponent = (readerType: ReaderType) => {
|
|
|
|
|
switch (readerType) {
|
|
|
|
|
case 'ContinuesVertical':
|
|
|
|
|
case 'Webtoon':
|
2021-05-17 01:38:59 +04:30
|
|
|
return VerticalPager;
|
2021-05-15 23:22:37 +04:30
|
|
|
break;
|
|
|
|
|
case 'SingleVertical':
|
|
|
|
|
case 'SingleRTL':
|
|
|
|
|
case 'SingleLTR':
|
2021-05-28 05:36:55 -07:00
|
|
|
return PagedPager;
|
|
|
|
|
break;
|
|
|
|
|
case 'DoubleVertical':
|
|
|
|
|
case 'DoubleRTL':
|
|
|
|
|
case 'DoubleLTR':
|
|
|
|
|
return DoublePagedPager;
|
2021-05-15 23:22:37 +04:30
|
|
|
break;
|
2021-05-29 08:11:59 -07:00
|
|
|
case 'ContinuesHorizontalLTR':
|
|
|
|
|
case 'ContinuesHorizontalRTL':
|
2021-05-17 01:38:59 +04:30
|
|
|
return HorizontalPager;
|
2021-05-15 23:22:37 +04:30
|
|
|
default:
|
2021-05-17 01:38:59 +04:30
|
|
|
return VerticalPager;
|
2021-05-15 23:22:37 +04:30
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-02-06 10:06:33 +01:00
|
|
|
const range = (n: number) => Array.from({ length: n }, (value, key) => key);
|
2023-06-04 21:13:07 +02:00
|
|
|
const initialChapter = {
|
2021-12-01 03:03:52 +03:30
|
|
|
pageCount: -1,
|
2023-09-30 16:55:35 +02:00
|
|
|
sourceOrder: -1,
|
2021-12-01 03:03:52 +03:30
|
|
|
chapterCount: 0,
|
2022-11-24 21:05:05 +01:00
|
|
|
lastPageRead: 0,
|
2021-12-01 03:03:52 +03:30
|
|
|
name: 'Loading...',
|
2023-10-15 16:03:08 +02:00
|
|
|
} as unknown as TChapter;
|
2021-01-22 17:00:33 +03:30
|
|
|
|
2023-10-28 00:32:02 +02:00
|
|
|
export function Reader() {
|
2023-03-23 13:59:32 +01:00
|
|
|
const { t } = useTranslation();
|
2023-06-04 21:30:15 +02:00
|
|
|
const navigate = useNavigate();
|
|
|
|
|
const location = useLocation();
|
2021-03-09 16:44:09 +03:30
|
|
|
|
2023-02-06 10:06:33 +01:00
|
|
|
const { chapterIndex, mangaId } = useParams<{ chapterIndex: string; mangaId: string }>();
|
2023-09-08 00:44:01 +02:00
|
|
|
|
|
|
|
|
const initialManga = useMemo(
|
|
|
|
|
() =>
|
|
|
|
|
({
|
|
|
|
|
id: +mangaId,
|
|
|
|
|
title: '',
|
|
|
|
|
thumbnailUrl: '',
|
|
|
|
|
genre: [],
|
|
|
|
|
inLibraryAt: 0,
|
|
|
|
|
lastReadAt: 0,
|
2023-09-30 16:55:35 +02:00
|
|
|
chapters: { totalCount: 0 },
|
2023-10-15 16:03:08 +02:00
|
|
|
}) as unknown as TManga,
|
2023-09-08 00:44:01 +02:00
|
|
|
[mangaId],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const { data, loading: isMangaLoading } = requestManager.useGetManga(mangaId);
|
2023-10-15 16:03:08 +02:00
|
|
|
const loadedChapter = useRef<TChapter | null>(null);
|
2023-09-30 16:55:35 +02:00
|
|
|
const isChapterLoaded =
|
|
|
|
|
Number(mangaId) === loadedChapter.current?.manga.id &&
|
|
|
|
|
Number(chapterIndex) === loadedChapter.current?.sourceOrder &&
|
|
|
|
|
loadedChapter.current?.pageCount !== -1;
|
2023-10-15 16:03:08 +02:00
|
|
|
const manga = data?.manga ?? initialManga;
|
2024-02-12 21:45:50 +01:00
|
|
|
const { data: chapterData, loading: isChapterLoading } = requestManager.useGetMangaChapter(mangaId, chapterIndex);
|
|
|
|
|
const arePagesUpdatedRef = useRef(false);
|
2023-09-30 16:55:35 +02:00
|
|
|
|
2024-02-17 20:24:02 +01:00
|
|
|
const {
|
|
|
|
|
settings: { downloadAheadLimit },
|
|
|
|
|
} = useMetadataServerSettings();
|
|
|
|
|
const isDownloadAheadEnabled = !!downloadAheadLimit;
|
2023-11-19 21:17:35 +01:00
|
|
|
|
2023-09-30 16:55:35 +02:00
|
|
|
const getLoadedChapter = () => {
|
|
|
|
|
const isAChapterLoaded = loadedChapter.current;
|
|
|
|
|
|
|
|
|
|
const isSameAsLoadedChapter = isAChapterLoaded && isChapterLoaded;
|
|
|
|
|
if (isSameAsLoadedChapter) {
|
2024-02-12 21:45:50 +01:00
|
|
|
const didPageCountChange =
|
|
|
|
|
chapterData?.chapter && loadedChapter.current?.pageCount !== chapterData.chapter.pageCount;
|
|
|
|
|
return didPageCountChange ? chapterData!.chapter : loadedChapter.current;
|
2023-09-30 16:55:35 +02:00
|
|
|
}
|
|
|
|
|
|
2024-02-12 21:45:50 +01:00
|
|
|
arePagesUpdatedRef.current = false;
|
2023-11-04 22:40:21 +01:00
|
|
|
|
2023-09-30 16:55:35 +02:00
|
|
|
if (chapterData?.chapter) {
|
2023-10-15 16:03:08 +02:00
|
|
|
return chapterData.chapter;
|
2023-09-30 16:55:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
};
|
|
|
|
|
loadedChapter.current = getLoadedChapter();
|
|
|
|
|
|
|
|
|
|
const chapter = loadedChapter.current ?? initialChapter;
|
2023-11-04 22:40:21 +01:00
|
|
|
const [fetchPages] = requestManager.useGetChapterPagesFetch(chapter.id);
|
2023-09-30 16:55:35 +02:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2024-02-12 21:45:50 +01:00
|
|
|
const shouldFetchPages = !isChapterLoading && !chapter.isDownloaded;
|
2023-11-04 22:40:21 +01:00
|
|
|
if (shouldFetchPages) {
|
2024-02-12 21:45:50 +01:00
|
|
|
fetchPages().then(() => {
|
|
|
|
|
arePagesUpdatedRef.current = true;
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
arePagesUpdatedRef.current = true;
|
2023-09-30 16:55:35 +02:00
|
|
|
}
|
|
|
|
|
}, [chapter.id]);
|
|
|
|
|
|
2024-02-12 21:45:50 +01:00
|
|
|
const isLoading = isChapterLoading || !arePagesUpdatedRef.current;
|
2023-06-08 13:43:58 +02:00
|
|
|
const [wasLastPageReadSet, setWasLastPageReadSet] = useState(false);
|
2021-03-19 14:52:20 +03:30
|
|
|
const [curPage, setCurPage] = useState<number>(0);
|
2023-11-11 23:53:12 +01:00
|
|
|
const isLastPage = curPage === chapter.pageCount - 1;
|
|
|
|
|
const curPageDebounced = useDebounce(curPage, isLastPage ? 0 : 1000);
|
2023-02-12 16:12:17 +01:00
|
|
|
const [pageToScrollTo, setPageToScrollTo] = useState<number | undefined>(undefined);
|
2023-10-28 00:32:02 +02:00
|
|
|
const { setOverride, setTitle } = useContext(NavBarContext);
|
2023-04-05 13:37:12 +02:00
|
|
|
const [retrievingNextChapter, setRetrievingNextChapter] = useState(false);
|
2023-12-05 22:20:13 +01:00
|
|
|
const { data: mangaChaptersData } = requestManager.useGetMangaChapters(mangaId, { nextFetchPolicy: 'standby' });
|
|
|
|
|
const mangaChapters = mangaChaptersData?.chapters.nodes;
|
2021-05-24 12:16:05 -07:00
|
|
|
|
2023-02-08 18:19:26 +01:00
|
|
|
const { settings: defaultSettings, loading: areDefaultSettingsLoading } = useDefaultReaderSettings();
|
2023-01-06 17:34:16 +01:00
|
|
|
const [settings, setSettings] = useState(getReaderSettingsFor(manga, defaultSettings));
|
2021-05-17 01:38:59 +04:30
|
|
|
|
2023-11-19 21:17:35 +01:00
|
|
|
const { settings: metadataSettings } = useMetadataServerSettings();
|
|
|
|
|
|
2024-02-16 19:16:18 +01:00
|
|
|
const prevChapters = useMemo(
|
|
|
|
|
() =>
|
|
|
|
|
Chapters.getNextChapters(chapter, mangaChapters ?? [], {
|
|
|
|
|
offset: ChapterOffset.PREV,
|
|
|
|
|
skipDupe: settings.skipDupChapters,
|
|
|
|
|
}),
|
|
|
|
|
[chapter, mangaChapters, settings.skipDupChapters],
|
|
|
|
|
);
|
|
|
|
|
const nextChapters = useMemo(
|
|
|
|
|
() => Chapters.getNextChapters(chapter, mangaChapters ?? [], { skipDupe: settings.skipDupChapters }),
|
|
|
|
|
[chapter, mangaChapters, settings.skipDupChapters],
|
|
|
|
|
);
|
|
|
|
|
const prevChapter = useMemo(
|
|
|
|
|
() =>
|
|
|
|
|
Chapters.getNextChapter(chapter, mangaChapters ?? [], {
|
|
|
|
|
offset: ChapterOffset.PREV,
|
|
|
|
|
skipDupe: settings.skipDupChapters,
|
|
|
|
|
}),
|
|
|
|
|
[chapter, mangaChapters, settings.skipDupChapters],
|
|
|
|
|
);
|
|
|
|
|
const nextChapter = useMemo(
|
|
|
|
|
() =>
|
|
|
|
|
Chapters.getNextChapter(chapter, mangaChapters ?? [], {
|
|
|
|
|
skipDupe: settings.skipDupChapters,
|
|
|
|
|
}),
|
|
|
|
|
[chapter, mangaChapters, settings.skipDupChapters],
|
|
|
|
|
);
|
2023-12-05 22:20:13 +01:00
|
|
|
|
2024-02-16 19:16:18 +01:00
|
|
|
const updateChapter = (patch: UpdateChapterPatchInput) => {
|
2024-02-18 15:20:37 +01:00
|
|
|
if (chapter === initialChapter) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-05 22:20:13 +01:00
|
|
|
const getChapterIdToDelete = () => {
|
2024-02-16 19:16:18 +01:00
|
|
|
const isAutoDeletionEnabled = !!patch.isRead && !!metadataSettings.deleteChaptersWhileReading;
|
2023-12-05 22:20:13 +01:00
|
|
|
if (!isAutoDeletionEnabled || !mangaChapters) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-16 19:16:18 +01:00
|
|
|
const chapterToDelete = [chapter, ...prevChapters][metadataSettings.deleteChaptersWhileReading - 1];
|
2023-12-05 22:20:13 +01:00
|
|
|
|
2024-01-01 22:40:39 +01:00
|
|
|
if (!chapterToDelete) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-16 19:16:18 +01:00
|
|
|
// chapter has to exist in the cache since the reader fetches all chapters of the manga
|
|
|
|
|
const chapterToDeleteUpToDateData = Chapters.getFromCache<TChapter>(chapterToDelete.id)!;
|
2024-01-01 22:40:39 +01:00
|
|
|
|
2023-12-05 22:20:13 +01:00
|
|
|
const shouldDeleteChapter =
|
2024-02-16 19:16:18 +01:00
|
|
|
chapterToDeleteUpToDateData.isRead &&
|
|
|
|
|
Chapters.isAutoDeletable(chapterToDeleteUpToDateData, metadataSettings.deleteChaptersWithBookmark);
|
2023-12-05 22:20:13 +01:00
|
|
|
if (!shouldDeleteChapter) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return chapterToDelete.id;
|
|
|
|
|
};
|
2023-11-19 21:17:35 +01:00
|
|
|
|
2024-02-16 19:16:18 +01:00
|
|
|
const downloadAhead = () => {
|
|
|
|
|
const currentChapter = Chapters.getFromCache<TChapter>(chapter.id);
|
|
|
|
|
|
|
|
|
|
const inDownloadRange = (patch.lastPageRead ?? 0) / chapter.pageCount > 0.25;
|
|
|
|
|
const shouldCheckDownloadAhead =
|
|
|
|
|
isDownloadAheadEnabled && chapter.manga.inLibrary && !!currentChapter?.isDownloaded && inDownloadRange;
|
|
|
|
|
|
|
|
|
|
if (shouldCheckDownloadAhead) {
|
|
|
|
|
const nextChapterUpToDate = nextChapter ? Chapters.getFromCache<TChapter>(nextChapter.id) : null;
|
|
|
|
|
|
|
|
|
|
if (!nextChapterUpToDate?.isDownloaded) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const nextChaptersUpToDate = Chapters.getNonRead(nextChapters).map(
|
|
|
|
|
// the chapters have to be in the cache since the reader fetches the whole chapter list of the manga
|
|
|
|
|
(mangaChapter) => Chapters.getFromCache<TChapter>(mangaChapter.id)!,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const chapterIdsToDownload = nextChaptersUpToDate
|
|
|
|
|
// "settingsData" can't be undefined since this would not get executed otherwise
|
2024-02-17 20:24:02 +01:00
|
|
|
.slice(-downloadAheadLimit)
|
2024-02-16 19:16:18 +01:00
|
|
|
.filter((mangaChapter) => !mangaChapter.isDownloaded)
|
|
|
|
|
.map((mangaChapter) => mangaChapter.id)
|
|
|
|
|
.filter((id) => !Chapters.isDownloading(id));
|
2024-02-12 20:53:53 +01:00
|
|
|
|
2024-02-16 19:16:18 +01:00
|
|
|
if (!chapterIdsToDownload.length) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Chapters.download(chapterIdsToDownload!).catch(
|
|
|
|
|
defaultPromiseErrorHandler('Reader::updateChapter: shouldDownloadAhead'),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
downloadAhead();
|
2023-11-24 03:17:49 +01:00
|
|
|
|
|
|
|
|
requestManager
|
|
|
|
|
.updateChapter(chapter.id, {
|
|
|
|
|
...patch,
|
2023-12-05 22:20:13 +01:00
|
|
|
chapterIdToDelete: getChapterIdToDelete(),
|
2023-11-24 03:17:49 +01:00
|
|
|
})
|
2023-12-31 01:42:54 +01:00
|
|
|
.response.catch();
|
2023-11-19 21:17:35 +01:00
|
|
|
};
|
|
|
|
|
|
2024-01-21 13:33:13 -08:00
|
|
|
const setSettingValue = (key: keyof IReaderSettings, value: AllowedMetadataValueTypes) => {
|
2023-01-06 17:15:29 +01:00
|
|
|
setSettings({ ...settings, [key]: value });
|
2023-02-06 10:06:33 +01:00
|
|
|
requestUpdateMangaMetadata(manga, [[key, value]]).catch(() =>
|
2023-03-23 13:59:32 +01:00
|
|
|
makeToast(t('reader.settings.error.label.failed_to_save_settings'), 'warning'),
|
2023-02-06 10:06:33 +01:00
|
|
|
);
|
2023-01-06 17:15:29 +01:00
|
|
|
};
|
|
|
|
|
|
2023-04-05 13:37:12 +02:00
|
|
|
const openNextChapter = useCallback(
|
2024-02-18 15:20:30 +01:00
|
|
|
(offset: ChapterOffset) => {
|
|
|
|
|
const isOpenNextChapter = offset === ChapterOffset.NEXT;
|
|
|
|
|
const chapterToOpen = isOpenNextChapter ? nextChapter : prevChapter;
|
|
|
|
|
|
|
|
|
|
if (!chapterToOpen) {
|
|
|
|
|
makeToast(
|
|
|
|
|
t(
|
|
|
|
|
isOpenNextChapter
|
|
|
|
|
? 'reader.error.label.next_chapter_does_not_exist'
|
|
|
|
|
: 'reader.error.label.prev_chapter_does_not_exist',
|
|
|
|
|
),
|
|
|
|
|
'error',
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-05 13:37:12 +02:00
|
|
|
setRetrievingNextChapter(true);
|
2023-11-19 21:17:22 +01:00
|
|
|
setCurPage(0);
|
2024-02-16 19:16:18 +01:00
|
|
|
|
2024-02-18 15:20:30 +01:00
|
|
|
navigate(`/manga/${manga.id}/chapter/${chapterToOpen.sourceOrder}`, {
|
|
|
|
|
replace: true,
|
|
|
|
|
state: location.state,
|
|
|
|
|
});
|
2023-04-05 13:37:12 +02:00
|
|
|
|
2024-02-18 15:20:30 +01:00
|
|
|
setRetrievingNextChapter(false);
|
2023-04-05 13:37:12 +02:00
|
|
|
},
|
2024-02-18 15:20:30 +01:00
|
|
|
[manga.id, prevChapter?.id, nextChapter?.id],
|
2023-04-05 13:37:12 +02:00
|
|
|
);
|
|
|
|
|
|
2023-05-18 13:25:19 +02:00
|
|
|
useEffect(() => {
|
2023-09-30 16:55:35 +02:00
|
|
|
if (isLoading || !chapter) {
|
2023-11-19 21:17:22 +01:00
|
|
|
setCurPage(0);
|
2023-05-18 13:25:19 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-08 13:43:58 +02:00
|
|
|
setWasLastPageReadSet(true);
|
2023-05-18 13:25:19 +02:00
|
|
|
if (chapter.lastPageRead === chapter.pageCount - 1) {
|
|
|
|
|
// last page, also probably read = true, we will load the first page.
|
|
|
|
|
setCurPage(0);
|
|
|
|
|
} else setCurPage(chapter.lastPageRead);
|
2023-09-30 16:55:35 +02:00
|
|
|
}, [chapter, isLoading]);
|
2023-05-18 13:25:19 +02:00
|
|
|
|
2023-01-07 16:44:00 +01:00
|
|
|
useEffect(() => {
|
2023-09-30 16:55:35 +02:00
|
|
|
if (!manga?.title || chapter.name === t('global.label.loading')) {
|
2023-03-23 13:59:32 +01:00
|
|
|
setTitle(t('reader.title'));
|
2023-01-07 16:44:00 +01:00
|
|
|
} else {
|
2023-09-30 16:55:35 +02:00
|
|
|
setTitle(`${manga.title}: ${chapter.name}`);
|
2023-01-07 16:44:00 +01:00
|
|
|
}
|
2023-03-28 23:14:03 +02:00
|
|
|
}, [t, manga, chapter]);
|
2023-01-07 16:44:00 +01:00
|
|
|
|
2023-01-06 17:34:16 +01:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (!areDefaultSettingsLoading && !isMangaLoading) {
|
2023-12-31 01:42:54 +01:00
|
|
|
checkAndHandleMissingStoredReaderSettings(manga, 'manga', defaultSettings).catch(
|
|
|
|
|
defaultPromiseErrorHandler('Reader::checkAndHandleMissingStoredReaderSettings'),
|
|
|
|
|
);
|
2023-01-06 17:34:16 +01:00
|
|
|
setSettings(getReaderSettingsFor(manga, defaultSettings));
|
|
|
|
|
}
|
|
|
|
|
}, [areDefaultSettingsLoading, isMangaLoading]);
|
|
|
|
|
|
2023-01-06 17:15:29 +01:00
|
|
|
useEffect(() => {
|
2021-05-17 01:38:59 +04:30
|
|
|
// set the custom navbar
|
2023-02-06 10:06:33 +01:00
|
|
|
setOverride({
|
|
|
|
|
status: true,
|
|
|
|
|
value: (
|
|
|
|
|
<ReaderNavBar
|
|
|
|
|
settings={settings}
|
|
|
|
|
setSettingValue={setSettingValue}
|
|
|
|
|
manga={manga}
|
2023-09-30 16:55:35 +02:00
|
|
|
chapter={chapter}
|
2023-02-06 10:06:33 +01:00
|
|
|
curPage={curPage}
|
2023-02-12 16:12:17 +01:00
|
|
|
scrollToPage={setPageToScrollTo}
|
2023-04-05 13:37:12 +02:00
|
|
|
openNextChapter={openNextChapter}
|
|
|
|
|
retrievingNextChapter={retrievingNextChapter}
|
2023-02-06 10:06:33 +01:00
|
|
|
/>
|
|
|
|
|
),
|
|
|
|
|
});
|
2021-03-18 21:46:24 +03:30
|
|
|
|
|
|
|
|
// clean up for when we leave the reader
|
|
|
|
|
return () => setOverride({ status: false, value: <div /> });
|
2023-04-05 13:37:12 +02:00
|
|
|
}, [manga, chapter, settings, curPage, chapterIndex, retrievingNextChapter]);
|
2021-03-18 21:46:24 +03:30
|
|
|
|
2021-05-18 02:26:45 +04:30
|
|
|
useEffect(() => {
|
2023-06-08 13:43:58 +02:00
|
|
|
if (!wasLastPageReadSet) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-18 15:20:37 +01:00
|
|
|
if (chapter === initialChapter) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-08 13:46:55 +02:00
|
|
|
// do not mutate the chapter, this will cause the page to jump around due to always scrolling to the last read page
|
2023-11-11 23:53:12 +01:00
|
|
|
const updateLastPageRead = curPageDebounced !== -1;
|
|
|
|
|
const updateIsRead = curPageDebounced === chapter.pageCount - 1;
|
2023-11-19 21:17:35 +01:00
|
|
|
const shouldUpdateChapter = updateLastPageRead || updateIsRead;
|
|
|
|
|
if (!shouldUpdateChapter) {
|
|
|
|
|
return;
|
2021-05-18 02:26:45 +04:30
|
|
|
}
|
2023-11-19 21:17:35 +01:00
|
|
|
|
|
|
|
|
updateChapter({
|
|
|
|
|
lastPageRead: updateLastPageRead ? curPageDebounced : undefined,
|
|
|
|
|
isRead: updateIsRead ? true : undefined,
|
|
|
|
|
});
|
|
|
|
|
}, [curPageDebounced, isDownloadAheadEnabled]);
|
2021-05-18 02:26:45 +04:30
|
|
|
|
2024-02-16 19:16:18 +01:00
|
|
|
const loadNextChapter = useCallback(() => {
|
2023-11-19 21:17:35 +01:00
|
|
|
updateChapter({
|
|
|
|
|
lastPageRead: chapter.pageCount - 1,
|
|
|
|
|
isRead: true,
|
|
|
|
|
});
|
|
|
|
|
|
2024-02-18 15:20:30 +01:00
|
|
|
openNextChapter(ChapterOffset.NEXT);
|
|
|
|
|
}, [chapter.pageCount, openNextChapter]);
|
2021-05-18 01:10:28 +04:30
|
|
|
|
2024-02-16 19:16:18 +01:00
|
|
|
const loadPrevChapter = useCallback(() => {
|
2024-02-18 15:20:30 +01:00
|
|
|
openNextChapter(ChapterOffset.PREV);
|
|
|
|
|
}, [openNextChapter]);
|
2022-11-24 21:05:05 +01:00
|
|
|
|
2023-11-04 22:40:21 +01:00
|
|
|
if (isLoading) {
|
2022-11-24 21:05:05 +01:00
|
|
|
return (
|
2023-02-06 10:06:33 +01:00
|
|
|
<Box
|
|
|
|
|
sx={{
|
|
|
|
|
height: '100vh',
|
|
|
|
|
width: '100vw',
|
|
|
|
|
display: 'grid',
|
|
|
|
|
placeItems: 'center',
|
|
|
|
|
}}
|
2022-11-24 21:05:05 +01:00
|
|
|
>
|
|
|
|
|
<CircularProgress thickness={5} />
|
|
|
|
|
</Box>
|
|
|
|
|
);
|
|
|
|
|
}
|
2021-05-25 13:14:07 +04:30
|
|
|
|
2021-05-15 17:18:57 +04:30
|
|
|
const pages = range(chapter.pageCount).map((index) => ({
|
|
|
|
|
index,
|
2023-05-18 13:25:19 +02:00
|
|
|
src: requestManager.getChapterPageUrl(mangaId, chapterIndex, index),
|
2021-05-15 17:18:57 +04:30
|
|
|
}));
|
|
|
|
|
|
2021-05-15 23:22:37 +04:30
|
|
|
const ReaderComponent = getReaderComponent(settings.readerType);
|
|
|
|
|
|
2023-02-12 16:12:17 +01:00
|
|
|
// last page, also probably read = true, we will load the first page.
|
|
|
|
|
const initialPage = pageToScrollTo ?? (chapter.lastPageRead === chapter.pageCount - 1 ? 0 : chapter.lastPageRead);
|
|
|
|
|
|
2021-01-20 01:05:24 +03:30
|
|
|
return (
|
2023-02-12 16:12:17 +01:00
|
|
|
<Box
|
|
|
|
|
sx={{
|
2024-01-26 11:51:08 -08:00
|
|
|
display: 'flex',
|
|
|
|
|
flexDirection: 'column',
|
|
|
|
|
alignContent: 'center',
|
2024-02-21 23:39:12 +01:00
|
|
|
alignItems: 'center',
|
2024-01-26 11:51:08 -08:00
|
|
|
justifyContent: 'center',
|
2024-01-26 23:59:12 +01:00
|
|
|
minWidth: settings.staticNav
|
|
|
|
|
? 'calc((100vw - (100vw - 100%)) - 300px)'
|
|
|
|
|
: 'calc(100vw - (100vw - 100%))', // 100vw = width excluding scrollbar; 100% = width including scrollbar
|
2024-01-26 11:51:08 -08:00
|
|
|
minHeight: '100vh',
|
2023-02-12 16:12:17 +01:00
|
|
|
marginLeft: settings.staticNav ? '300px' : 'unset',
|
|
|
|
|
}}
|
|
|
|
|
>
|
2023-02-06 10:06:33 +01:00
|
|
|
<PageNumber settings={settings} curPage={curPage} pageCount={chapter.pageCount} />
|
2024-02-22 19:24:27 +01:00
|
|
|
<Box sx={{ alignSelf: 'stretch' }}>
|
|
|
|
|
<ReaderComponent
|
|
|
|
|
key={chapter.id}
|
|
|
|
|
pages={pages}
|
|
|
|
|
pageCount={chapter.pageCount}
|
|
|
|
|
setCurPage={setCurPage}
|
|
|
|
|
initialPage={initialPage}
|
|
|
|
|
curPage={curPage}
|
|
|
|
|
settings={settings}
|
|
|
|
|
manga={manga}
|
|
|
|
|
chapter={chapter}
|
|
|
|
|
nextChapter={loadNextChapter}
|
|
|
|
|
prevChapter={loadPrevChapter}
|
|
|
|
|
/>
|
|
|
|
|
</Box>
|
2021-12-04 10:29:36 +03:30
|
|
|
</Box>
|
2021-01-20 01:05:24 +03:30
|
|
|
);
|
|
|
|
|
}
|