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/. */
|
|
|
|
|
|
2022-11-09 18:09:15 +01:00
|
|
|
import { Warning } from '@mui/icons-material';
|
2023-02-06 10:06:33 +01:00
|
|
|
import { CircularProgress, IconButton, Stack, Tooltip } from '@mui/material';
|
2021-12-13 19:52:56 +05:30
|
|
|
import { Box } from '@mui/system';
|
2022-11-21 01:33:45 +01:00
|
|
|
import NavbarContext, { useSetDefaultBackTo } from 'components/context/NavbarContext';
|
2022-11-09 18:09:15 +01:00
|
|
|
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';
|
2023-02-06 10:06:33 +01:00
|
|
|
import React, { useContext, useEffect, useRef } from 'react';
|
2023-03-23 13:59:32 +01:00
|
|
|
import { useTranslation } from 'react-i18next';
|
2021-12-13 19:52:56 +05:30
|
|
|
import { useParams } from 'react-router-dom';
|
2022-11-09 18:09:15 +01:00
|
|
|
import { useQuery } from 'util/client';
|
2023-02-24 16:40:37 +01:00
|
|
|
import { IManga } from 'typings';
|
2021-05-30 04:01:49 +04:30
|
|
|
|
2022-11-02 10:42:02 +01:00
|
|
|
const AUTOFETCH_AGE = 60 * 60 * 24; // 24 hours
|
|
|
|
|
|
2022-11-09 18:09:15 +01:00
|
|
|
const Manga: React.FC = () => {
|
2023-03-23 13:59:32 +01:00
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
2021-03-17 19:17:03 +03:30
|
|
|
const { setTitle } = useContext(NavbarContext);
|
2021-05-13 17:46:40 +04:30
|
|
|
const { id } = useParams<{ id: string }>();
|
2022-11-02 10:42:02 +01:00
|
|
|
const autofetchedRef = useRef(false);
|
|
|
|
|
|
2022-11-09 18:09:15 +01:00
|
|
|
const {
|
2023-02-06 10:06:33 +01:00
|
|
|
data: manga,
|
|
|
|
|
error,
|
|
|
|
|
loading,
|
|
|
|
|
isValidating,
|
|
|
|
|
mutate,
|
2022-11-09 18:09:15 +01:00
|
|
|
} = useQuery<IManga>(`/api/v1/manga/${id}/?onlineFetch=false`);
|
2021-01-19 20:20:28 +03:30
|
|
|
|
2022-11-09 18:09:15 +01:00
|
|
|
const [refresh, { loading: refreshing }] = useRefreshManga(id);
|
2021-05-30 04:01:49 +04:30
|
|
|
|
2023-02-06 10:06:33 +01:00
|
|
|
useSetDefaultBackTo(
|
2023-02-08 18:19:26 +01:00
|
|
|
manga?.inLibrary === false && manga.sourceId != null ? `/sources/${manga.sourceId}/popular` : '/library',
|
2023-02-06 10:06:33 +01:00
|
|
|
);
|
2022-11-21 01:33:45 +01:00
|
|
|
|
2021-01-19 21:02:57 +03:30
|
|
|
useEffect(() => {
|
2022-11-02 10:42:02 +01:00
|
|
|
// 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 (
|
2023-02-06 10:06:33 +01:00
|
|
|
manga.inLibrary &&
|
|
|
|
|
(manga.age > AUTOFETCH_AGE || manga.chaptersAge > AUTOFETCH_AGE) &&
|
|
|
|
|
autofetchedRef.current === false
|
2022-11-02 10:42:02 +01:00
|
|
|
) {
|
|
|
|
|
autofetchedRef.current = true;
|
2022-11-09 18:09:15 +01:00
|
|
|
refresh();
|
2021-05-15 13:43:26 +04:30
|
|
|
}
|
|
|
|
|
}, [manga]);
|
2021-01-19 21:02:57 +03:30
|
|
|
|
2022-11-02 10:42:02 +01:00
|
|
|
useEffect(() => {
|
2023-03-23 13:59:32 +01:00
|
|
|
setTitle(manga?.title ?? t('manga.title'));
|
2022-11-02 10:42:02 +01:00
|
|
|
}, [manga?.title]);
|
|
|
|
|
|
2022-11-09 18:09:15 +01:00
|
|
|
if (error && !manga) {
|
2023-03-23 13:59:32 +01:00
|
|
|
return <EmptyView message={t('manga.error.label.request_failure')} messageExtra={error.message ?? error} />;
|
2022-11-09 18:09:15 +01:00
|
|
|
}
|
2021-01-19 20:20:28 +03:30
|
|
|
return (
|
2021-11-17 16:58:39 +03:30
|
|
|
<Box sx={{ display: { md: 'flex' }, overflow: 'hidden' }}>
|
2022-11-09 18:09:15 +01:00
|
|
|
<NavbarToolbar>
|
|
|
|
|
<Stack direction="row" alignItems="center">
|
|
|
|
|
{error && !isValidating && !refreshing && (
|
2023-02-06 10:06:33 +01:00
|
|
|
<Tooltip
|
|
|
|
|
title={
|
|
|
|
|
<>
|
2023-03-23 13:59:32 +01:00
|
|
|
{t('manga.error.label.request_failure')}
|
2023-02-06 10:06:33 +01:00
|
|
|
<br />
|
|
|
|
|
{error.message ?? error}
|
|
|
|
|
</>
|
|
|
|
|
}
|
2022-11-09 18:09:15 +01:00
|
|
|
>
|
|
|
|
|
<IconButton onClick={() => mutate()}>
|
|
|
|
|
<Warning color="error" />
|
|
|
|
|
</IconButton>
|
|
|
|
|
</Tooltip>
|
|
|
|
|
)}
|
2023-02-06 10:06:33 +01:00
|
|
|
{manga && (refreshing || isValidating) && (
|
2022-11-09 18:09:15 +01:00
|
|
|
<IconButton disabled>
|
|
|
|
|
<CircularProgress size={16} />
|
|
|
|
|
</IconButton>
|
|
|
|
|
)}
|
2023-02-08 18:19:26 +01:00
|
|
|
{manga && <MangaToolbarMenu manga={manga} onRefresh={refresh} refreshing={refreshing} />}
|
2022-11-09 18:09:15 +01:00
|
|
|
</Stack>
|
|
|
|
|
</NavbarToolbar>
|
|
|
|
|
|
|
|
|
|
{loading && <LoadingPlaceholder />}
|
|
|
|
|
|
|
|
|
|
{manga && <MangaDetails manga={manga} />}
|
|
|
|
|
<ChapterList mangaId={id} />
|
2021-11-17 16:58:39 +03:30
|
|
|
</Box>
|
2021-01-19 20:20:28 +03:30
|
|
|
);
|
2022-11-09 18:09:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default Manga;
|