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 MoreVertIcon from '@mui/icons-material/MoreVert';
|
2024-04-14 15:50:12 +02:00
|
|
|
import CardActionArea from '@mui/material/CardActionArea';
|
|
|
|
|
import Checkbox from '@mui/material/Checkbox';
|
|
|
|
|
import Stack from '@mui/material/Stack';
|
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 { useTheme } from '@mui/material/styles';
|
|
|
|
|
import Typography from '@mui/material/Typography';
|
2025-01-12 19:08:46 +01:00
|
|
|
import React, { memo, MouseEvent, TouchEvent, useRef } from 'react';
|
2022-11-09 18:09:15 +01:00
|
|
|
import { Link } from 'react-router-dom';
|
2023-03-23 13:59:32 +01:00
|
|
|
import { useTranslation } from 'react-i18next';
|
2023-12-30 04:04:59 +01:00
|
|
|
import PopupState, { bindMenu, bindTrigger } from 'material-ui-popup-state';
|
2024-04-05 02:03:00 +02:00
|
|
|
import { useLongPress } from 'use-long-press';
|
2025-02-01 14:24:38 +01:00
|
|
|
import { CustomTooltip } from '@/modules/core/components/CustomTooltip.tsx';
|
2024-10-05 16:09:34 +02:00
|
|
|
import { getDateString } from '@/util/DateHelper.ts';
|
|
|
|
|
import { DownloadStateIndicator } from '@/modules/core/components/DownloadStateIndicator.tsx';
|
2024-07-11 17:41:38 +02:00
|
|
|
import { ChapterType } from '@/lib/graphql/generated/graphql.ts';
|
2024-10-05 19:15:25 +02:00
|
|
|
import { ChapterActionMenuItems } from '@/modules/chapter/components/actions/ChapterActionMenuItems.tsx';
|
2024-10-05 16:09:34 +02:00
|
|
|
import { Menu } from '@/modules/core/components/menu/Menu.tsx';
|
|
|
|
|
import { TypographyMaxLines } from '@/modules/core/components/TypographyMaxLines.tsx';
|
2024-07-02 16:20:28 +02:00
|
|
|
import {
|
|
|
|
|
ChapterBookmarkInfo,
|
|
|
|
|
ChapterDownloadInfo,
|
|
|
|
|
ChapterIdInfo,
|
|
|
|
|
ChapterMangaInfo,
|
|
|
|
|
ChapterNumberInfo,
|
|
|
|
|
ChapterReadInfo,
|
2024-12-06 23:59:39 +01:00
|
|
|
Chapters,
|
2024-07-02 16:20:28 +02:00
|
|
|
ChapterScanlatorInfo,
|
2024-10-05 19:15:25 +02:00
|
|
|
} from '@/modules/chapter/services/Chapters.ts';
|
2025-01-17 00:48:37 +01:00
|
|
|
import { applyStyles } from '@/modules/core/utils/ApplyStyles.ts';
|
2024-07-02 16:20:28 +02:00
|
|
|
|
|
|
|
|
type TChapter = ChapterIdInfo &
|
|
|
|
|
ChapterMangaInfo &
|
|
|
|
|
ChapterDownloadInfo &
|
|
|
|
|
ChapterReadInfo &
|
|
|
|
|
ChapterBookmarkInfo &
|
|
|
|
|
ChapterNumberInfo &
|
|
|
|
|
ChapterScanlatorInfo &
|
|
|
|
|
Pick<ChapterType, 'name' | 'sourceOrder' | 'uploadDate'>;
|
2022-11-09 18:09:15 +01:00
|
|
|
|
2023-01-07 16:46:07 +01:00
|
|
|
interface IProps {
|
2024-10-03 04:03:04 +02:00
|
|
|
mode?: 'manga.page' | 'reader';
|
2023-10-15 16:03:08 +02:00
|
|
|
chapter: TChapter;
|
2023-02-06 10:06:33 +01:00
|
|
|
showChapterNumber: boolean;
|
2025-01-12 19:08:46 +01:00
|
|
|
onSelect: (id: number, selected: boolean, isShiftKey?: boolean) => void;
|
2023-02-06 10:06:33 +01:00
|
|
|
selected: boolean | null;
|
2024-10-03 04:03:04 +02:00
|
|
|
selectable?: boolean;
|
|
|
|
|
isActiveChapter?: boolean; // reader
|
2022-11-09 18:09:15 +01:00
|
|
|
}
|
|
|
|
|
|
2025-01-12 19:08:46 +01:00
|
|
|
export const ChapterCard = memo((props: IProps) => {
|
2023-03-23 13:59:32 +01:00
|
|
|
const { t } = useTranslation();
|
2022-11-09 18:09:15 +01:00
|
|
|
const theme = useTheme();
|
|
|
|
|
|
2024-04-07 15:21:25 +02:00
|
|
|
const menuButtonRef = useRef<HTMLButtonElement>(null);
|
|
|
|
|
|
2024-10-03 04:03:04 +02:00
|
|
|
const {
|
|
|
|
|
mode = 'manga.page',
|
|
|
|
|
chapter,
|
|
|
|
|
showChapterNumber,
|
|
|
|
|
onSelect,
|
|
|
|
|
selected,
|
|
|
|
|
selectable = true,
|
|
|
|
|
isActiveChapter = false,
|
|
|
|
|
} = props;
|
2022-11-09 18:09:15 +01:00
|
|
|
const isSelecting = selected !== null;
|
|
|
|
|
|
2024-04-05 02:03:00 +02:00
|
|
|
const { isDownloaded } = chapter;
|
|
|
|
|
|
2024-04-07 15:21:25 +02:00
|
|
|
const handleClick = (event: MouseEvent | TouchEvent) => {
|
2024-04-05 02:03:00 +02:00
|
|
|
if (!isSelecting) return;
|
|
|
|
|
|
2024-04-07 15:21:25 +02:00
|
|
|
event.preventDefault();
|
|
|
|
|
event.stopPropagation();
|
2025-01-12 19:08:46 +01:00
|
|
|
onSelect(chapter.id, !selected, event.shiftKey);
|
2024-04-07 15:21:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleClickOpenMenu = (
|
|
|
|
|
event: React.MouseEvent | React.TouchEvent,
|
|
|
|
|
openMenu: (e: React.SyntheticEvent) => void,
|
|
|
|
|
) => {
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
openMenu(event);
|
2022-11-09 18:09:15 +01:00
|
|
|
};
|
|
|
|
|
|
2024-04-07 15:21:25 +02:00
|
|
|
const longPressBind = useLongPress((event, { context: openMenu }) => {
|
|
|
|
|
if (!isSelecting && !!menuButtonRef.current) {
|
|
|
|
|
handleClickOpenMenu(event, () => (openMenu as (event: Element) => void)?.(menuButtonRef.current!));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
|
event.shiftKey = true;
|
|
|
|
|
handleClick(event);
|
2024-04-05 02:03:00 +02:00
|
|
|
});
|
2022-11-09 18:09:15 +01:00
|
|
|
|
|
|
|
|
return (
|
2024-10-03 04:03:04 +02:00
|
|
|
<PopupState variant="popover" popupId="chapter-card-action-menu">
|
|
|
|
|
{(popupState) => (
|
|
|
|
|
<Stack sx={{ pt: 1, px: 1 }}>
|
|
|
|
|
<Card
|
|
|
|
|
sx={{
|
|
|
|
|
touchCallout: 'none',
|
2025-01-17 00:48:37 +01:00
|
|
|
...applyStyles(mode === 'reader' && isActiveChapter, {
|
|
|
|
|
backgroundColor: 'primary.main',
|
|
|
|
|
}),
|
2024-10-03 04:03:04 +02:00
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<CardActionArea
|
|
|
|
|
component={Link}
|
2024-12-06 23:59:39 +01:00
|
|
|
to={Chapters.getReaderUrl(chapter)}
|
2024-10-03 04:03:04 +02:00
|
|
|
style={{
|
|
|
|
|
color: theme.palette.text[chapter.isRead ? 'disabled' : 'primary'],
|
|
|
|
|
}}
|
2025-02-01 19:48:24 +01:00
|
|
|
state={Chapters.getReaderOpenChapterLocationState(chapter)}
|
2024-10-03 04:03:04 +02:00
|
|
|
replace={mode === 'reader'}
|
|
|
|
|
onClick={(e) => handleClick(e)}
|
|
|
|
|
{...longPressBind(popupState.open)}
|
|
|
|
|
>
|
|
|
|
|
<CardContent
|
|
|
|
|
sx={{
|
|
|
|
|
display: 'flex',
|
|
|
|
|
justifyContent: 'space-between',
|
|
|
|
|
alignItems: 'center',
|
|
|
|
|
padding: 1.5,
|
|
|
|
|
'&:last-child': { pb: 1.5 },
|
2023-12-30 04:04:59 +01:00
|
|
|
}}
|
|
|
|
|
>
|
2024-10-03 04:03:04 +02:00
|
|
|
<Stack
|
|
|
|
|
direction="column"
|
2024-04-07 15:21:25 +02:00
|
|
|
sx={{
|
2024-10-03 04:03:04 +02:00
|
|
|
flex: 1,
|
2023-12-30 04:04:59 +01:00
|
|
|
}}
|
|
|
|
|
>
|
2024-09-03 17:15:47 +02:00
|
|
|
<Stack
|
|
|
|
|
sx={{
|
2024-10-03 04:03:04 +02:00
|
|
|
flexDirection: 'row',
|
|
|
|
|
gap: 0.5,
|
|
|
|
|
alignItems: 'center',
|
2024-09-03 17:15:47 +02:00
|
|
|
}}
|
|
|
|
|
>
|
2024-10-03 04:03:04 +02:00
|
|
|
{chapter.isBookmarked && (
|
2025-01-17 00:48:37 +01:00
|
|
|
<BookmarkIcon
|
|
|
|
|
color={mode === 'reader' && isActiveChapter ? 'secondary' : 'primary'}
|
|
|
|
|
/>
|
2024-10-03 04:03:04 +02:00
|
|
|
)}
|
2025-01-17 00:48:37 +01:00
|
|
|
<TypographyMaxLines
|
|
|
|
|
variant="h6"
|
|
|
|
|
component="h4"
|
|
|
|
|
sx={{
|
|
|
|
|
...applyStyles(mode === 'reader' && isActiveChapter, {
|
|
|
|
|
color: theme.palette.primary.contrastText,
|
|
|
|
|
}),
|
|
|
|
|
}}
|
|
|
|
|
>
|
2024-10-03 04:03:04 +02:00
|
|
|
{showChapterNumber
|
|
|
|
|
? `${t('chapter.title_one')} ${chapter.chapterNumber}`
|
|
|
|
|
: chapter.name}
|
|
|
|
|
</TypographyMaxLines>
|
2024-04-07 15:21:25 +02:00
|
|
|
</Stack>
|
2025-01-17 00:48:37 +01:00
|
|
|
<Typography
|
|
|
|
|
variant="caption"
|
|
|
|
|
sx={{
|
|
|
|
|
...applyStyles(mode === 'reader' && isActiveChapter, {
|
|
|
|
|
color: theme.palette.primary.contrastText,
|
|
|
|
|
}),
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{chapter.scanlator}
|
|
|
|
|
</Typography>
|
|
|
|
|
<Typography
|
|
|
|
|
variant="caption"
|
|
|
|
|
sx={{
|
|
|
|
|
...applyStyles(mode === 'reader' && isActiveChapter, {
|
|
|
|
|
color: theme.palette.primary.contrastText,
|
|
|
|
|
}),
|
|
|
|
|
}}
|
|
|
|
|
>
|
2024-10-03 04:03:04 +02:00
|
|
|
{getDateString(Number(chapter.uploadDate ?? 0), true)}
|
|
|
|
|
{isDownloaded && ` • ${t('chapter.status.label.downloaded')}`}
|
|
|
|
|
</Typography>
|
|
|
|
|
</Stack>
|
2023-12-30 04:04:59 +01:00
|
|
|
|
2025-01-17 00:48:37 +01:00
|
|
|
<DownloadStateIndicator
|
|
|
|
|
chapterId={chapter.id}
|
|
|
|
|
color={
|
|
|
|
|
mode === 'reader' && isActiveChapter
|
|
|
|
|
? theme.palette.primary.contrastText
|
|
|
|
|
: undefined
|
|
|
|
|
}
|
|
|
|
|
/>
|
2023-12-30 04:04:59 +01:00
|
|
|
|
2024-10-03 04:03:04 +02:00
|
|
|
<Stack sx={{ minHeight: '48px' }}>
|
|
|
|
|
{selected === null ? (
|
2025-02-01 14:24:38 +01:00
|
|
|
<CustomTooltip title={t('global.button.options')}>
|
2024-10-03 04:03:04 +02:00
|
|
|
<IconButton
|
|
|
|
|
ref={menuButtonRef}
|
|
|
|
|
{...bindTrigger(popupState)}
|
|
|
|
|
onClick={(e) => handleClickOpenMenu(e, popupState.open)}
|
|
|
|
|
aria-label="more"
|
|
|
|
|
size="large"
|
2025-01-26 03:29:08 +01:00
|
|
|
sx={{
|
|
|
|
|
color: 'inherit',
|
|
|
|
|
...applyStyles(mode === 'reader' && isActiveChapter, {
|
|
|
|
|
color: 'primary.contrastText',
|
|
|
|
|
}),
|
|
|
|
|
}}
|
2023-12-30 04:04:59 +01:00
|
|
|
>
|
2024-10-03 04:03:04 +02:00
|
|
|
<MoreVertIcon />
|
|
|
|
|
</IconButton>
|
2025-02-01 14:24:38 +01:00
|
|
|
</CustomTooltip>
|
2024-10-03 04:03:04 +02:00
|
|
|
) : (
|
2025-02-01 14:24:38 +01:00
|
|
|
<CustomTooltip
|
2024-10-03 04:03:04 +02:00
|
|
|
title={t(selected ? 'global.button.deselect' : 'global.button.select')}
|
|
|
|
|
>
|
|
|
|
|
<Checkbox checked={selected} />
|
2025-02-01 14:24:38 +01:00
|
|
|
</CustomTooltip>
|
2024-10-03 04:03:04 +02:00
|
|
|
)}
|
|
|
|
|
</Stack>
|
|
|
|
|
</CardContent>
|
|
|
|
|
</CardActionArea>
|
|
|
|
|
</Card>
|
|
|
|
|
{!isSelecting && popupState.isOpen && (
|
|
|
|
|
<Menu {...bindMenu(popupState)}>
|
|
|
|
|
{(onClose) => (
|
|
|
|
|
<ChapterActionMenuItems
|
|
|
|
|
onClose={onClose}
|
|
|
|
|
chapter={chapter}
|
2025-01-12 19:08:46 +01:00
|
|
|
handleSelection={() => onSelect(chapter.id, true)}
|
2025-01-12 15:05:09 +01:00
|
|
|
canBeDownloaded={Chapters.isDownloadable(chapter)}
|
2024-10-03 04:03:04 +02:00
|
|
|
selectable={selectable}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</Menu>
|
|
|
|
|
)}
|
|
|
|
|
</Stack>
|
|
|
|
|
)}
|
|
|
|
|
</PopupState>
|
2022-11-09 18:09:15 +01:00
|
|
|
);
|
2025-01-12 19:08:46 +01:00
|
|
|
});
|