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';
|
2023-06-04 21:08:39 +02:00
|
|
|
import { Box, CardActionArea, styled } from '@mui/material';
|
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-10-04 22:34:32 +02:00
|
|
|
import React, { useCallback, useContext, useEffect, useMemo } 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';
|
|
|
|
|
import { t as translate } from 'i18next';
|
2023-05-18 13:48:01 +02:00
|
|
|
import { GroupedVirtuoso } from 'react-virtuoso';
|
2023-08-15 13:54:29 +02:00
|
|
|
import requestManager from '@/lib/requests/RequestManager.ts';
|
2023-06-05 01:42:39 +02:00
|
|
|
import LoadingPlaceholder from '@/components/util/LoadingPlaceholder';
|
|
|
|
|
import EmptyView from '@/components/util/EmptyView';
|
|
|
|
|
import DownloadStateIndicator from '@/components/molecules/DownloadStateIndicator';
|
|
|
|
|
import NavbarContext from '@/components/context/NavbarContext';
|
2023-10-04 22:34:32 +02:00
|
|
|
import { ChapterType, DownloadType } from '@/lib/graphql/generated/graphql.ts';
|
2023-05-18 13:48:01 +02:00
|
|
|
|
|
|
|
|
const StyledGroupedVirtuoso = styled(GroupedVirtuoso)(({ theme }) => ({
|
|
|
|
|
// 64px header
|
|
|
|
|
height: 'calc(100vh - 64px)',
|
|
|
|
|
[theme.breakpoints.down('sm')]: {
|
|
|
|
|
// 64px header (margin); 64px menu (margin);
|
|
|
|
|
height: 'calc(100vh - 64px - 64px)',
|
|
|
|
|
},
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
const StyledGroupHeader = styled(Typography, { shouldForwardProp: (prop) => prop !== 'isFirstItem' })<{
|
|
|
|
|
isFirstItem: boolean;
|
|
|
|
|
}>(({ theme, isFirstItem }) => ({
|
|
|
|
|
paddingLeft: '24px',
|
|
|
|
|
// 16px - 10px (bottom padding of the group items)
|
|
|
|
|
paddingTop: '6px',
|
|
|
|
|
paddingBottom: '16px',
|
|
|
|
|
fontWeight: 700,
|
|
|
|
|
textTransform: 'uppercase',
|
|
|
|
|
backgroundColor: theme.palette.background.default,
|
|
|
|
|
[theme.breakpoints.down('sm')]: {
|
|
|
|
|
// 16px - 8px (margin of header)
|
|
|
|
|
paddingTop: isFirstItem ? '8px' : '6px',
|
|
|
|
|
},
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
const StyledGroupItemWrapper = styled(Box, { shouldForwardProp: (prop) => prop !== 'isLastItem' })<{
|
|
|
|
|
isLastItem: boolean;
|
|
|
|
|
}>(({ isLastItem }) => ({
|
|
|
|
|
padding: '0 10px',
|
|
|
|
|
paddingBottom: isLastItem ? '0' : '10px',
|
|
|
|
|
}));
|
2021-09-28 00:41:12 +03:30
|
|
|
|
|
|
|
|
function epochToDate(epoch: number) {
|
|
|
|
|
const date = new Date(0); // The 0 there is the key, which sets the date to the epoch
|
|
|
|
|
date.setUTCSeconds(epoch);
|
|
|
|
|
return date;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-06 10:06:33 +01:00
|
|
|
function isTheSameDay(first: Date, second: Date) {
|
|
|
|
|
return (
|
|
|
|
|
first.getDate() === second.getDate() &&
|
|
|
|
|
first.getMonth() === second.getMonth() &&
|
|
|
|
|
first.getFullYear() === second.getFullYear()
|
|
|
|
|
);
|
2021-09-28 00:41:12 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getDateString(date: Date) {
|
|
|
|
|
const today = new Date();
|
2023-03-23 13:59:32 +01:00
|
|
|
if (isTheSameDay(today, date)) return translate('global.date.label.today');
|
2021-09-28 00:41:12 +03:30
|
|
|
// calculate yesterday
|
|
|
|
|
const yesterday = new Date();
|
|
|
|
|
yesterday.setDate(today.getDate() - 1);
|
2023-03-23 13:59:32 +01:00
|
|
|
if (isTheSameDay(yesterday, date)) return translate('global.date.label.yesterday');
|
2021-09-28 00:41:12 +03:30
|
|
|
return date.toLocaleDateString();
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-30 16:55:35 +02:00
|
|
|
const groupByDate = (updates: ChapterType[]): [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
|
|
|
|
2022-11-26 13:51:56 +01:00
|
|
|
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
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
fetchMore,
|
|
|
|
|
} = requestManager.useGetRecentlyUpdatedChapters(undefined, {
|
|
|
|
|
fetchPolicy: 'cache-and-network',
|
|
|
|
|
notifyOnNetworkStatusChange: true,
|
2023-10-02 16:58:32 +02:00
|
|
|
omitAbortSignal: true,
|
2023-09-30 16:55:35 +02:00
|
|
|
});
|
|
|
|
|
const hasNextPage = !!chapterUpdateData?.chapters.pageInfo.hasNextPage;
|
|
|
|
|
const endCursor = chapterUpdateData?.chapters.pageInfo.endCursor;
|
|
|
|
|
const updateEntries = (chapterUpdateData?.chapters.nodes as ChapterType[]) ?? [];
|
2023-05-18 13:48:01 +02:00
|
|
|
const groupedUpdates = useMemo(() => groupByDate(updateEntries), [updateEntries]);
|
|
|
|
|
const groupCounts: number[] = useMemo(() => groupedUpdates.map((group) => group[1]), [groupedUpdates]);
|
2023-10-04 22:34:32 +02:00
|
|
|
const { data: downloaderData } = requestManager.useDownloadSubscription();
|
|
|
|
|
const queue = (downloaderData?.downloadChanged.queue as DownloadType[]) ?? [];
|
2021-09-28 00:41:12 +03:30
|
|
|
|
|
|
|
|
useEffect(() => {
|
2023-03-23 13:59:32 +01:00
|
|
|
setTitle(t('updates.title'));
|
2021-09-28 00:41:12 +03:30
|
|
|
|
2023-03-11 18:05:58 +01:00
|
|
|
setAction(null);
|
2023-03-28 23:14:03 +02:00
|
|
|
}, [t]);
|
2021-09-28 00:41:12 +03:30
|
|
|
|
2023-09-30 16:55:35 +02:00
|
|
|
const downloadForChapter = (chapter: ChapterType) => {
|
|
|
|
|
const {
|
|
|
|
|
sourceOrder,
|
|
|
|
|
manga: { id: mangaId },
|
|
|
|
|
} = chapter;
|
2023-10-04 22:34:32 +02:00
|
|
|
return queue.find((q) => sourceOrder === q.chapter.sourceOrder && mangaId === q.chapter.manga.id);
|
2021-09-28 00:41:12 +03:30
|
|
|
};
|
|
|
|
|
|
2023-09-30 16:55:35 +02:00
|
|
|
const downloadChapter = (chapter: ChapterType) => {
|
2023-09-01 20:40:24 +02:00
|
|
|
requestManager.addChapterToDownloadQueue(chapter.id);
|
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
|
|
|
|
2023-06-23 03:40:41 +08:00
|
|
|
if (!isLoading && updateEntries.length === 0) {
|
|
|
|
|
return <EmptyView message={t('updates.error.label.no_updates_available')} />;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-28 00:41:12 +03:30
|
|
|
return (
|
2023-05-18 13:48:01 +02:00
|
|
|
<StyledGroupedVirtuoso
|
|
|
|
|
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) => {
|
2023-09-30 16:55:35 +02:00
|
|
|
const chapter = updateEntries[index];
|
|
|
|
|
const { manga } = chapter;
|
2023-05-18 13:48:01 +02:00
|
|
|
const download = downloadForChapter(chapter);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<StyledGroupItemWrapper key={index} isLastItem={index === updateEntries.length - 1}>
|
|
|
|
|
<Card>
|
|
|
|
|
<CardActionArea
|
|
|
|
|
component={Link}
|
2023-09-30 16:55:35 +02:00
|
|
|
to={`/manga/${chapter.manga.id}/chapter/${chapter.sourceOrder}`}
|
2023-06-04 21:30:15 +02:00
|
|
|
state={location.state}
|
2021-11-10 23:04:04 +03:30
|
|
|
>
|
2023-05-18 13:48:01 +02:00
|
|
|
<CardContent
|
|
|
|
|
sx={{
|
|
|
|
|
display: 'flex',
|
|
|
|
|
justifyContent: 'space-between',
|
|
|
|
|
alignItems: 'center',
|
|
|
|
|
padding: 2,
|
2023-02-06 10:06:33 +01:00
|
|
|
}}
|
2022-11-26 13:51:56 +01:00
|
|
|
>
|
2023-05-18 13:48:01 +02:00
|
|
|
<Box sx={{ display: 'flex' }}>
|
|
|
|
|
<Avatar
|
|
|
|
|
variant="rounded"
|
|
|
|
|
sx={{
|
|
|
|
|
width: 56,
|
|
|
|
|
height: 56,
|
|
|
|
|
flex: '0 0 auto',
|
|
|
|
|
marginRight: 2,
|
|
|
|
|
imageRendering: 'pixelated',
|
|
|
|
|
}}
|
2023-09-30 16:55:35 +02:00
|
|
|
src={requestManager.getValidImgUrlFor(manga.thumbnailUrl ?? '')}
|
2023-05-18 13:48:01 +02:00
|
|
|
/>
|
|
|
|
|
<Box sx={{ display: 'flex', flexDirection: 'column' }}>
|
|
|
|
|
<Typography variant="h5" component="h2">
|
|
|
|
|
{manga.title}
|
|
|
|
|
</Typography>
|
|
|
|
|
<Typography variant="caption" display="block" gutterBottom>
|
|
|
|
|
{chapter.name}
|
|
|
|
|
</Typography>
|
2022-11-26 13:51:56 +01:00
|
|
|
</Box>
|
2023-05-18 13:48:01 +02:00
|
|
|
</Box>
|
|
|
|
|
{download && <DownloadStateIndicator download={download} />}
|
2023-09-30 16:55:35 +02:00
|
|
|
{download == null && !chapter.isDownloaded && (
|
2023-05-18 13:48:01 +02:00
|
|
|
<IconButton
|
|
|
|
|
onClick={(e) => {
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
downloadChapter(chapter);
|
|
|
|
|
}}
|
|
|
|
|
size="large"
|
|
|
|
|
>
|
|
|
|
|
<DownloadIcon />
|
|
|
|
|
</IconButton>
|
|
|
|
|
)}
|
|
|
|
|
</CardContent>
|
|
|
|
|
</CardActionArea>
|
|
|
|
|
</Card>
|
|
|
|
|
</StyledGroupItemWrapper>
|
|
|
|
|
);
|
|
|
|
|
}}
|
|
|
|
|
/>
|
2021-09-28 00:41:12 +03:30
|
|
|
);
|
2022-11-26 13:51:56 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default Updates;
|