2022-11-09 18:09:15 +01:00
|
|
|
/*
|
|
|
|
|
* 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/.
|
|
|
|
|
*/
|
2022-11-09 18:09:15 +01:00
|
|
|
|
|
|
|
|
import BookmarkIcon from '@mui/icons-material/Bookmark';
|
|
|
|
|
import BookmarkAdd from '@mui/icons-material/BookmarkAdd';
|
|
|
|
|
import BookmarkRemove from '@mui/icons-material/BookmarkRemove';
|
|
|
|
|
import CheckBoxOutlineBlank from '@mui/icons-material/CheckBoxOutlineBlank';
|
|
|
|
|
import Delete from '@mui/icons-material/Delete';
|
|
|
|
|
import Done from '@mui/icons-material/Done';
|
|
|
|
|
import DoneAll from '@mui/icons-material/DoneAll';
|
|
|
|
|
import Download from '@mui/icons-material/Download';
|
|
|
|
|
import MoreVertIcon from '@mui/icons-material/MoreVert';
|
|
|
|
|
import RemoveDone from '@mui/icons-material/RemoveDone';
|
2023-11-05 17:22:29 +01:00
|
|
|
import { CardActionArea, Checkbox, ListItemIcon, ListItemText, Stack, Tooltip } from '@mui/material';
|
2022-11-09 18:09:15 +01:00
|
|
|
import Card from '@mui/material/Card';
|
|
|
|
|
import CardContent from '@mui/material/CardContent';
|
|
|
|
|
import IconButton from '@mui/material/IconButton';
|
|
|
|
|
import Menu from '@mui/material/Menu';
|
|
|
|
|
import MenuItem from '@mui/material/MenuItem';
|
|
|
|
|
import { useTheme } from '@mui/material/styles';
|
|
|
|
|
import Typography from '@mui/material/Typography';
|
|
|
|
|
import React from 'react';
|
|
|
|
|
import { Link } 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';
|
2023-06-05 01:42:39 +02:00
|
|
|
import { getUploadDateString } from '@/util/date';
|
2023-10-28 00:32:02 +02:00
|
|
|
import { DownloadStateIndicator } from '@/components/molecules/DownloadStateIndicator';
|
2023-10-15 16:03:08 +02:00
|
|
|
import { DownloadType, UpdateChapterPatchInput } from '@/lib/graphql/generated/graphql.ts';
|
|
|
|
|
import { TChapter } from '@/typings.ts';
|
2023-11-19 21:17:35 +01:00
|
|
|
import { useMetadataServerSettings } from '@/util/metadataServerSettings.ts';
|
2022-11-09 18:09:15 +01:00
|
|
|
|
2023-01-07 16:46:07 +01:00
|
|
|
interface IProps {
|
2023-10-15 16:03:08 +02:00
|
|
|
chapter: TChapter;
|
2023-11-19 21:17:35 +01:00
|
|
|
allChapters: TChapter[];
|
2023-10-04 22:34:32 +02:00
|
|
|
downloadChapter: DownloadType | undefined;
|
2023-02-06 10:06:33 +01:00
|
|
|
showChapterNumber: boolean;
|
|
|
|
|
onSelect: (selected: boolean) => void;
|
|
|
|
|
selected: boolean | null;
|
2022-11-09 18:09:15 +01:00
|
|
|
}
|
|
|
|
|
|
2023-10-28 00:32:02 +02:00
|
|
|
export const ChapterCard: React.FC<IProps> = (props: IProps) => {
|
2023-03-23 13:59:32 +01:00
|
|
|
const { t } = useTranslation();
|
2022-11-09 18:09:15 +01:00
|
|
|
const theme = useTheme();
|
|
|
|
|
|
2023-11-19 21:17:35 +01:00
|
|
|
const { chapter, allChapters, downloadChapter: dc, showChapterNumber, onSelect, selected } = props;
|
2022-11-09 18:09:15 +01:00
|
|
|
const isSelecting = selected !== null;
|
|
|
|
|
|
2023-11-19 21:17:35 +01:00
|
|
|
const { settings: metadataServerSettings } = useMetadataServerSettings();
|
|
|
|
|
|
2022-11-09 18:09:15 +01:00
|
|
|
const [anchorEl, setAnchorEl] = React.useState<null | HTMLElement>(null);
|
|
|
|
|
|
|
|
|
|
const handleMenuClick = (event: React.MouseEvent<HTMLButtonElement>) => {
|
|
|
|
|
// prevent parent tags from getting the event
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
|
|
setAnchorEl(event.currentTarget);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleClose = () => {
|
|
|
|
|
setAnchorEl(null);
|
|
|
|
|
};
|
|
|
|
|
|
2023-09-01 20:36:40 +02:00
|
|
|
type UpdatePatchInput = UpdateChapterPatchInput & { markPrevRead?: boolean };
|
|
|
|
|
const sendChange = <Key extends keyof UpdatePatchInput>(key: Key, value: UpdatePatchInput[Key]) => {
|
2022-11-09 18:09:15 +01:00
|
|
|
handleClose();
|
|
|
|
|
|
2023-11-24 03:17:49 +01:00
|
|
|
const shouldDeleteChapter = ({ isBookmarked, isDownloaded }: TChapter) =>
|
|
|
|
|
isDownloaded && (!isBookmarked || metadataServerSettings.deleteChaptersWithBookmark);
|
|
|
|
|
|
2023-11-19 21:17:35 +01:00
|
|
|
const isMarkAsRead = (key === 'isRead' && value) || key === 'markPrevRead';
|
|
|
|
|
const shouldAutoDeleteChapters = isMarkAsRead && metadataServerSettings.deleteChaptersManuallyMarkedRead;
|
|
|
|
|
|
2023-11-24 03:17:49 +01:00
|
|
|
const chaptersToDelete = key === 'isRead' ? [chapter] : allChapters;
|
|
|
|
|
const chapterIdsToDelete = shouldAutoDeleteChapters
|
|
|
|
|
? chaptersToDelete.filter(shouldDeleteChapter).map(({ id: chapterId }) => chapterId)
|
|
|
|
|
: [];
|
2023-11-19 21:17:35 +01:00
|
|
|
|
2023-09-01 20:36:40 +02:00
|
|
|
if (key === 'markPrevRead') {
|
2023-11-19 21:17:35 +01:00
|
|
|
const index = allChapters.findIndex(({ id: chapterId }) => chapterId === chapter.id);
|
2023-12-05 22:18:53 +01:00
|
|
|
|
|
|
|
|
const isFirstChapter = index + 1 > allChapters.length - 1;
|
|
|
|
|
if (isFirstChapter) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-19 21:17:35 +01:00
|
|
|
requestManager.updateChapters(
|
|
|
|
|
allChapters
|
2023-12-05 22:18:53 +01:00
|
|
|
.slice(index + 1)
|
2023-11-19 21:17:35 +01:00
|
|
|
.filter(({ isRead }) => !isRead)
|
|
|
|
|
.map(({ id: chapterId }) => chapterId),
|
2023-11-24 03:17:49 +01:00
|
|
|
{ isRead: true, chapterIdsToDelete },
|
2023-11-19 21:17:35 +01:00
|
|
|
);
|
2023-09-01 20:36:40 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-30 16:55:35 +02:00
|
|
|
requestManager.updateChapter(chapter.id, {
|
|
|
|
|
[key]: value,
|
|
|
|
|
lastPageRead: key === 'isRead' ? 0 : undefined,
|
2023-11-24 03:17:49 +01:00
|
|
|
deleteChapter: !!chapterIdsToDelete.length,
|
2023-09-30 16:55:35 +02:00
|
|
|
});
|
2022-11-09 18:09:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const downloadChapter = () => {
|
2023-09-01 20:40:24 +02:00
|
|
|
requestManager.addChapterToDownloadQueue(chapter.id);
|
2022-11-09 18:09:15 +01:00
|
|
|
handleClose();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const deleteChapter = () => {
|
2023-09-30 16:55:35 +02:00
|
|
|
requestManager.deleteDownloadedChapter(chapter.id);
|
2022-11-09 18:09:15 +01:00
|
|
|
handleClose();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleSelect = () => {
|
|
|
|
|
onSelect(true);
|
|
|
|
|
handleClose();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleClick = (event: React.MouseEvent<HTMLAnchorElement>) => {
|
|
|
|
|
if (isSelecting) {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
onSelect(!selected);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-09-30 16:55:35 +02:00
|
|
|
const { isDownloaded } = chapter;
|
|
|
|
|
const canBeDownloaded = !chapter.isDownloaded && dc === undefined;
|
2022-11-09 18:09:15 +01:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<li>
|
|
|
|
|
<Card
|
|
|
|
|
sx={{
|
|
|
|
|
position: 'relative',
|
|
|
|
|
margin: 1,
|
|
|
|
|
}}
|
|
|
|
|
>
|
2022-11-26 13:51:56 +01:00
|
|
|
<CardActionArea
|
|
|
|
|
component={Link}
|
2023-09-30 16:55:35 +02:00
|
|
|
to={`/manga/${chapter.manga.id}/chapter/${chapter.sourceOrder}`}
|
2022-11-09 18:09:15 +01:00
|
|
|
style={{
|
2023-09-30 16:55:35 +02:00
|
|
|
color: theme.palette.text[chapter.isRead ? 'disabled' : 'primary'],
|
2022-11-09 18:09:15 +01:00
|
|
|
}}
|
|
|
|
|
onClick={handleClick}
|
|
|
|
|
>
|
|
|
|
|
<CardContent
|
|
|
|
|
sx={{
|
|
|
|
|
display: 'flex',
|
|
|
|
|
justifyContent: 'space-between',
|
|
|
|
|
alignItems: 'center',
|
|
|
|
|
padding: 2,
|
|
|
|
|
'&:last-child': { pb: 2 },
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Stack direction="column" flex={1}>
|
|
|
|
|
<Typography variant="h5" component="h2">
|
2023-09-30 16:55:35 +02:00
|
|
|
{chapter.isBookmarked && (
|
2023-02-06 10:06:33 +01:00
|
|
|
<BookmarkIcon
|
|
|
|
|
color="primary"
|
|
|
|
|
sx={{ mr: 0.5, position: 'relative', top: '0.15em' }}
|
|
|
|
|
/>
|
2022-11-09 18:09:15 +01:00
|
|
|
)}
|
2023-03-23 13:59:32 +01:00
|
|
|
{showChapterNumber ? `${t('chapter.title')} ${chapter.chapterNumber}` : chapter.name}
|
2022-11-09 18:09:15 +01:00
|
|
|
</Typography>
|
2023-02-06 10:06:33 +01:00
|
|
|
<Typography variant="caption">{chapter.scanlator}</Typography>
|
2022-11-09 18:09:15 +01:00
|
|
|
<Typography variant="caption">
|
2023-09-30 16:55:35 +02:00
|
|
|
{getUploadDateString(Number(chapter.uploadDate ?? 0))}
|
2023-03-23 13:59:32 +01:00
|
|
|
{isDownloaded && ` • ${t('chapter.status.label.downloaded')}`}
|
2022-11-09 18:09:15 +01:00
|
|
|
</Typography>
|
|
|
|
|
</Stack>
|
|
|
|
|
|
2022-11-26 13:51:56 +01:00
|
|
|
{dc && <DownloadStateIndicator download={dc} />}
|
|
|
|
|
|
2022-11-09 18:09:15 +01:00
|
|
|
{selected === null ? (
|
2023-11-05 17:22:29 +01:00
|
|
|
<Tooltip title={t('global.button.options')}>
|
|
|
|
|
<IconButton aria-label="more" onClick={handleMenuClick} size="large">
|
|
|
|
|
<MoreVertIcon />
|
|
|
|
|
</IconButton>
|
|
|
|
|
</Tooltip>
|
2022-11-09 18:09:15 +01:00
|
|
|
) : (
|
2023-11-05 17:22:29 +01:00
|
|
|
<Tooltip title={t(selected ? 'global.button.deselect' : 'global.button.select')}>
|
|
|
|
|
<Checkbox checked={selected} />
|
|
|
|
|
</Tooltip>
|
2022-11-09 18:09:15 +01:00
|
|
|
)}
|
|
|
|
|
</CardContent>
|
2022-11-26 13:51:56 +01:00
|
|
|
</CardActionArea>
|
2023-02-08 18:19:26 +01:00
|
|
|
<Menu anchorEl={anchorEl} keepMounted open={Boolean(anchorEl)} onClose={handleClose}>
|
2022-11-09 18:09:15 +01:00
|
|
|
<MenuItem onClick={handleSelect}>
|
|
|
|
|
<ListItemIcon>
|
|
|
|
|
<CheckBoxOutlineBlank fontSize="small" />
|
|
|
|
|
</ListItemIcon>
|
2023-03-23 13:59:32 +01:00
|
|
|
<ListItemText>{t('chapter.action.label.select')}</ListItemText>
|
2022-11-09 18:09:15 +01:00
|
|
|
</MenuItem>
|
|
|
|
|
{isDownloaded && (
|
|
|
|
|
<MenuItem onClick={deleteChapter}>
|
|
|
|
|
<ListItemIcon>
|
|
|
|
|
<Delete fontSize="small" />
|
|
|
|
|
</ListItemIcon>
|
2023-03-23 13:59:32 +01:00
|
|
|
<ListItemText>{t('chapter.action.download.delete.label.action')}</ListItemText>
|
2022-11-09 18:09:15 +01:00
|
|
|
</MenuItem>
|
|
|
|
|
)}
|
|
|
|
|
{canBeDownloaded && (
|
|
|
|
|
<MenuItem onClick={downloadChapter}>
|
|
|
|
|
<ListItemIcon>
|
|
|
|
|
<Download fontSize="small" />
|
|
|
|
|
</ListItemIcon>
|
2023-03-23 13:59:32 +01:00
|
|
|
<ListItemText>{t('chapter.action.download.add.label.action')}</ListItemText>
|
2022-11-09 18:09:15 +01:00
|
|
|
</MenuItem>
|
2023-01-07 16:46:07 +01:00
|
|
|
)}
|
2023-09-30 16:55:35 +02:00
|
|
|
<MenuItem onClick={() => sendChange('isBookmarked', !chapter.isBookmarked)}>
|
2022-11-09 18:09:15 +01:00
|
|
|
<ListItemIcon>
|
2023-09-30 16:55:35 +02:00
|
|
|
{chapter.isBookmarked && <BookmarkRemove fontSize="small" />}
|
|
|
|
|
{!chapter.isBookmarked && <BookmarkAdd fontSize="small" />}
|
2022-11-09 18:09:15 +01:00
|
|
|
</ListItemIcon>
|
|
|
|
|
<ListItemText>
|
2023-09-30 16:55:35 +02:00
|
|
|
{chapter.isBookmarked && t('chapter.action.bookmark.remove.label.action')}
|
|
|
|
|
{!chapter.isBookmarked && t('chapter.action.bookmark.add.label.action')}
|
2022-11-09 18:09:15 +01:00
|
|
|
</ListItemText>
|
|
|
|
|
</MenuItem>
|
2023-09-30 16:55:35 +02:00
|
|
|
<MenuItem onClick={() => sendChange('isRead', !chapter.isRead)}>
|
2022-11-09 18:09:15 +01:00
|
|
|
<ListItemIcon>
|
2023-09-30 16:55:35 +02:00
|
|
|
{chapter.isRead && <RemoveDone fontSize="small" />}
|
|
|
|
|
{!chapter.isRead && <Done fontSize="small" />}
|
2022-11-09 18:09:15 +01:00
|
|
|
</ListItemIcon>
|
|
|
|
|
<ListItemText>
|
2023-09-30 16:55:35 +02:00
|
|
|
{chapter.isRead && t('chapter.action.mark_as_read.remove.label.action')}
|
|
|
|
|
{!chapter.isRead && t('chapter.action.mark_as_read.add.label.action.current')}
|
2022-11-09 18:09:15 +01:00
|
|
|
</ListItemText>
|
|
|
|
|
</MenuItem>
|
|
|
|
|
<MenuItem onClick={() => sendChange('markPrevRead', true)}>
|
|
|
|
|
<ListItemIcon>
|
|
|
|
|
<DoneAll fontSize="small" />
|
|
|
|
|
</ListItemIcon>
|
2023-03-23 13:59:32 +01:00
|
|
|
<ListItemText>{t('chapter.action.mark_as_read.add.label.action.previous')}</ListItemText>
|
2022-11-09 18:09:15 +01:00
|
|
|
</MenuItem>
|
|
|
|
|
</Menu>
|
|
|
|
|
</Card>
|
|
|
|
|
</li>
|
|
|
|
|
);
|
|
|
|
|
};
|