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 IconButton from '@mui/material/IconButton';
|
|
|
|
|
import { useTheme } from '@mui/material/styles';
|
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';
|
2025-05-03 12:14:05 +02:00
|
|
|
import { DownloadStateIndicator } from '@/modules/core/components/downloads/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';
|
2025-05-05 01:52:51 +02:00
|
|
|
import { Chapters } from '@/modules/chapter/services/Chapters.ts';
|
|
|
|
|
import { applyStyles } from '@/modules/core/utils/ApplyStyles.ts';
|
|
|
|
|
import { ChapterCardMetadata } from '@/modules/chapter/components/cards/ChapterCardMetadata.tsx';
|
|
|
|
|
import { MUIUtil } from '@/lib/mui/MUI.util.ts';
|
|
|
|
|
import { ListCardContent } from '@/modules/core/components/lists/cards/ListCardContent.tsx';
|
2024-07-02 16:20:28 +02:00
|
|
|
import {
|
|
|
|
|
ChapterBookmarkInfo,
|
|
|
|
|
ChapterDownloadInfo,
|
|
|
|
|
ChapterIdInfo,
|
|
|
|
|
ChapterMangaInfo,
|
|
|
|
|
ChapterNumberInfo,
|
|
|
|
|
ChapterReadInfo,
|
|
|
|
|
ChapterScanlatorInfo,
|
2025-05-05 01:52:51 +02:00
|
|
|
} from '@/modules/chapter/Chapter.types.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,
|
2025-03-29 03:12:36 +01:00
|
|
|
openMenu?: (e: React.SyntheticEvent) => void,
|
2024-04-07 15:21:25 +02:00
|
|
|
) => {
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
event.preventDefault();
|
2025-03-29 03:12:36 +01:00
|
|
|
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={{
|
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-05-30 22:20:26 +02:00
|
|
|
userSelect: 'none',
|
2024-10-03 04:03:04 +02:00
|
|
|
}}
|
2025-01-28 02:16:00 +01:00
|
|
|
state={Chapters.getReaderOpenChapterLocationState(chapter, true)}
|
2024-10-03 04:03:04 +02:00
|
|
|
replace={mode === 'reader'}
|
|
|
|
|
onClick={(e) => handleClick(e)}
|
|
|
|
|
{...longPressBind(popupState.open)}
|
|
|
|
|
>
|
2025-05-03 11:56:09 +02:00
|
|
|
<ListCardContent>
|
2025-03-10 22:59:21 +01:00
|
|
|
<ChapterCardMetadata
|
|
|
|
|
title={
|
|
|
|
|
showChapterNumber
|
|
|
|
|
? `${t('chapter.title_one')} ${chapter.chapterNumber}`
|
|
|
|
|
: chapter.name
|
|
|
|
|
}
|
|
|
|
|
secondaryText={chapter.scanlator}
|
|
|
|
|
ternaryText={`${getDateString(Number(chapter.uploadDate ?? 0), true)}${isDownloaded ? ` • ${t('chapter.status.label.downloaded')}` : ''}`}
|
|
|
|
|
infoIcons={
|
|
|
|
|
chapter.isBookmarked && (
|
2025-01-17 00:48:37 +01:00
|
|
|
<BookmarkIcon
|
|
|
|
|
color={mode === 'reader' && isActiveChapter ? 'secondary' : 'primary'}
|
|
|
|
|
/>
|
2025-03-10 22:59:21 +01:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
slotProps={{
|
|
|
|
|
title: {
|
|
|
|
|
variant: 'h6',
|
|
|
|
|
component: 'h3',
|
|
|
|
|
sx: applyStyles(mode === 'reader' && isActiveChapter, {
|
|
|
|
|
color: theme.palette.primary.contrastText,
|
|
|
|
|
}),
|
|
|
|
|
},
|
|
|
|
|
secondaryText: {
|
|
|
|
|
sx: applyStyles(mode === 'reader' && isActiveChapter, {
|
2025-01-17 00:48:37 +01:00
|
|
|
color: theme.palette.primary.contrastText,
|
|
|
|
|
}),
|
2025-03-10 22:59:21 +01:00
|
|
|
},
|
|
|
|
|
ternaryText: {
|
|
|
|
|
sx: applyStyles(mode === 'reader' && isActiveChapter, {
|
2025-01-17 00:48:37 +01:00
|
|
|
color: theme.palette.primary.contrastText,
|
|
|
|
|
}),
|
2025-03-10 22:59:21 +01:00
|
|
|
},
|
|
|
|
|
}}
|
|
|
|
|
/>
|
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}
|
2025-03-29 03:12:36 +01:00
|
|
|
{...MUIUtil.preventRippleProp(bindTrigger(popupState), {
|
|
|
|
|
onClick: (e: MouseEvent) => handleClickOpenMenu(e),
|
|
|
|
|
})}
|
2024-10-03 04:03:04 +02:00
|
|
|
aria-label="more"
|
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>
|
2025-05-03 11:56:09 +02:00
|
|
|
</ListCardContent>
|
2024-10-03 04:03:04 +02:00
|
|
|
</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
|
|
|
});
|