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-01-22 17:00:33 +03:30
|
|
|
import React, { useEffect, useState, useContext } from 'react';
|
2021-09-09 17:51:22 +04:30
|
|
|
import { Theme } from '@mui/material/styles';
|
|
|
|
|
import makeStyles from '@mui/styles/makeStyles';
|
2021-01-19 20:20:28 +03:30
|
|
|
import { useParams } from 'react-router-dom';
|
2021-05-13 17:46:40 +04:30
|
|
|
import { Virtuoso } from 'react-virtuoso';
|
2021-05-27 05:13:01 +04:30
|
|
|
import ChapterCard from 'components/manga/ChapterCard';
|
|
|
|
|
import MangaDetails from 'components/manga/MangaDetails';
|
|
|
|
|
import NavbarContext from 'context/NavbarContext';
|
|
|
|
|
import client from 'util/client';
|
|
|
|
|
import LoadingPlaceholder from 'components/LoadingPlaceholder';
|
|
|
|
|
import makeToast from 'components/Toast';
|
2021-01-19 20:20:28 +03:30
|
|
|
|
2021-03-18 22:55:17 +03:30
|
|
|
const useStyles = makeStyles((theme: Theme) => ({
|
|
|
|
|
root: {
|
|
|
|
|
[theme.breakpoints.up('md')]: {
|
|
|
|
|
display: 'flex',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
2021-03-19 14:52:20 +03:30
|
|
|
chapters: {
|
|
|
|
|
listStyle: 'none',
|
|
|
|
|
padding: 0,
|
2021-05-27 17:13:22 +04:30
|
|
|
minHeight: '200px',
|
2021-03-19 14:52:20 +03:30
|
|
|
[theme.breakpoints.up('md')]: {
|
2021-03-27 22:21:39 +04:30
|
|
|
width: '50vw',
|
|
|
|
|
height: 'calc(100vh - 64px)',
|
|
|
|
|
margin: 0,
|
2021-03-19 14:52:20 +03:30
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
2021-03-18 22:55:17 +03:30
|
|
|
loading: {
|
|
|
|
|
margin: '10px 0',
|
|
|
|
|
display: 'flex',
|
|
|
|
|
justifyContent: 'center',
|
|
|
|
|
},
|
|
|
|
|
}));
|
|
|
|
|
|
2021-05-30 04:01:49 +04:30
|
|
|
const baseWebsocketUrl = JSON.parse(window.localStorage.getItem('serverBaseURL')!).replace('http', 'ws');
|
|
|
|
|
const initialQueue = {
|
|
|
|
|
status: 'Stopped',
|
|
|
|
|
queue: [],
|
|
|
|
|
} as IQueue;
|
|
|
|
|
|
2021-01-19 20:20:28 +03:30
|
|
|
export default function Manga() {
|
2021-03-18 22:55:17 +03:30
|
|
|
const classes = useStyles();
|
|
|
|
|
|
2021-03-17 19:17:03 +03:30
|
|
|
const { setTitle } = useContext(NavbarContext);
|
|
|
|
|
useEffect(() => { setTitle('Manga'); }, []); // delegate setting topbar action to MangaDetails
|
2021-03-09 16:44:09 +03:30
|
|
|
|
2021-05-13 17:46:40 +04:30
|
|
|
const { id } = useParams<{ id: string }>();
|
2021-01-19 20:20:28 +03:30
|
|
|
|
2021-01-19 21:02:57 +03:30
|
|
|
const [manga, setManga] = useState<IManga>();
|
|
|
|
|
const [chapters, setChapters] = useState<IChapter[]>([]);
|
2021-05-16 01:07:48 +04:30
|
|
|
const [noChaptersFound, setNoChaptersFound] = useState(false);
|
2021-05-15 13:43:26 +04:30
|
|
|
const [chapterUpdateTriggerer, setChapterUpdateTriggerer] = useState(0);
|
2021-09-27 18:29:05 +03:30
|
|
|
const [fetchedOnline, setFetchedOnline] = useState(false);
|
|
|
|
|
const [fetchedOffline, setFetchedOffline] = useState(false);
|
2021-05-15 13:43:26 +04:30
|
|
|
|
2021-05-30 04:01:49 +04:30
|
|
|
const [, setWsClient] = useState<WebSocket>();
|
|
|
|
|
const [{ queue }, setQueueState] = useState<IQueue>(initialQueue);
|
|
|
|
|
|
2021-05-15 13:43:26 +04:30
|
|
|
function triggerChaptersUpdate() {
|
|
|
|
|
setChapterUpdateTriggerer(chapterUpdateTriggerer + 1);
|
|
|
|
|
}
|
2021-01-19 21:02:57 +03:30
|
|
|
|
2021-05-30 04:01:49 +04:30
|
|
|
useEffect(() => {
|
|
|
|
|
const wsc = new WebSocket(`${baseWebsocketUrl}/api/v1/downloads`);
|
|
|
|
|
wsc.onmessage = (e) => {
|
|
|
|
|
const data = JSON.parse(e.data) as IQueue;
|
|
|
|
|
setQueueState(data);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
setWsClient(wsc);
|
|
|
|
|
|
|
|
|
|
return () => wsc.close();
|
2021-09-17 01:26:26 +04:30
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
triggerChaptersUpdate();
|
2021-05-30 04:01:49 +04:30
|
|
|
}, [queue.length]);
|
|
|
|
|
|
2021-09-17 01:26:26 +04:30
|
|
|
const downloadStatusStringFor = (chapter: IChapter) => {
|
2021-05-30 04:01:49 +04:30
|
|
|
let rtn = '';
|
|
|
|
|
if (chapter.downloaded) {
|
|
|
|
|
rtn = ' • Downloaded';
|
|
|
|
|
}
|
|
|
|
|
queue.forEach((q) => {
|
|
|
|
|
if (chapter.index === q.chapterIndex && chapter.mangaId === q.mangaId) {
|
2021-09-17 00:54:46 +04:30
|
|
|
rtn = ` • Downloading (${(q.progress * 100).toFixed(2)}%)`;
|
2021-05-30 04:01:49 +04:30
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return rtn;
|
|
|
|
|
};
|
|
|
|
|
|
2021-01-19 21:02:57 +03:30
|
|
|
useEffect(() => {
|
2021-05-15 13:43:26 +04:30
|
|
|
if (manga === undefined || !manga.freshData) {
|
|
|
|
|
client.get(`/api/v1/manga/${id}/?onlineFetch=${manga !== undefined}`)
|
|
|
|
|
.then((response) => response.data)
|
|
|
|
|
.then((data: IManga) => {
|
|
|
|
|
setManga(data);
|
|
|
|
|
setTitle(data.title);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}, [manga]);
|
2021-01-19 21:02:57 +03:30
|
|
|
|
|
|
|
|
useEffect(() => {
|
2021-09-27 18:29:05 +03:30
|
|
|
const shouldFetchOnline = fetchedOffline && !fetchedOnline;
|
|
|
|
|
|
2021-05-15 13:43:26 +04:30
|
|
|
client.get(`/api/v1/manga/${id}/chapters?onlineFetch=${shouldFetchOnline}`)
|
2021-03-07 22:25:29 +03:30
|
|
|
.then((response) => response.data)
|
2021-05-15 20:36:56 +04:30
|
|
|
.then((data) => {
|
2021-09-27 18:29:05 +03:30
|
|
|
if (data.length === 0 && fetchedOffline) {
|
2021-05-15 20:36:56 +04:30
|
|
|
makeToast('No chapters found', 'warning');
|
2021-05-16 01:07:48 +04:30
|
|
|
setNoChaptersFound(true);
|
2021-05-15 20:36:56 +04:30
|
|
|
}
|
|
|
|
|
setChapters(data);
|
|
|
|
|
})
|
2021-10-10 23:00:03 +03:30
|
|
|
.then(() => {
|
|
|
|
|
if (shouldFetchOnline) {
|
|
|
|
|
setFetchedOnline(true);
|
|
|
|
|
} else setFetchedOffline(true);
|
|
|
|
|
});
|
2021-10-10 23:19:42 +03:30
|
|
|
}, [fetchedOnline, fetchedOffline, chapterUpdateTriggerer]);
|
2021-01-19 21:02:57 +03:30
|
|
|
|
2021-01-19 20:20:28 +03:30
|
|
|
return (
|
2021-03-18 22:55:17 +03:30
|
|
|
<div className={classes.root}>
|
2021-03-27 22:21:39 +04:30
|
|
|
<LoadingPlaceholder
|
|
|
|
|
shouldRender={manga !== undefined}
|
|
|
|
|
component={MangaDetails}
|
|
|
|
|
componentProps={{ manga }}
|
|
|
|
|
/>
|
2021-05-13 17:46:40 +04:30
|
|
|
|
|
|
|
|
<LoadingPlaceholder
|
2021-05-16 01:07:48 +04:30
|
|
|
shouldRender={chapters.length > 0 || noChaptersFound}
|
2021-05-13 17:46:40 +04:30
|
|
|
>
|
|
|
|
|
<Virtuoso
|
2021-05-13 18:28:30 +04:30
|
|
|
style={{ // override Virtuoso default values and set them with class
|
|
|
|
|
height: 'undefined',
|
2021-05-27 17:13:22 +04:30
|
|
|
overflowY: window.innerWidth < 960 ? 'visible' : 'auto',
|
2021-05-13 18:28:30 +04:30
|
|
|
}}
|
2021-05-13 17:46:40 +04:30
|
|
|
className={classes.chapters}
|
|
|
|
|
totalCount={chapters.length}
|
2021-05-16 01:07:48 +04:30
|
|
|
itemContent={(index:number) => (
|
|
|
|
|
<ChapterCard
|
|
|
|
|
chapter={chapters[index]}
|
2021-09-17 01:26:26 +04:30
|
|
|
downloadStatusString={downloadStatusStringFor(chapters[index])}
|
2021-05-16 01:07:48 +04:30
|
|
|
triggerChaptersUpdate={triggerChaptersUpdate}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2021-05-13 17:46:40 +04:30
|
|
|
useWindowScroll={window.innerWidth < 960}
|
|
|
|
|
overscan={window.innerHeight * 0.5}
|
|
|
|
|
/>
|
|
|
|
|
</LoadingPlaceholder>
|
|
|
|
|
|
2021-03-18 22:55:17 +03:30
|
|
|
</div>
|
2021-01-19 20:20:28 +03:30
|
|
|
);
|
|
|
|
|
}
|