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

122 lines
4.3 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-26 23:32:12 +03:30
import Warning from '@mui/icons-material/Warning';
import CircularProgress from '@mui/material/CircularProgress';
import IconButton from '@mui/material/IconButton';
import Stack from '@mui/material/Stack';
import Tooltip from '@mui/material/Tooltip';
import Box from '@mui/material/Box';
import React, { useContext, useEffect, useRef } from 'react';
import { useTranslation } from 'react-i18next';
2021-12-13 19:52:56 +05:30
import { useParams } from 'react-router-dom';
2023-09-08 00:44:01 +02:00
import { isNetworkRequestInFlight } from '@apollo/client/core/networkStatus';
2023-10-28 00:32:02 +02:00
import { requestManager } from '@/lib/requests/RequestManager.ts';
import { NavBarContext } from '@/components/context/NavbarContext';
import { ChapterList } from '@/components/chapter/ChapterList.tsx';
import { useRefreshManga } from '@/components/manga/useRefreshManga.ts';
2023-10-28 00:32:02 +02:00
import { MangaDetails } from '@/components/manga/MangaDetails';
import { MangaToolbarMenu } from '@/components/manga/MangaToolbarMenu';
2024-04-29 15:29:33 +02:00
import { EmptyViewAbsoluteCentered } from '@/components/util/EmptyViewAbsoluteCentered.tsx';
2023-10-28 00:32:02 +02:00
import { LoadingPlaceholder } from '@/components/util/LoadingPlaceholder';
2024-07-08 18:02:50 +02:00
import { GetMangaScreenQuery } from '@/lib/graphql/generated/graphql.ts';
import { GET_MANGA_SCREEN } from '@/lib/graphql/queries/MangaQuery.ts';
2021-05-30 04:01:49 +04:30
2023-10-28 00:32:02 +02:00
export const Manga: React.FC = () => {
const { t } = useTranslation();
2023-10-28 00:32:02 +02:00
const { setTitle, setAction } = useContext(NavBarContext);
const { id } = useParams<{ id: string }>();
const autofetchedRef = useRef(false);
2024-07-08 18:02:50 +02:00
const {
data,
error: mangaError,
loading: isLoading,
networkStatus,
refetch,
} = requestManager.useGetManga<GetMangaScreenQuery>(GET_MANGA_SCREEN, id);
2023-09-08 00:44:01 +02:00
const isValidating = isNetworkRequestInFlight(networkStatus);
2023-10-15 16:03:08 +02:00
const manga = data?.manga;
2021-01-19 20:20:28 +03:30
const [refresh, { loading: refreshing, error: refreshError }] = useRefreshManga(id);
2021-05-30 04:01:49 +04:30
const error = mangaError ?? refreshError;
2021-01-19 21:02:57 +03:30
useEffect(() => {
if (manga == null) return;
2023-09-08 00:44:01 +02:00
const doFetch = !autofetchedRef.current && !manga.initialized;
2023-09-08 00:44:01 +02:00
if (doFetch) {
autofetchedRef.current = true;
refresh();
}
}, [manga]);
2021-01-19 21:02:57 +03:30
useEffect(() => {
2024-07-01 19:51:27 +02:00
setTitle(manga?.title ?? t('manga.title_one'));
setAction(null);
return () => {
setTitle('');
setAction(null);
};
}, [t, manga?.title]);
useEffect(() => {
setAction(
2024-09-03 17:15:47 +02:00
<Stack
direction="row"
sx={{
alignItems: 'center',
}}
>
{error && !isValidating && !refreshing && (
<Tooltip
title={
<>
{t('manga.error.label.request_failure')}
<br />
{error.message ?? error}
</>
}
>
2023-09-08 00:44:01 +02:00
<IconButton onClick={() => refetch()}>
<Warning color="error" />
</IconButton>
</Tooltip>
)}
{manga && (refreshing || isValidating) && (
<IconButton disabled>
<CircularProgress size={16} />
</IconButton>
)}
{manga && <MangaToolbarMenu manga={manga} onRefresh={refresh} refreshing={refreshing} />}
</Stack>,
);
return () => {
setAction(null);
};
2023-09-08 00:44:01 +02:00
}, [t, error, isValidating, refreshing, manga, refresh]);
if (error && !manga) {
2024-04-29 15:29:33 +02:00
return (
<EmptyViewAbsoluteCentered message={t('manga.error.label.request_failure')} messageExtra={error.message} />
);
}
2021-01-19 20:20:28 +03:30
return (
<Box sx={{ display: { md: 'flex' }, overflow: 'hidden' }}>
{isLoading && <LoadingPlaceholder />}
{manga && <MangaDetails manga={manga} />}
{manga && <ChapterList manga={manga} isRefreshing={refreshing} />}
</Box>
2021-01-19 20:20:28 +03:30
);
};