2021-09-28 00:41:12 +03:30
|
|
|
/*
|
|
|
|
|
* Copyright (C) Contributors to the Suwayomi project
|
|
|
|
|
*
|
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
2023-05-18 13:17:41 +02:00
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
2021-09-28 00:41:12 +03:30
|
|
|
|
2022-11-26 13:51:56 +01:00
|
|
|
import DownloadIcon from '@mui/icons-material/Download';
|
2024-04-14 15:50:12 +02:00
|
|
|
import Box from '@mui/material/Box';
|
|
|
|
|
import CardActionArea from '@mui/material/CardActionArea';
|
|
|
|
|
import Tooltip from '@mui/material/Tooltip';
|
2022-11-26 13:51:56 +01:00
|
|
|
import Avatar from '@mui/material/Avatar';
|
2021-09-28 00:41:12 +03:30
|
|
|
import Card from '@mui/material/Card';
|
|
|
|
|
import CardContent from '@mui/material/CardContent';
|
|
|
|
|
import IconButton from '@mui/material/IconButton';
|
|
|
|
|
import Typography from '@mui/material/Typography';
|
2024-09-26 15:33:22 +02:00
|
|
|
import React, { useCallback, useContext, useLayoutEffect, useMemo, useRef, useState } from 'react';
|
2023-06-04 21:30:15 +02:00
|
|
|
import { Link, useLocation } from 'react-router-dom';
|
2023-03-23 13:59:32 +01:00
|
|
|
import { useTranslation } from 'react-i18next';
|
2024-09-12 16:02:20 +02:00
|
|
|
import Refresh from '@mui/icons-material/Refresh';
|
2024-10-06 01:27:51 +02:00
|
|
|
import { requestManager } from '@/lib/requests/RequestManager.ts';
|
2024-10-05 16:09:34 +02:00
|
|
|
import { LoadingPlaceholder } from '@/modules/core/components/placeholder/LoadingPlaceholder.tsx';
|
|
|
|
|
import { EmptyViewAbsoluteCentered } from '@/modules/core/components/placeholder/EmptyViewAbsoluteCentered.tsx';
|
|
|
|
|
import { DownloadStateIndicator } from '@/modules/core/components/DownloadStateIndicator.tsx';
|
2024-09-12 16:02:20 +02:00
|
|
|
import { ChapterType, DownloadState } from '@/lib/graphql/generated/graphql.ts';
|
2024-10-05 22:17:23 +02:00
|
|
|
import { NavBarContext } from '@/modules/navigation-bar/contexts/NavbarContext.tsx';
|
2024-10-05 16:09:34 +02:00
|
|
|
import { UpdateChecker } from '@/modules/core/components/UpdateChecker.tsx';
|
|
|
|
|
import { StyledGroupedVirtuoso } from '@/modules/core/components/virtuoso/StyledGroupedVirtuoso.tsx';
|
|
|
|
|
import { StyledGroupHeader } from '@/modules/core/components/virtuoso/StyledGroupHeader.tsx';
|
|
|
|
|
import { StyledGroupItemWrapper } from '@/modules/core/components/virtuoso/StyledGroupItemWrapper.tsx';
|
2024-10-05 19:21:26 +02:00
|
|
|
import { Mangas } from '@/modules/manga/services/Mangas.ts';
|
2024-10-05 16:09:34 +02:00
|
|
|
import { SpinnerImage } from '@/modules/core/components/SpinnerImage.tsx';
|
|
|
|
|
import { dateTimeFormatter, epochToDate, getDateString } from '@/util/DateHelper.ts';
|
|
|
|
|
import { defaultPromiseErrorHandler } from '@/lib/DefaultPromiseErrorHandler.ts';
|
|
|
|
|
import { TypographyMaxLines } from '@/modules/core/components/TypographyMaxLines.tsx';
|
2024-10-05 19:15:25 +02:00
|
|
|
import { ChapterIdInfo, ChapterMangaInfo } from '@/modules/chapter/services/Chapters.ts';
|
2024-10-05 23:22:42 +02:00
|
|
|
import { makeToast } from '@/modules/core/utils/Toast.ts';
|
2024-10-12 18:41:42 +02:00
|
|
|
import { VirtuosoUtil } from '@/lib/virtuoso/Virtuoso.util.tsx';
|
2024-12-11 20:10:59 +01:00
|
|
|
import { AppRoutes } from '@/modules/core/AppRoute.constants.ts';
|
2024-12-20 17:24:40 +01:00
|
|
|
import { getErrorMessage } from '@/lib/HelperFunctions.ts';
|
2021-09-28 00:41:12 +03:30
|
|
|
|
2024-07-02 16:20:28 +02:00
|
|
|
const groupByDate = (updates: Pick<ChapterType, 'fetchedAt'>[]): [date: string, items: number][] => {
|
2023-05-18 13:48:01 +02:00
|
|
|
if (!updates.length) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
2021-09-28 00:41:12 +03:30
|
|
|
|
2023-05-18 13:48:01 +02:00
|
|
|
const dateToItemMap = new Map<string, number>();
|
|
|
|
|
updates.forEach((item) => {
|
2023-09-30 16:55:35 +02:00
|
|
|
const date = getDateString(epochToDate(Number(item.fetchedAt)));
|
2023-05-18 13:48:01 +02:00
|
|
|
dateToItemMap.set(date, (dateToItemMap.get(date) ?? 0) + 1);
|
2021-09-28 00:41:12 +03:30
|
|
|
});
|
|
|
|
|
|
2023-05-18 13:48:01 +02:00
|
|
|
return [...dateToItemMap.entries()];
|
|
|
|
|
};
|
2021-09-28 00:41:12 +03:30
|
|
|
|
2023-10-28 00:32:02 +02:00
|
|
|
export const Updates: React.FC = () => {
|
2023-03-23 13:59:32 +01:00
|
|
|
const { t } = useTranslation();
|
2023-06-04 21:30:15 +02:00
|
|
|
const location = useLocation();
|
2021-09-28 00:41:12 +03:30
|
|
|
|
2023-10-28 00:32:02 +02:00
|
|
|
const { setTitle, setAction } = useContext(NavBarContext);
|
2023-05-18 13:48:01 +02:00
|
|
|
const {
|
2023-09-30 16:55:35 +02:00
|
|
|
data: chapterUpdateData,
|
|
|
|
|
loading: isLoading,
|
2024-05-05 00:50:15 +02:00
|
|
|
error,
|
2023-09-30 16:55:35 +02:00
|
|
|
fetchMore,
|
2024-05-05 00:50:15 +02:00
|
|
|
refetch,
|
2023-09-30 16:55:35 +02:00
|
|
|
} = requestManager.useGetRecentlyUpdatedChapters(undefined, {
|
|
|
|
|
fetchPolicy: 'cache-and-network',
|
|
|
|
|
notifyOnNetworkStatusChange: true,
|
|
|
|
|
});
|
|
|
|
|
const hasNextPage = !!chapterUpdateData?.chapters.pageInfo.hasNextPage;
|
|
|
|
|
const endCursor = chapterUpdateData?.chapters.pageInfo.endCursor;
|
2023-10-15 16:03:08 +02:00
|
|
|
const updateEntries = chapterUpdateData?.chapters.nodes ?? [];
|
2023-05-18 13:48:01 +02:00
|
|
|
const groupedUpdates = useMemo(() => groupByDate(updateEntries), [updateEntries]);
|
|
|
|
|
const groupCounts: number[] = useMemo(() => groupedUpdates.map((group) => group[1]), [groupedUpdates]);
|
2024-01-01 22:40:39 +01:00
|
|
|
const { data: downloaderData } = requestManager.useGetDownloadStatus();
|
2024-07-11 17:41:38 +02:00
|
|
|
const queue = downloaderData?.downloadStatus.queue ?? [];
|
2021-09-28 00:41:12 +03:30
|
|
|
|
2024-10-12 18:41:42 +02:00
|
|
|
const computeItemKey = VirtuosoUtil.useCreateGroupedComputeItemKey(
|
|
|
|
|
groupCounts,
|
|
|
|
|
useCallback((index) => groupedUpdates[index][0], [groupedUpdates]),
|
|
|
|
|
useCallback((index) => updateEntries[index].id, [updateEntries]),
|
|
|
|
|
);
|
|
|
|
|
|
2023-11-25 15:39:39 +01:00
|
|
|
const lastUpdateTimestampCompRef = useRef<HTMLElement>(null);
|
|
|
|
|
const [lastUpdateTimestampCompHeight, setLastUpdateTimestampCompHeight] = useState(0);
|
|
|
|
|
useLayoutEffect(() => {
|
|
|
|
|
setLastUpdateTimestampCompHeight(lastUpdateTimestampCompRef.current?.clientHeight ?? 0);
|
|
|
|
|
}, [lastUpdateTimestampCompRef.current]);
|
|
|
|
|
|
2023-11-25 15:35:36 +01:00
|
|
|
const { data: lastUpdateTimestampData } = requestManager.useGetLastGlobalUpdateTimestamp({
|
|
|
|
|
/**
|
|
|
|
|
* The {@link UpdateChecker} is responsible for updating the timestamp
|
|
|
|
|
*/
|
|
|
|
|
fetchPolicy: 'cache-only',
|
|
|
|
|
});
|
2023-11-19 22:43:29 +01:00
|
|
|
const lastUpdateTimestamp = lastUpdateTimestampData?.lastUpdateTimestamp.timestamp;
|
|
|
|
|
|
2024-09-26 15:33:22 +02:00
|
|
|
useLayoutEffect(() => {
|
2023-03-23 13:59:32 +01:00
|
|
|
setTitle(t('updates.title'));
|
2023-11-25 15:39:39 +01:00
|
|
|
setAction(<UpdateChecker />);
|
2024-01-26 20:50:51 +01:00
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
setTitle('');
|
|
|
|
|
setAction(null);
|
|
|
|
|
};
|
2023-11-19 22:43:29 +01:00
|
|
|
}, [t, lastUpdateTimestamp]);
|
2021-09-28 00:41:12 +03:30
|
|
|
|
2024-07-02 16:20:28 +02:00
|
|
|
const downloadForChapter = (chapter: Pick<ChapterType, 'sourceOrder'> & ChapterMangaInfo) => {
|
|
|
|
|
const { sourceOrder, mangaId } = chapter;
|
2024-07-11 17:41:38 +02:00
|
|
|
return queue.find((q) => sourceOrder === q.chapter.sourceOrder && mangaId === q.manga.id);
|
2021-09-28 00:41:12 +03:30
|
|
|
};
|
|
|
|
|
|
2024-09-12 16:02:20 +02:00
|
|
|
const handleRetry = async (chapter: ChapterIdInfo) => {
|
|
|
|
|
try {
|
|
|
|
|
await requestManager.addChapterToDownloadQueue(chapter.id).response;
|
|
|
|
|
} catch (e) {
|
2024-12-20 17:24:40 +01:00
|
|
|
makeToast(t('download.queue.error.label.failed_to_remove'), 'error', getErrorMessage(e));
|
2024-09-12 16:02:20 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-02 16:20:28 +02:00
|
|
|
const downloadChapter = (chapter: ChapterIdInfo) => {
|
2024-08-05 15:20:09 +02:00
|
|
|
requestManager
|
|
|
|
|
.addChapterToDownloadQueue(chapter.id)
|
2024-12-20 17:24:40 +01:00
|
|
|
.response.catch((e) =>
|
|
|
|
|
makeToast(t('global.error.label.failed_to_save_changes'), 'error', getErrorMessage(e)),
|
|
|
|
|
);
|
2021-09-28 00:41:12 +03:30
|
|
|
};
|
|
|
|
|
|
2023-05-18 13:48:01 +02:00
|
|
|
const loadMore = useCallback(() => {
|
|
|
|
|
if (!hasNextPage) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-30 16:55:35 +02:00
|
|
|
fetchMore({ variables: { offset: updateEntries.length } });
|
|
|
|
|
}, [hasNextPage, endCursor]);
|
2023-05-18 13:48:01 +02:00
|
|
|
|
2024-05-05 00:50:15 +02:00
|
|
|
if (error) {
|
|
|
|
|
return (
|
|
|
|
|
<EmptyViewAbsoluteCentered
|
|
|
|
|
message={t('global.error.label.failed_to_load_data')}
|
2024-12-21 23:27:03 +01:00
|
|
|
messageExtra={getErrorMessage(error)}
|
2024-05-05 00:50:15 +02:00
|
|
|
retry={() => refetch().catch(defaultPromiseErrorHandler('Updates::refetch'))}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-23 03:40:41 +08:00
|
|
|
if (!isLoading && updateEntries.length === 0) {
|
2024-04-29 15:29:33 +02:00
|
|
|
return <EmptyViewAbsoluteCentered message={t('updates.error.label.no_updates_available')} />;
|
2023-06-23 03:40:41 +08:00
|
|
|
}
|
|
|
|
|
|
2021-09-28 00:41:12 +03:30
|
|
|
return (
|
2023-11-25 15:39:39 +01:00
|
|
|
<>
|
|
|
|
|
<Typography
|
|
|
|
|
ref={lastUpdateTimestampCompRef}
|
|
|
|
|
sx={{
|
|
|
|
|
marginLeft: '10px',
|
|
|
|
|
paddingTop: (theme) => ({ [theme.breakpoints.up('sm')]: { paddingTop: '6px' } }),
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{t('library.settings.global_update.label.last_update', {
|
2024-05-03 15:57:38 +02:00
|
|
|
date: lastUpdateTimestamp ? dateTimeFormatter.format(+lastUpdateTimestamp) : '-',
|
2023-11-25 15:39:39 +01:00
|
|
|
})}
|
|
|
|
|
</Typography>
|
|
|
|
|
<StyledGroupedVirtuoso
|
|
|
|
|
heightToSubtract={lastUpdateTimestampCompHeight}
|
|
|
|
|
style={{
|
|
|
|
|
// override Virtuoso default values and set them with class
|
|
|
|
|
height: 'undefined',
|
|
|
|
|
}}
|
|
|
|
|
components={{
|
|
|
|
|
Footer: () => (isLoading ? <LoadingPlaceholder usePadding /> : null),
|
|
|
|
|
}}
|
|
|
|
|
overscan={window.innerHeight * 0.5}
|
|
|
|
|
endReached={loadMore}
|
|
|
|
|
groupCounts={groupCounts}
|
|
|
|
|
groupContent={(index) => (
|
2024-12-26 02:29:58 +01:00
|
|
|
<StyledGroupHeader isFirstItem={index === 0}>
|
2024-11-04 01:26:58 +01:00
|
|
|
<Typography variant="h5" component="h2">
|
|
|
|
|
{groupedUpdates[index][0]}
|
|
|
|
|
</Typography>
|
2023-11-25 15:39:39 +01:00
|
|
|
</StyledGroupHeader>
|
|
|
|
|
)}
|
2024-10-12 18:41:42 +02:00
|
|
|
computeItemKey={computeItemKey}
|
2023-11-25 15:39:39 +01:00
|
|
|
itemContent={(index) => {
|
|
|
|
|
const chapter = updateEntries[index];
|
|
|
|
|
const { manga } = chapter;
|
|
|
|
|
const download = downloadForChapter(chapter);
|
|
|
|
|
|
|
|
|
|
return (
|
2024-10-12 18:41:42 +02:00
|
|
|
<StyledGroupItemWrapper>
|
2023-11-25 15:39:39 +01:00
|
|
|
<Card>
|
|
|
|
|
<CardActionArea
|
|
|
|
|
component={Link}
|
2024-12-11 20:10:59 +01:00
|
|
|
to={AppRoutes.reader.path(chapter.manga.id, chapter.sourceOrder)}
|
2023-11-25 15:39:39 +01:00
|
|
|
state={location.state}
|
2023-12-18 23:03:29 +01:00
|
|
|
sx={{
|
|
|
|
|
color: (theme) => theme.palette.text[chapter.isRead ? 'disabled' : 'primary'],
|
|
|
|
|
}}
|
2022-11-26 13:51:56 +01:00
|
|
|
>
|
2023-11-25 15:39:39 +01:00
|
|
|
<CardContent
|
|
|
|
|
sx={{
|
|
|
|
|
display: 'flex',
|
|
|
|
|
justifyContent: 'space-between',
|
|
|
|
|
alignItems: 'center',
|
2024-08-31 15:38:51 +02:00
|
|
|
padding: 1.5,
|
2023-11-25 15:39:39 +01:00
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Box sx={{ display: 'flex' }}>
|
2024-12-11 20:10:59 +01:00
|
|
|
<Link
|
|
|
|
|
to={AppRoutes.manga.path(chapter.manga.id)}
|
|
|
|
|
style={{ textDecoration: 'none' }}
|
|
|
|
|
>
|
2024-04-10 02:04:02 +04:00
|
|
|
<Avatar
|
|
|
|
|
variant="rounded"
|
|
|
|
|
sx={{
|
|
|
|
|
width: 56,
|
|
|
|
|
height: 56,
|
|
|
|
|
flex: '0 0 auto',
|
2024-08-31 15:38:51 +02:00
|
|
|
marginRight: 1,
|
2024-04-10 02:04:02 +04:00
|
|
|
background: 'transparent',
|
2024-03-08 00:24:01 +01:00
|
|
|
}}
|
2024-04-10 02:04:02 +04:00
|
|
|
>
|
|
|
|
|
<SpinnerImage
|
|
|
|
|
imgStyle={{
|
|
|
|
|
objectFit: 'cover',
|
|
|
|
|
width: '100%',
|
|
|
|
|
height: '100%',
|
|
|
|
|
imageRendering: 'pixelated',
|
|
|
|
|
}}
|
|
|
|
|
spinnerStyle={{ small: true }}
|
|
|
|
|
alt={manga.title}
|
|
|
|
|
src={Mangas.getThumbnailUrl(manga)}
|
|
|
|
|
/>
|
|
|
|
|
</Avatar>
|
|
|
|
|
</Link>
|
2024-08-31 15:38:51 +02:00
|
|
|
<Box
|
|
|
|
|
sx={{
|
|
|
|
|
display: 'flex',
|
|
|
|
|
flexDirection: 'column',
|
|
|
|
|
justifyContent: 'center',
|
|
|
|
|
flexGrow: 1,
|
|
|
|
|
flexShrink: 1,
|
|
|
|
|
wordBreak: 'break-word',
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<TypographyMaxLines variant="h6" component="h3">
|
2023-11-25 15:39:39 +01:00
|
|
|
{manga.title}
|
2024-05-22 23:28:02 +02:00
|
|
|
</TypographyMaxLines>
|
2024-08-31 15:38:51 +02:00
|
|
|
<TypographyMaxLines variant="caption" display="block" lines={1}>
|
2023-11-25 15:39:39 +01:00
|
|
|
{chapter.name}
|
2024-05-22 23:28:16 +02:00
|
|
|
</TypographyMaxLines>
|
2023-11-25 15:39:39 +01:00
|
|
|
</Box>
|
|
|
|
|
</Box>
|
|
|
|
|
{download && <DownloadStateIndicator download={download} />}
|
2024-09-12 16:02:20 +02:00
|
|
|
{download?.state === DownloadState.Error && (
|
|
|
|
|
<Tooltip title={t('global.button.retry')}>
|
|
|
|
|
<IconButton
|
|
|
|
|
onClick={(e) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
handleRetry(download.chapter);
|
|
|
|
|
}}
|
|
|
|
|
size="large"
|
|
|
|
|
>
|
|
|
|
|
<Refresh />
|
|
|
|
|
</IconButton>
|
|
|
|
|
</Tooltip>
|
|
|
|
|
)}
|
2023-11-25 15:39:39 +01:00
|
|
|
{download == null && !chapter.isDownloaded && (
|
|
|
|
|
<Tooltip title={t('chapter.action.download.add.label.action')}>
|
|
|
|
|
<IconButton
|
|
|
|
|
onClick={(e) => {
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
downloadChapter(chapter);
|
|
|
|
|
}}
|
|
|
|
|
size="large"
|
|
|
|
|
>
|
|
|
|
|
<DownloadIcon />
|
|
|
|
|
</IconButton>
|
|
|
|
|
</Tooltip>
|
|
|
|
|
)}
|
|
|
|
|
</CardContent>
|
|
|
|
|
</CardActionArea>
|
|
|
|
|
</Card>
|
|
|
|
|
</StyledGroupItemWrapper>
|
|
|
|
|
);
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</>
|
2021-09-28 00:41:12 +03:30
|
|
|
);
|
2022-11-26 13:51:56 +01:00
|
|
|
};
|