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

119 lines
4.0 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';
import { makeStyles, Theme } from '@material-ui/core/styles';
2021-01-19 20:20:28 +03:30
import { useParams } from 'react-router-dom';
import { Virtuoso } from 'react-virtuoso';
2021-01-19 20:20:28 +03:30
import ChapterCard from '../components/ChapterCard';
import MangaDetails from '../components/MangaDetails';
2021-03-08 21:04:42 +03:30
import NavbarContext from '../context/NavbarContext';
import client from '../util/client';
2021-03-27 22:21:39 +04:30
import LoadingPlaceholder from '../components/LoadingPlaceholder';
2021-05-15 20:36:56 +04:30
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-24 21:26:55 +04:30
minHeight: '50vh',
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-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[]>([]);
2021-05-15 18:51:38 +04:30
const [fetchedChapters, setFetchedChapters] = useState(false);
const [noChaptersFound, setNoChaptersFound] = useState(false);
const [chapterUpdateTriggerer, setChapterUpdateTriggerer] = useState(0);
function triggerChaptersUpdate() {
setChapterUpdateTriggerer(chapterUpdateTriggerer + 1);
}
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(() => {
2021-05-15 20:36:56 +04:30
const shouldFetchOnline = fetchedChapters && chapterUpdateTriggerer === 0;
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 && fetchedChapters) {
makeToast('No chapters found', 'warning');
setNoChaptersFound(true);
2021-05-15 20:36:56 +04:30
}
setChapters(data);
})
2021-05-15 18:51:38 +04:30
.then(() => setFetchedChapters(true));
2021-05-15 20:36:56 +04:30
}, [chapters.length, fetchedChapters, 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 }}
/>
<LoadingPlaceholder
shouldRender={chapters.length > 0 || noChaptersFound}
>
<Virtuoso
style={{ // override Virtuoso default values and set them with class
height: 'undefined',
2021-05-24 21:26:55 +04:30
overflowY: 'visible',
}}
className={classes.chapters}
totalCount={chapters.length}
itemContent={(index:number) => (
<ChapterCard
chapter={chapters[index]}
triggerChaptersUpdate={triggerChaptersUpdate}
/>
)}
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
);
}