Files
suwayomi-material-you-webui/src/screens/Manga.tsx

176 lines
6.2 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-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';
import { Link, useParams } from 'react-router-dom';
import { Virtuoso } from 'react-virtuoso';
2021-10-30 16:10:34 +03:30
import ChapterCard from 'components/ChapterCard';
import MangaDetails from 'components/MangaDetails';
import NavbarContext from 'components/context/NavbarContext';
import client from 'util/client';
2021-10-30 16:10:34 +03:30
import LoadingPlaceholder from 'components/util/LoadingPlaceholder';
import makeToast from 'components/util/Toast';
import { Fab } from '@mui/material';
import PlayArrow from '@mui/icons-material/PlayArrow';
import { Box } from '@mui/system';
2021-01-19 20:20:28 +03:30
2021-03-18 22:55:17 +03:30
const useStyles = makeStyles((theme: Theme) => ({
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
}));
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();
const { setTitle } = useContext(NavbarContext);
useEffect(() => { setTitle('Manga'); }, []); // delegate setting topbar action to MangaDetails
2021-03-09 16:44:09 +03: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[]>([]);
const [noChaptersFound, setNoChaptersFound] = useState(false);
const [chapterUpdateTriggerer, setChapterUpdateTriggerer] = useState(0);
const [fetchedOnline, setFetchedOnline] = useState(false);
const [fetchedOffline, setFetchedOffline] = useState(false);
const [firstUnreadChapter, setFirstUnreadChapter] = useState<IChapter>();
2021-05-30 04:01:49 +04:30
const [, setWsClient] = useState<WebSocket>();
const [{ queue }, setQueueState] = useState<IQueue>(initialQueue);
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();
}, []);
useEffect(() => {
triggerChaptersUpdate();
2021-05-30 04:01:49 +04:30
}, [queue.length]);
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(() => {
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(() => {
const shouldFetchOnline = fetchedOffline && !fetchedOnline && (chapterUpdateTriggerer < 2);
client.get(`/api/v1/manga/${id}/chapters?onlineFetch=${shouldFetchOnline}`)
.then((response) => response.data)
2021-05-15 20:36:56 +04:30
.then((data) => {
if (data.length === 0 && fetchedOffline) {
2021-05-15 20:36:56 +04:30
makeToast('No chapters found', 'warning');
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
useEffect(() => {
const a = [...chapters].reverse().find((chp) => !chp.read);
setFirstUnreadChapter(a);
}, [chapters]);
const ResumeFab = () => (firstUnreadChapter === undefined ? null
: (
<Fab
2021-11-09 23:04:03 +03:30
sx={{ position: 'fixed', bottom: '2em', right: '3em' }}
component={Link}
variant="extended"
color="primary"
to={`/manga/${id}/chapter/${firstUnreadChapter.index}/page/${firstUnreadChapter.lastPageRead}`}
>
<PlayArrow />
{firstUnreadChapter.index === 1 ? 'Start' : 'Resume' }
</Fab>
));
2021-01-19 20:20:28 +03:30
return (
<Box sx={{ display: { md: 'flex' }, overflow: 'hidden' }}>
2021-03-27 22:21:39 +04:30
<LoadingPlaceholder
shouldRender={manga !== undefined}
component={MangaDetails}
componentProps={{ manga }}
/>
<LoadingPlaceholder
shouldRender={chapters.length > 0 || noChaptersFound}
>
<Virtuoso
style={{ // override Virtuoso default values and set them with class
height: 'undefined',
overflowY: window.innerWidth < 900 ? 'visible' : 'auto',
}}
className={classes.chapters}
totalCount={chapters.length}
itemContent={(index:number) => (
<ChapterCard
chapter={chapters[index]}
downloadStatusString={downloadStatusStringFor(chapters[index])}
triggerChaptersUpdate={triggerChaptersUpdate}
/>
)}
useWindowScroll={window.innerWidth < 900}
overscan={window.innerHeight * 0.5}
/>
</LoadingPlaceholder>
<ResumeFab />
</Box>
2021-01-19 20:20:28 +03:30
);
}