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

106 lines
3.7 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/. */
import { Warning } from '@mui/icons-material';
import {
CircularProgress, IconButton, Stack, Tooltip,
} from '@mui/material';
2021-12-13 19:52:56 +05:30
import { Box } from '@mui/system';
2021-10-30 16:10:34 +03:30
import NavbarContext from 'components/context/NavbarContext';
import ChapterList from 'components/manga/ChapterList';
import { useRefreshManga } from 'components/manga/hooks';
import MangaDetails from 'components/manga/MangaDetails';
import MangaToolbarMenu from 'components/manga/MangaToolbarMenu';
import { NavbarToolbar } from 'components/navbar/DefaultNavBar';
import EmptyView from 'components/util/EmptyView';
2021-10-30 16:10:34 +03:30
import LoadingPlaceholder from 'components/util/LoadingPlaceholder';
import React, {
useContext, useEffect, useRef,
} from 'react';
2021-12-13 19:52:56 +05:30
import { useParams } from 'react-router-dom';
import { useQuery } from 'util/client';
2021-05-30 04:01:49 +04:30
const AUTOFETCH_AGE = 60 * 60 * 24; // 24 hours
const Manga: React.FC = () => {
const { setTitle } = useContext(NavbarContext);
const { id } = useParams<{ id: string }>();
const autofetchedRef = useRef(false);
const {
data: manga, error, loading, isValidating, mutate,
} = useQuery<IManga>(`/api/v1/manga/${id}/?onlineFetch=false`);
2021-01-19 20:20:28 +03:30
const [refresh, { loading: refreshing }] = useRefreshManga(id);
2021-05-30 04:01:49 +04:30
2021-01-19 21:02:57 +03:30
useEffect(() => {
// Automatically fetch manga from source if data is older then 24 hours
// Automatic fetch is done only once, to prevent issues when server does
// not update age for some reason (ie. error on source side)
if (manga == null) return;
if (
manga.inLibrary
&& (manga.age > AUTOFETCH_AGE || manga.chaptersAge > AUTOFETCH_AGE)
&& autofetchedRef.current === false
) {
autofetchedRef.current = true;
refresh();
}
}, [manga]);
2021-01-19 21:02:57 +03:30
useEffect(() => {
setTitle(manga?.title ?? 'Manga');
}, [manga?.title]);
if (error && !manga) {
return (
<EmptyView message="Could not load manga" messageExtra={error.message ?? error} />
);
}
2021-01-19 20:20:28 +03:30
return (
<Box sx={{ display: { md: 'flex' }, overflow: 'hidden' }}>
<NavbarToolbar>
<Stack direction="row" alignItems="center">
{error && !isValidating && !refreshing && (
<Tooltip title={(
<>
Could not fetch manga data
<br />
{error.message ?? error}
</>
)}
>
<IconButton onClick={() => mutate()}>
<Warning color="error" />
</IconButton>
</Tooltip>
)}
{(manga && (refreshing || isValidating)) && (
<IconButton disabled>
<CircularProgress size={16} />
</IconButton>
)}
{manga && (
<MangaToolbarMenu
manga={manga}
onRefresh={refresh}
refreshing={refreshing}
/>
)}
</Stack>
</NavbarToolbar>
{loading && <LoadingPlaceholder />}
{manga && <MangaDetails manga={manga} />}
<ChapterList mangaId={id} />
</Box>
2021-01-19 20:20:28 +03:30
);
};
export default Manga;