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

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
2024-04-14 15:50:12 +02:00
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, useLayoutEffect, 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';
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';
2024-06-05 23:04:13 +02:00
import { NavBarContext } from '@/components/context/NavbarContext';
2023-12-30 14:30:04 +01:00
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 = () => {
2023-03-23 13:59:32 +01:00
const { t } = useTranslation();
2023-10-28 00:32:02 +02:00
const { setTitle, setAction } = 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);
2021-01-19 20:20:28 +03:30
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;
2022-11-02 10:42:02 +01:00
2024-05-11 01:10:55 +02:00
const [refresh, { loading: refreshing, error: refreshError }] = useRefreshManga(id);
2021-05-30 04:01:49 +04:30
2024-05-11 01:10:55 +02:00
const error = mangaError ?? refreshError;
2021-01-19 21:02:57 +03:30
useEffect(() => {
2022-11-02 10:42:02 +01:00
if (manga == null) return;
2023-09-08 00:44:01 +02:00
2024-01-29 20:55:33 +01:00
const doFetch = !autofetchedRef.current && !manga.initialized;
2023-09-08 00:44:01 +02:00
if (doFetch) {
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
useLayoutEffect(() => {
2024-07-01 19:51:27 +02:00
setTitle(manga?.title ?? t('manga.title_one'));
setAction(null);
2024-01-26 20:50:51 +01:00
return () => {
setTitle('');
setAction(null);
};
}, [t, manga?.title]);
2022-11-02 10:42:02 +01:00
useLayoutEffect(() => {
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>,
);
2024-01-26 20:50:51 +01:00
return () => {
setAction(null);
};
2023-09-08 00:44:01 +02:00
}, [t, error, isValidating, refreshing, manga, refresh]);
2022-11-09 18:09:15 +01:00
if (error && !manga) {
2024-04-29 15:29:33 +02:00
return (
<EmptyViewAbsoluteCentered message={t('manga.error.label.request_failure')} messageExtra={error.message} />
);
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' }}>
2023-05-15 11:56:55 +02:00
{isLoading && <LoadingPlaceholder />}
2022-11-09 18:09:15 +01:00
{manga && <MangaDetails manga={manga} />}
2023-09-30 16:55:35 +02:00
{manga && <ChapterList manga={manga} isRefreshing={refreshing} />}
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
};