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
|
|
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
2022-11-26 13:51:56 +01:00
|
|
|
import DownloadIcon from '@mui/icons-material/Download';
|
|
|
|
|
import { CardActionArea } from '@mui/material';
|
|
|
|
|
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';
|
2022-11-26 13:51:56 +01:00
|
|
|
import { Box } from '@mui/system';
|
2021-10-30 16:10:34 +03:30
|
|
|
import NavbarContext from 'components/context/NavbarContext';
|
2022-11-26 13:51:56 +01:00
|
|
|
import DownloadStateIndicator from 'components/molecules/DownloadStateIndicator';
|
2021-11-10 23:04:04 +03:30
|
|
|
import EmptyView from 'components/util/EmptyView';
|
|
|
|
|
import LoadingPlaceholder from 'components/util/LoadingPlaceholder';
|
2023-02-06 10:06:33 +01:00
|
|
|
import React, { useContext, useEffect, useRef, useState } from 'react';
|
2022-11-26 13:51:56 +01:00
|
|
|
import { Link, useHistory } from 'react-router-dom';
|
|
|
|
|
import client from 'util/client';
|
|
|
|
|
import useLocalStorage from 'util/useLocalStorage';
|
2023-02-24 16:40:37 +01:00
|
|
|
import { IChapter, IMangaChapter, IQueue, PaginatedList } from 'typings';
|
2023-03-23 13:59:32 +01:00
|
|
|
import { useTranslation } from 'react-i18next';
|
|
|
|
|
import { t as translate } from 'i18next';
|
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-02-08 18:19:26 +01:00
|
|
|
function groupByDate(updates: IMangaChapter[]): [string, { item: IMangaChapter; globalIdx: number }[]][] {
|
2021-09-28 00:41:12 +03:30
|
|
|
if (updates.length === 0) return [];
|
|
|
|
|
|
|
|
|
|
const groups = {};
|
2021-11-10 23:04:04 +03:30
|
|
|
updates.forEach((item, globalIdx) => {
|
2021-09-28 00:41:12 +03:30
|
|
|
const key = getDateString(epochToDate(item.chapter.fetchedAt));
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
if (groups[key] === undefined) groups[key] = [];
|
|
|
|
|
// @ts-ignore
|
2021-11-10 23:04:04 +03:30
|
|
|
groups[key].push({ item, globalIdx });
|
2021-09-28 00:41:12 +03:30
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
return Object.keys(groups).map((key) => [key, groups[key]]);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-08 18:19:26 +01:00
|
|
|
const baseWebsocketUrl = JSON.parse(window.localStorage.getItem('serverBaseURL')!).replace('http', 'ws');
|
2021-09-28 00:41:12 +03:30
|
|
|
const initialQueue = {
|
|
|
|
|
status: 'Stopped',
|
|
|
|
|
queue: [],
|
|
|
|
|
} as IQueue;
|
|
|
|
|
|
2022-11-26 13:51:56 +01:00
|
|
|
const Updates: React.FC = () => {
|
2023-03-23 13:59:32 +01:00
|
|
|
const { t } = useTranslation();
|
2021-09-28 00:41:12 +03:30
|
|
|
const history = useHistory();
|
|
|
|
|
|
|
|
|
|
const { setTitle, setAction } = useContext(NavbarContext);
|
|
|
|
|
const [updateEntries, setUpdateEntries] = useState<IMangaChapter[]>([]);
|
2021-11-10 23:04:04 +03:30
|
|
|
const [hasNextPage, setHasNextPage] = useState(true);
|
|
|
|
|
const [fetched, setFetched] = useState(false);
|
|
|
|
|
const [lastPageNum, setLastPageNum] = useState(0);
|
2021-09-28 00:41:12 +03:30
|
|
|
|
|
|
|
|
const [serverAddress] = useLocalStorage<String>('serverBaseURL', '');
|
|
|
|
|
const [useCache] = useLocalStorage<boolean>('useCache', true);
|
|
|
|
|
|
|
|
|
|
const [, setWsClient] = useState<WebSocket>();
|
|
|
|
|
const [{ queue }, setQueueState] = useState<IQueue>(initialQueue);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const wsc = new WebSocket(`${baseWebsocketUrl}/api/v1/downloads`);
|
|
|
|
|
wsc.onmessage = (e) => {
|
|
|
|
|
const data = JSON.parse(e.data) as IQueue;
|
|
|
|
|
setQueueState(data);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
setWsClient(wsc);
|
|
|
|
|
|
|
|
|
|
return () => wsc.close();
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
useEffect(() => {
|
2021-11-10 23:04:04 +03:30
|
|
|
if (hasNextPage) {
|
2023-02-06 10:06:33 +01:00
|
|
|
client
|
|
|
|
|
.get(`/api/v1/update/recentChapters/${lastPageNum}`)
|
2021-11-10 23:04:04 +03:30
|
|
|
.then((response) => response.data)
|
|
|
|
|
.then(({ hasNextPage: fetchedHasNextPage, page }: PaginatedList<IMangaChapter>) => {
|
2023-02-06 10:06:33 +01:00
|
|
|
setUpdateEntries([...updateEntries, ...page]);
|
2021-11-10 23:04:04 +03:30
|
|
|
setHasNextPage(fetchedHasNextPage);
|
|
|
|
|
setFetched(true);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}, [lastPageNum]);
|
|
|
|
|
|
|
|
|
|
const lastEntry = useRef<HTMLDivElement>(null);
|
|
|
|
|
|
|
|
|
|
const scrollHandler = () => {
|
|
|
|
|
if (lastEntry.current) {
|
|
|
|
|
const rect = lastEntry.current.getBoundingClientRect();
|
2023-02-06 10:06:33 +01:00
|
|
|
if ((rect.y + rect.height) / window.innerHeight < 2 && hasNextPage) {
|
2021-11-10 23:04:04 +03:30
|
|
|
setLastPageNum(lastPageNum + 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
window.addEventListener('scroll', scrollHandler, true);
|
|
|
|
|
return () => {
|
|
|
|
|
window.removeEventListener('scroll', scrollHandler, true);
|
|
|
|
|
};
|
|
|
|
|
}, [hasNextPage, updateEntries]);
|
|
|
|
|
|
2023-02-06 10:06:33 +01:00
|
|
|
if (!fetched) {
|
|
|
|
|
return <LoadingPlaceholder />;
|
|
|
|
|
}
|
|
|
|
|
if (fetched && updateEntries.length === 0) {
|
2023-03-23 13:59:32 +01:00
|
|
|
return <EmptyView message={t('updates.error.label.no_updates_available')} />;
|
2023-02-06 10:06:33 +01:00
|
|
|
}
|
2021-09-28 00:41:12 +03:30
|
|
|
|
2022-11-26 13:51:56 +01:00
|
|
|
const downloadForChapter = (chapter: IChapter) => {
|
|
|
|
|
const { index, mangaId } = chapter;
|
|
|
|
|
return queue.find((q) => index === q.chapterIndex && mangaId === q.mangaId);
|
2021-09-28 00:41:12 +03:30
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const downloadChapter = (chapter: IChapter) => {
|
|
|
|
|
client.get(`/api/v1/download/${chapter.mangaId}/chapter/${chapter.index}`);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
{groupByDate(updateEntries).map((dateGroup) => (
|
|
|
|
|
<div key={dateGroup[0]}>
|
2021-11-10 23:04:04 +03:30
|
|
|
<Typography
|
|
|
|
|
variant="h5"
|
|
|
|
|
sx={{
|
|
|
|
|
ml: 3,
|
|
|
|
|
my: 2,
|
|
|
|
|
fontWeight: 700,
|
2023-05-11 00:47:59 +02:00
|
|
|
textTransform: 'uppercase',
|
2021-11-10 23:04:04 +03:30
|
|
|
}}
|
|
|
|
|
>
|
2021-09-28 00:41:12 +03:30
|
|
|
{dateGroup[0]}
|
2021-11-10 23:04:04 +03:30
|
|
|
</Typography>
|
2022-11-26 13:51:56 +01:00
|
|
|
{dateGroup[1].map(({ item: { chapter, manga }, globalIdx }) => {
|
|
|
|
|
const download = downloadForChapter(chapter);
|
|
|
|
|
return (
|
|
|
|
|
<Card
|
|
|
|
|
ref={globalIdx === updateEntries.length - 1 ? lastEntry : undefined}
|
|
|
|
|
key={globalIdx}
|
|
|
|
|
sx={{ margin: '10px' }}
|
2021-11-10 23:04:04 +03:30
|
|
|
>
|
2022-11-26 13:51:56 +01:00
|
|
|
<CardActionArea
|
|
|
|
|
component={Link}
|
2023-02-06 10:06:33 +01:00
|
|
|
to={{
|
|
|
|
|
pathname: `/manga/${chapter.mangaId}/chapter/${chapter.index}`,
|
|
|
|
|
state: history.location.state,
|
|
|
|
|
}}
|
2022-11-26 13:51:56 +01:00
|
|
|
>
|
|
|
|
|
<CardContent
|
2021-11-10 23:04:04 +03:30
|
|
|
sx={{
|
2022-11-26 13:51:56 +01:00
|
|
|
display: 'flex',
|
|
|
|
|
justifyContent: 'space-between',
|
|
|
|
|
alignItems: 'center',
|
|
|
|
|
padding: 2,
|
2021-11-10 23:04:04 +03:30
|
|
|
}}
|
2022-11-26 13:51:56 +01:00
|
|
|
>
|
|
|
|
|
<Box sx={{ display: 'flex' }}>
|
|
|
|
|
<Avatar
|
|
|
|
|
variant="rounded"
|
|
|
|
|
sx={{
|
|
|
|
|
width: 56,
|
|
|
|
|
height: 56,
|
|
|
|
|
flex: '0 0 auto',
|
|
|
|
|
marginRight: 2,
|
|
|
|
|
imageRendering: 'pixelated',
|
|
|
|
|
}}
|
|
|
|
|
src={`${serverAddress}${manga.thumbnailUrl}?useCache=${useCache}`}
|
|
|
|
|
/>
|
|
|
|
|
<Box sx={{ display: 'flex', flexDirection: 'column' }}>
|
|
|
|
|
<Typography variant="h5" component="h2">
|
|
|
|
|
{manga.title}
|
|
|
|
|
</Typography>
|
2023-02-08 18:19:26 +01:00
|
|
|
<Typography variant="caption" display="block" gutterBottom>
|
2022-11-26 13:51:56 +01:00
|
|
|
{chapter.name}
|
|
|
|
|
</Typography>
|
|
|
|
|
</Box>
|
|
|
|
|
</Box>
|
|
|
|
|
{download && <DownloadStateIndicator download={download} />}
|
|
|
|
|
{download == null && !chapter.downloaded && (
|
2021-09-28 00:41:12 +03:30
|
|
|
<IconButton
|
|
|
|
|
onClick={(e) => {
|
|
|
|
|
e.stopPropagation();
|
2022-11-26 13:51:56 +01:00
|
|
|
e.preventDefault();
|
|
|
|
|
downloadChapter(chapter);
|
2021-09-28 00:41:12 +03:30
|
|
|
}}
|
|
|
|
|
size="large"
|
|
|
|
|
>
|
|
|
|
|
<DownloadIcon />
|
|
|
|
|
</IconButton>
|
|
|
|
|
)}
|
2022-11-26 13:51:56 +01:00
|
|
|
</CardContent>
|
|
|
|
|
</CardActionArea>
|
|
|
|
|
</Card>
|
|
|
|
|
);
|
|
|
|
|
})}
|
2021-09-28 00:41:12 +03:30
|
|
|
</div>
|
|
|
|
|
))}
|
|
|
|
|
</>
|
|
|
|
|
);
|
2022-11-26 13:51:56 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default Updates;
|