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';
|
2023-11-25 15:39:39 +01:00
|
|
|
import React, { useCallback, useContext, useEffect, 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';
|
2023-10-28 00:32:02 +02:00
|
|
|
import { requestManager } from '@/lib/requests/RequestManager.ts';
|
|
|
|
|
import { LoadingPlaceholder } from '@/components/util/LoadingPlaceholder';
|
2024-04-29 15:29:33 +02:00
|
|
|
import { EmptyViewAbsoluteCentered } from '@/components/util/EmptyViewAbsoluteCentered.tsx';
|
2023-10-28 00:32:02 +02:00
|
|
|
import { DownloadStateIndicator } from '@/components/molecules/DownloadStateIndicator';
|
2024-07-11 17:41:38 +02:00
|
|
|
import { ChapterType } from '@/lib/graphql/generated/graphql.ts';
|
2023-10-28 00:32:02 +02:00
|
|
|
import { NavBarContext } from '@/components/context/NavbarContext.tsx';
|
2023-11-19 22:43:29 +01:00
|
|
|
import { UpdateChecker } from '@/components/library/UpdateChecker.tsx';
|
2024-01-02 23:32:49 +01:00
|
|
|
import { StyledGroupedVirtuoso } from '@/components/virtuoso/StyledGroupedVirtuoso.tsx';
|
|
|
|
|
import { StyledGroupHeader } from '@/components/virtuoso/StyledGroupHeader.tsx';
|
|
|
|
|
import { StyledGroupItemWrapper } from '@/components/virtuoso/StyledGroupItemWrapper.tsx';
|
2024-02-16 19:38:13 +01:00
|
|
|
import { Mangas } from '@/lib/data/Mangas.ts';
|
2024-03-08 00:24:01 +01:00
|
|
|
import { SpinnerImage } from '@/components/util/SpinnerImage.tsx';
|
2024-05-03 16:19:20 +02:00
|
|
|
import { dateTimeFormatter, epochToDate, getDateString } from '@/util/date.ts';
|
2024-05-05 00:50:15 +02:00
|
|
|
import { defaultPromiseErrorHandler } from '@/util/defaultPromiseErrorHandler.ts';
|
2024-05-22 23:28:02 +02:00
|
|
|
import { TypographyMaxLines } from '@/components/atoms/TypographyMaxLines.tsx';
|
2024-07-02 16:20:28 +02:00
|
|
|
import { ChapterIdInfo, ChapterMangaInfo } from '@/lib/data/Chapters.ts';
|
2024-08-05 15:20:09 +02:00
|
|
|
import { makeToast } from '@/components/util/Toast.tsx';
|
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
|
|
|
|
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;
|
|
|
|
|
|
2021-09-28 00:41:12 +03:30
|
|
|
useEffect(() => {
|
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-07-02 16:20:28 +02:00
|
|
|
const downloadChapter = (chapter: ChapterIdInfo) => {
|
2024-08-05 15:20:09 +02:00
|
|
|
requestManager
|
|
|
|
|
.addChapterToDownloadQueue(chapter.id)
|
|
|
|
|
.response.catch(() => makeToast(t('global.error.label.failed_to_save_changes'), 'error'));
|
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')}
|
|
|
|
|
messageExtra={error.message}
|
|
|
|
|
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) => (
|
|
|
|
|
<StyledGroupHeader variant="h5" isFirstItem={index === 0}>
|
|
|
|
|
{groupedUpdates[index][0]}
|
|
|
|
|
</StyledGroupHeader>
|
|
|
|
|
)}
|
|
|
|
|
itemContent={(index) => {
|
|
|
|
|
const chapter = updateEntries[index];
|
|
|
|
|
const { manga } = chapter;
|
|
|
|
|
const download = downloadForChapter(chapter);
|
|
|
|
|
|
|
|
|
|
return (
|
2024-07-11 17:22:52 +02:00
|
|
|
<StyledGroupItemWrapper key={index}>
|
2023-11-25 15:39:39 +01:00
|
|
|
<Card>
|
|
|
|
|
<CardActionArea
|
|
|
|
|
component={Link}
|
|
|
|
|
to={`/manga/${chapter.manga.id}/chapter/${chapter.sourceOrder}`}
|
|
|
|
|
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',
|
|
|
|
|
padding: 2,
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Box sx={{ display: 'flex' }}>
|
2024-04-10 02:04:02 +04:00
|
|
|
<Link to={`/manga/${chapter.manga.id}`} style={{ textDecoration: 'none' }}>
|
|
|
|
|
<Avatar
|
|
|
|
|
variant="rounded"
|
|
|
|
|
sx={{
|
|
|
|
|
width: 56,
|
|
|
|
|
height: 56,
|
|
|
|
|
flex: '0 0 auto',
|
|
|
|
|
marginRight: 2,
|
|
|
|
|
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>
|
2023-11-25 15:39:39 +01:00
|
|
|
<Box sx={{ display: 'flex', flexDirection: 'column' }}>
|
2024-05-22 23:28:02 +02:00
|
|
|
<TypographyMaxLines variant="h5" component="h2">
|
2023-11-25 15:39:39 +01:00
|
|
|
{manga.title}
|
2024-05-22 23:28:02 +02:00
|
|
|
</TypographyMaxLines>
|
2024-05-22 23:28:16 +02:00
|
|
|
<TypographyMaxLines
|
|
|
|
|
variant="caption"
|
|
|
|
|
display="block"
|
|
|
|
|
gutterBottom
|
|
|
|
|
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} />}
|
|
|
|
|
{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
|
|
|
};
|