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-08-14 21:43:06 +02:00
|
|
|
import { useCallback, useContext, useEffect, 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';
|
|
|
|
|
import { ChapterOffset, IChapter, IManga, IMangaCard, IReaderSettings, ReaderType, TranslationKey } from '@/typings';
|
2023-08-15 13:54:29 +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';
|
|
|
|
|
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 NavbarContext from '@/components/context/NavbarContext';
|
|
|
|
|
import makeToast from '@/components/util/Toast';
|
2021-01-20 01:05:24 +03:30
|
|
|
|
2023-04-05 13:37:12 +02:00
|
|
|
const isDupChapter = async (chapterIndex: number, currentChapter: IChapter) => {
|
2023-06-04 00:50:58 +02:00
|
|
|
const nextChapter = await requestManager.getChapter(currentChapter.mangaId, chapterIndex).response;
|
2023-04-05 13:37:12 +02:00
|
|
|
|
|
|
|
|
return nextChapter.chapterNumber === currentChapter.chapterNumber;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* In case duplicated chapters should be skipped the function will check all next/prev chapters until
|
|
|
|
|
* - a non duplicated chapter was found
|
|
|
|
|
* - no prev/next chapter exists => chapter request will fail and error will be raised up
|
|
|
|
|
*/
|
|
|
|
|
const getOffsetChapter = async (
|
|
|
|
|
chapterIndex: number,
|
|
|
|
|
currentChapter: IChapter,
|
|
|
|
|
skipDupChapters: boolean,
|
|
|
|
|
offset: ChapterOffset,
|
|
|
|
|
): Promise<number> => {
|
|
|
|
|
const shouldSkipChapter = skipDupChapters && (await isDupChapter(chapterIndex, currentChapter));
|
|
|
|
|
if (shouldSkipChapter) {
|
|
|
|
|
return getOffsetChapter(chapterIndex + offset, currentChapter, skipDupChapters, offset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return chapterIndex;
|
|
|
|
|
};
|
|
|
|
|
|
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,
|
|
|
|
|
index: -1,
|
|
|
|
|
chapterCount: 0,
|
2022-11-24 21:05:05 +01:00
|
|
|
lastPageRead: 0,
|
2021-12-01 03:03:52 +03:30
|
|
|
name: 'Loading...',
|
2023-09-01 20:36:40 +02:00
|
|
|
} as IChapter;
|
2021-01-22 17:00:33 +03:30
|
|
|
|
2021-01-20 01:05:24 +03:30
|
|
|
export default 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-05-18 13:25:19 +02:00
|
|
|
const {
|
|
|
|
|
data: manga = {
|
|
|
|
|
id: +mangaId,
|
|
|
|
|
title: '',
|
|
|
|
|
thumbnailUrl: '',
|
|
|
|
|
genre: [],
|
|
|
|
|
inLibraryAt: 0,
|
|
|
|
|
lastReadAt: 0,
|
|
|
|
|
} as IMangaCard | IManga,
|
|
|
|
|
isLoading: isMangaLoading,
|
|
|
|
|
} = requestManager.useGetManga(mangaId);
|
2023-06-04 21:13:07 +02:00
|
|
|
const { data: chapter = initialChapter, isLoading: isChapterLoading } = requestManager.useGetChapter(
|
2023-05-18 13:25:19 +02:00
|
|
|
mangaId,
|
|
|
|
|
chapterIndex,
|
2023-06-08 13:56:25 +02:00
|
|
|
{ disableCache: true, revalidateOnFocus: false },
|
2023-05-18 13:25:19 +02:00
|
|
|
);
|
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-02-12 16:12:17 +01:00
|
|
|
const [pageToScrollTo, setPageToScrollTo] = useState<number | undefined>(undefined);
|
2021-03-18 21:46:24 +03:30
|
|
|
const { setOverride, setTitle } = useContext(NavbarContext);
|
2023-04-05 13:37:12 +02:00
|
|
|
const [retrievingNextChapter, setRetrievingNextChapter] = useState(false);
|
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-01-06 17:15:29 +01:00
|
|
|
const setSettingValue = (key: keyof IReaderSettings, value: string | boolean) => {
|
|
|
|
|
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(
|
|
|
|
|
async (offset: ChapterOffset, setHistory: (nextChapterIndex: number) => void) => {
|
|
|
|
|
setRetrievingNextChapter(true);
|
|
|
|
|
try {
|
|
|
|
|
setHistory(
|
|
|
|
|
await getOffsetChapter(
|
|
|
|
|
chapter.index + offset,
|
|
|
|
|
chapter as IChapter,
|
|
|
|
|
settings.skipDupChapters,
|
|
|
|
|
offset,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
const offsetToTranslationKeyMap: { [chapterOffset in ChapterOffset]: TranslationKey } = {
|
|
|
|
|
[ChapterOffset.PREV]: 'reader.error.label.unable_to_get_prev_chapter_skip_dup',
|
|
|
|
|
[ChapterOffset.NEXT]: 'reader.error.label.unable_to_get_next_chapter_skip_dup',
|
|
|
|
|
};
|
|
|
|
|
|
2023-07-04 21:53:25 +02:00
|
|
|
makeToast(t(offsetToTranslationKeyMap[offset]), 'error');
|
2023-04-05 13:37:12 +02:00
|
|
|
} finally {
|
|
|
|
|
setRetrievingNextChapter(false);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
[chapter, settings],
|
|
|
|
|
);
|
|
|
|
|
|
2023-05-18 13:25:19 +02:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (isChapterLoading || !chapter) {
|
|
|
|
|
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);
|
|
|
|
|
}, [chapter, isChapterLoading]);
|
|
|
|
|
|
2023-01-07 16:44:00 +01:00
|
|
|
useEffect(() => {
|
2023-03-23 13:59:32 +01:00
|
|
|
if (!manga?.title || (chapter as IChapter)?.name === t('global.label.loading')) {
|
|
|
|
|
setTitle(t('reader.title'));
|
2023-01-07 16:44:00 +01:00
|
|
|
} else {
|
|
|
|
|
setTitle(`${manga.title}: ${(chapter as IChapter).name}`);
|
|
|
|
|
}
|
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-02-08 18:19:26 +01:00
|
|
|
checkAndHandleMissingStoredReaderSettings(manga, 'manga', defaultSettings).catch(() => {});
|
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}
|
|
|
|
|
chapter={chapter as IChapter}
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
2021-05-18 02:26:45 +04:30
|
|
|
if (curPage !== -1) {
|
2023-09-01 20:36:40 +02:00
|
|
|
requestManager.updateChapter(chapter.id, { lastPageRead: curPage });
|
2021-05-18 02:26:45 +04:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (curPage === chapter.pageCount - 1) {
|
2023-09-01 20:36:40 +02:00
|
|
|
requestManager.updateChapter(chapter.id, { isRead: true });
|
2021-05-18 02:26:45 +04:30
|
|
|
}
|
|
|
|
|
}, [curPage]);
|
|
|
|
|
|
2022-11-24 21:05:05 +01:00
|
|
|
const nextChapter = useCallback(() => {
|
2021-05-18 01:10:28 +04:30
|
|
|
if (chapter.index < chapter.chapterCount) {
|
2023-09-01 20:36:40 +02:00
|
|
|
requestManager.updateChapter(chapter.id, {
|
2023-05-18 13:25:19 +02:00
|
|
|
lastPageRead: chapter.pageCount - 1,
|
2023-09-01 20:36:40 +02:00
|
|
|
isRead: true,
|
2023-05-18 13:25:19 +02:00
|
|
|
});
|
2021-05-18 02:26:45 +04:30
|
|
|
|
2023-04-05 13:37:12 +02:00
|
|
|
openNextChapter(ChapterOffset.NEXT, (nextChapterIndex) =>
|
2023-06-04 21:30:15 +02:00
|
|
|
navigate(`/manga/${manga.id}/chapter/${nextChapterIndex}`, {
|
|
|
|
|
replace: true,
|
|
|
|
|
state: location.state,
|
2023-04-05 13:37:12 +02:00
|
|
|
}),
|
|
|
|
|
);
|
2021-05-18 01:10:28 +04:30
|
|
|
}
|
2023-04-05 13:37:12 +02:00
|
|
|
}, [chapter.index, chapter.chapterCount, chapter.pageCount, manga.id, settings.skipDupChapters]);
|
2021-05-18 01:10:28 +04:30
|
|
|
|
2022-11-24 21:05:05 +01:00
|
|
|
const prevChapter = useCallback(() => {
|
2021-05-25 13:14:07 +04:30
|
|
|
if (chapter.index > 1) {
|
2023-04-05 13:37:12 +02:00
|
|
|
openNextChapter(ChapterOffset.PREV, (prevChapterIndex) =>
|
2023-06-04 21:30:15 +02:00
|
|
|
navigate(`/manga/${manga.id}/chapter/${prevChapterIndex}`, {
|
|
|
|
|
replace: true,
|
|
|
|
|
state: location.state,
|
2023-04-05 13:37:12 +02:00
|
|
|
}),
|
|
|
|
|
);
|
2021-05-25 13:14:07 +04:30
|
|
|
}
|
2023-04-05 13:37:12 +02:00
|
|
|
}, [chapter.index, manga.id, settings.skipDupChapters]);
|
2022-11-24 21:05:05 +01:00
|
|
|
|
|
|
|
|
// return spinner while chpater data is loading
|
|
|
|
|
if (chapter.pageCount === -1) {
|
|
|
|
|
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={{
|
|
|
|
|
width: settings.staticNav ? 'calc(100vw - 300px)' : '100vw',
|
|
|
|
|
marginLeft: settings.staticNav ? '300px' : 'unset',
|
|
|
|
|
}}
|
|
|
|
|
>
|
2023-02-06 10:06:33 +01:00
|
|
|
<PageNumber settings={settings} curPage={curPage} pageCount={chapter.pageCount} />
|
2021-05-15 23:22:37 +04:30
|
|
|
<ReaderComponent
|
2021-05-15 18:17:12 +04:30
|
|
|
pages={pages}
|
2021-05-15 23:22:37 +04:30
|
|
|
pageCount={chapter.pageCount}
|
2021-05-15 18:17:12 +04:30
|
|
|
setCurPage={setCurPage}
|
2023-02-12 16:12:17 +01:00
|
|
|
initialPage={initialPage}
|
2021-05-15 18:17:12 +04:30
|
|
|
curPage={curPage}
|
|
|
|
|
settings={settings}
|
2021-05-17 09:57:14 +02:00
|
|
|
manga={manga}
|
|
|
|
|
chapter={chapter}
|
2021-05-18 01:10:28 +04:30
|
|
|
nextChapter={nextChapter}
|
2021-05-25 13:14:07 +04:30
|
|
|
prevChapter={prevChapter}
|
2021-05-15 17:18:57 +04:30
|
|
|
/>
|
2021-12-04 10:29:36 +03:30
|
|
|
</Box>
|
2021-01-20 01:05:24 +03:30
|
|
|
);
|
|
|
|
|
}
|