Files
suwayomi-material-you-webui/src/screens/Reader.tsx
T

294 lines
11 KiB
TypeScript
Raw Normal View History

/*
* 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
* 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-02-06 10:06:33 +01:00
import React, { useCallback, useContext, useEffect, useState } from 'react';
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';
import requestManager from '@/lib/RequestManager';
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
const isDupChapter = async (chapterIndex: number, currentChapter: IChapter) => {
const nextChapter = await requestManager.getChapter(currentChapter.mangaId, chapterIndex).response;
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);
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...',
};
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();
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);
const { data: chapter = initialChapter, isLoading: isChapterLoading } = requestManager.useGetChapter(
2023-05-18 13:25:19 +02:00
mangaId,
chapterIndex,
{ disableCache: true, revalidateOnFocus: false },
2023-05-18 13:25:19 +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);
const [retrievingNextChapter, setRetrievingNextChapter] = useState(false);
2023-02-08 18:19:26 +01:00
const { settings: defaultSettings, loading: areDefaultSettingsLoading } = useDefaultReaderSettings();
const [settings, setSettings] = useState(getReaderSettingsFor(manga, defaultSettings));
2021-05-17 01:38:59 +04:30
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
);
};
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',
};
makeToast(t(offsetToTranslationKeyMap[offset]) as string, 'error');
} finally {
setRetrievingNextChapter(false);
}
},
[chapter, settings],
);
2023-05-18 13:25:19 +02:00
useEffect(() => {
if (isChapterLoading || !chapter) {
return;
}
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}`);
}
}, [t, manga, chapter]);
2023-01-07 16:44:00 +01:00
useEffect(() => {
if (!areDefaultSettingsLoading && !isMangaLoading) {
2023-02-08 18:19:26 +01:00
checkAndHandleMissingStoredReaderSettings(manga, 'manga', defaultSettings).catch(() => {});
setSettings(getReaderSettingsFor(manga, defaultSettings));
}
}, [areDefaultSettingsLoading, isMangaLoading]);
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}
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 /> });
}, [manga, chapter, settings, curPage, chapterIndex, retrievingNextChapter]);
2021-03-18 21:46:24 +03:30
2021-05-18 02:26:45 +04:30
useEffect(() => {
if (!wasLastPageReadSet) {
return;
}
// 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-05-18 13:25:19 +02:00
requestManager.updateChapter(manga.id, chapter.index, { lastPageRead: curPage });
2021-05-18 02:26:45 +04:30
}
if (curPage === chapter.pageCount - 1) {
2023-05-18 13:25:19 +02:00
requestManager.updateChapter(manga.id, chapter.index, { read: true });
2021-05-18 02:26:45 +04:30
}
}, [curPage]);
2022-11-24 21:05:05 +01:00
const nextChapter = useCallback(() => {
if (chapter.index < chapter.chapterCount) {
2023-05-18 13:25:19 +02:00
requestManager.updateChapter(manga.id, chapter.index, {
lastPageRead: chapter.pageCount - 1,
read: true,
});
2022-11-24 21:05:05 +01:00
openNextChapter(ChapterOffset.NEXT, (nextChapterIndex) =>
navigate(`/manga/${manga.id}/chapter/${nextChapterIndex}`, {
replace: true,
state: location.state,
}),
);
2022-11-24 21:05:05 +01:00
}
}, [chapter.index, chapter.chapterCount, chapter.pageCount, manga.id, settings.skipDupChapters]);
2022-11-24 21:05:05 +01:00
const prevChapter = useCallback(() => {
if (chapter.index > 1) {
openNextChapter(ChapterOffset.PREV, (prevChapterIndex) =>
navigate(`/manga/${manga.id}/chapter/${prevChapterIndex}`, {
replace: true,
state: location.state,
}),
);
2022-11-24 21:05:05 +01:00
}
}, [chapter.index, manga.id, settings.skipDupChapters]);
2022-11-24 21:05:05 +01:00
2021-05-25 13:14:07 +04:30
// return spinner while chpater data is loading
2021-03-23 03:50:55 +04:30
if (chapter.pageCount === -1) {
2021-02-04 03:42:30 +03:30
return (
2023-02-06 10:06:33 +01:00
<Box
sx={{
height: '100vh',
width: '100vw',
display: 'grid',
placeItems: 'center',
}}
2021-12-04 10:29:36 +03:30
>
2021-03-18 21:46:24 +03:30
<CircularProgress thickness={5} />
2021-12-04 10:29:36 +03:30
</Box>
2021-02-04 03:42:30 +03:30
);
2021-01-20 01:05:24 +03: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}
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
);
}