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
|
|
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
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';
|
2021-05-18 01:10:28 +04:30
|
|
|
import { useHistory, useParams } from 'react-router-dom';
|
2021-10-30 16:10:34 +03:30
|
|
|
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';
|
2023-01-06 17:34:16 +01:00
|
|
|
import ReaderNavBar from 'components/navbar/ReaderNavBar';
|
2021-10-30 16:10:34 +03:30
|
|
|
import NavbarContext from 'components/context/NavbarContext';
|
2021-05-27 05:13:01 +04:30
|
|
|
import client from 'util/client';
|
|
|
|
|
import useLocalStorage from 'util/useLocalStorage';
|
2021-12-04 10:29:36 +03:30
|
|
|
import { Box } from '@mui/system';
|
2023-01-06 17:15:29 +01:00
|
|
|
import { requestUpdateMangaMetadata } from 'util/metadata';
|
2023-01-06 17:34:16 +01:00
|
|
|
import {
|
|
|
|
|
checkAndHandleMissingStoredReaderSettings,
|
|
|
|
|
getReaderSettingsFor,
|
|
|
|
|
useDefaultReaderSettings,
|
|
|
|
|
} from 'util/readerSettings';
|
2023-02-05 16:15:20 +01:00
|
|
|
import makeToast from 'components/util/Toast';
|
2023-02-24 16:40:37 +01:00
|
|
|
import { IChapter, IManga, IMangaCard, IPartialChapter, IReaderSettings, ReaderType } from 'typings';
|
2021-01-20 01:05:24 +03:30
|
|
|
|
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);
|
2021-12-01 03:03:52 +03:30
|
|
|
const initialChapter = () => ({
|
|
|
|
|
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() {
|
2021-05-18 01:10:28 +04:30
|
|
|
const history = useHistory();
|
2021-03-09 16:44:09 +03:30
|
|
|
|
2021-03-07 22:25:29 +03:30
|
|
|
const [serverAddress] = useLocalStorage<String>('serverBaseURL', '');
|
2021-01-22 17:00:33 +03:30
|
|
|
|
2023-02-06 10:06:33 +01:00
|
|
|
const { chapterIndex, mangaId } = useParams<{ chapterIndex: string; mangaId: string }>();
|
|
|
|
|
const [manga, setManga] = useState<IMangaCard | IManga>({
|
|
|
|
|
id: +mangaId,
|
|
|
|
|
title: '',
|
|
|
|
|
thumbnailUrl: '',
|
2023-02-16 11:26:09 +05:30
|
|
|
genre: [],
|
2023-03-04 22:21:39 +05:30
|
|
|
inLibraryAt: 0,
|
2023-03-05 12:26:50 +05:30
|
|
|
lastReadAt: 0,
|
2023-02-06 10:06:33 +01:00
|
|
|
});
|
2022-11-24 21:05:05 +01:00
|
|
|
const [chapter, setChapter] = useState<IChapter | IPartialChapter>(initialChapter());
|
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);
|
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));
|
|
|
|
|
const [isMangaLoading, setIsMangaLoading] = useState(true);
|
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(() =>
|
|
|
|
|
makeToast('Failed to save the reader settings to the server', 'warning'),
|
|
|
|
|
);
|
2023-01-06 17:15:29 +01:00
|
|
|
};
|
|
|
|
|
|
2023-01-07 16:44:00 +01:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (!manga?.title || (chapter as IChapter)?.name === 'Loading...') {
|
|
|
|
|
setTitle(`Reader - Manga ${mangaId} Chapter ${chapterIndex}`);
|
|
|
|
|
} else {
|
|
|
|
|
setTitle(`${manga.title}: ${(chapter as IChapter).name}`);
|
|
|
|
|
}
|
|
|
|
|
}, [manga, chapter]);
|
|
|
|
|
|
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-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 /> });
|
2021-03-23 03:50:55 +04:30
|
|
|
}, [manga, chapter, settings, curPage, chapterIndex]);
|
2021-03-18 21:46:24 +03:30
|
|
|
|
|
|
|
|
useEffect(() => {
|
2023-01-06 17:34:16 +01:00
|
|
|
setIsMangaLoading(true);
|
2023-02-06 10:06:33 +01:00
|
|
|
client
|
|
|
|
|
.get(`/api/v1/manga/${mangaId}/`)
|
2021-03-18 21:46:24 +03:30
|
|
|
.then((response) => response.data)
|
|
|
|
|
.then((data: IManga) => {
|
|
|
|
|
setManga(data);
|
2023-01-06 17:34:16 +01:00
|
|
|
setIsMangaLoading(false);
|
2021-03-18 21:46:24 +03:30
|
|
|
});
|
2023-01-06 17:15:29 +01:00
|
|
|
}, [mangaId]);
|
2021-01-20 01:05:24 +03:30
|
|
|
|
|
|
|
|
useEffect(() => {
|
2021-03-23 03:50:55 +04:30
|
|
|
setChapter(initialChapter);
|
2023-02-06 10:06:33 +01:00
|
|
|
client
|
|
|
|
|
.get(`/api/v1/manga/${mangaId}/chapter/${chapterIndex}`)
|
2021-03-07 22:25:29 +03:30
|
|
|
.then((response) => response.data)
|
2023-02-06 10:06:33 +01:00
|
|
|
.then((data: IChapter) => {
|
2021-03-23 03:50:55 +04:30
|
|
|
setChapter(data);
|
2021-11-15 12:53:18 +03:30
|
|
|
|
|
|
|
|
if (data.lastPageRead === data.pageCount - 1) {
|
|
|
|
|
// last page, also probably read = true, we will load the first page.
|
|
|
|
|
setCurPage(0);
|
|
|
|
|
} else setCurPage(data.lastPageRead);
|
2021-01-22 17:00:33 +03:30
|
|
|
});
|
2021-03-23 03:50:55 +04:30
|
|
|
}, [chapterIndex]);
|
2021-01-20 01:05:24 +03:30
|
|
|
|
2021-05-18 02:26:45 +04:30
|
|
|
useEffect(() => {
|
|
|
|
|
if (curPage !== -1) {
|
|
|
|
|
const formData = new FormData();
|
|
|
|
|
formData.append('lastPageRead', curPage.toString());
|
|
|
|
|
client.patch(`/api/v1/manga/${manga.id}/chapter/${chapter.index}`, formData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (curPage === chapter.pageCount - 1) {
|
|
|
|
|
const formDataRead = new FormData();
|
|
|
|
|
formDataRead.append('read', 'true');
|
|
|
|
|
client.patch(`/api/v1/manga/${manga.id}/chapter/${chapter.index}`, formDataRead);
|
|
|
|
|
}
|
|
|
|
|
}, [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) {
|
2021-05-18 02:26:45 +04:30
|
|
|
const formData = new FormData();
|
|
|
|
|
formData.append('lastPageRead', `${chapter.pageCount - 1}`);
|
|
|
|
|
formData.append('read', 'true');
|
|
|
|
|
client.patch(`/api/v1/manga/${manga.id}/chapter/${chapter.index}`, formData);
|
|
|
|
|
|
2023-02-06 10:06:33 +01:00
|
|
|
history.replace({
|
|
|
|
|
pathname: `/manga/${manga.id}/chapter/${chapter.index + 1}`,
|
|
|
|
|
state: history.location.state,
|
|
|
|
|
});
|
2021-05-18 01:10:28 +04:30
|
|
|
}
|
2022-11-24 21:05:05 +01:00
|
|
|
}, [chapter.index, chapter.chapterCount, chapter.pageCount, manga.id]);
|
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-02-06 10:06:33 +01:00
|
|
|
history.replace({
|
|
|
|
|
pathname: `/manga/${manga.id}/chapter/${chapter.index - 1}`,
|
|
|
|
|
state: history.location.state,
|
|
|
|
|
});
|
2021-05-25 13:14:07 +04:30
|
|
|
}
|
2022-11-24 21:05:05 +01:00
|
|
|
}, [chapter.index, manga.id]);
|
|
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
|
src: `${serverAddress}/api/v1/manga/${mangaId}/chapter/${chapterIndex}/page/${index}`,
|
|
|
|
|
}));
|
|
|
|
|
|
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
|
|
|
);
|
|
|
|
|
}
|