Files
suwayomi-material-you-webui/src/modules/chapter/components/cards/ChapterCard.tsx

234 lines
10 KiB
TypeScript
Raw Normal View History

/*
* 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/.
*/
import BookmarkIcon from '@mui/icons-material/Bookmark';
import MoreVertIcon from '@mui/icons-material/MoreVert';
import CardActionArea from '@mui/material/CardActionArea';
import Checkbox from '@mui/material/Checkbox';
import Stack from '@mui/material/Stack';
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 React, { memo, MouseEvent, TouchEvent, useRef } from 'react';
import { Link } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
import PopupState, { bindMenu, bindTrigger } from 'material-ui-popup-state';
2024-04-05 02:03:00 +02:00
import { useLongPress } from 'use-long-press';
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';
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 {
ChapterBookmarkInfo,
ChapterDownloadInfo,
ChapterIdInfo,
ChapterMangaInfo,
ChapterNumberInfo,
ChapterReadInfo,
Chapters,
ChapterScanlatorInfo,
2024-10-05 19:15:25 +02:00
} 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';
type TChapter = ChapterIdInfo &
ChapterMangaInfo &
ChapterDownloadInfo &
ChapterReadInfo &
ChapterBookmarkInfo &
ChapterNumberInfo &
ChapterScanlatorInfo &
Pick<ChapterType, 'name' | 'sourceOrder' | 'uploadDate'>;
interface IProps {
2024-10-03 04:03:04 +02:00
mode?: 'manga.page' | 'reader';
2023-10-15 16:03:08 +02:00
chapter: TChapter;
showChapterNumber: boolean;
onSelect: (id: number, selected: boolean, isShiftKey?: boolean) => void;
selected: boolean | null;
2024-10-03 04:03:04 +02:00
selectable?: boolean;
isActiveChapter?: boolean; // reader
}
export const ChapterCard = memo((props: IProps) => {
const { t } = useTranslation();
const theme = useTheme();
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;
const isSelecting = selected !== null;
2024-04-05 02:03:00 +02:00
const { isDownloaded } = chapter;
const handleClick = (event: MouseEvent | TouchEvent) => {
2024-04-05 02:03:00 +02:00
if (!isSelecting) return;
event.preventDefault();
event.stopPropagation();
onSelect(chapter.id, !selected, event.shiftKey);
};
const handleClickOpenMenu = (
event: React.MouseEvent | React.TouchEvent,
openMenu?: (e: React.SyntheticEvent) => void,
) => {
event.stopPropagation();
event.preventDefault();
openMenu?.(event);
};
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
});
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',
...applyStyles(mode === 'reader' && isActiveChapter, {
backgroundColor: 'primary.main',
}),
2024-10-03 04:03:04 +02:00
}}
>
<CardActionArea
component={Link}
to={Chapters.getReaderUrl(chapter)}
2024-10-03 04:03:04 +02:00
style={{
color: theme.palette.text[chapter.isRead ? 'disabled' : 'primary'],
}}
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)}
>
<CardContent
sx={{
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
padding: 1.5,
'&:last-child': { pb: 1.5 },
}}
>
<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 && (
<BookmarkIcon
color={mode === 'reader' && isActiveChapter ? 'secondary' : 'primary'}
/>
)
}
slotProps={{
title: {
variant: 'h6',
component: 'h3',
sx: applyStyles(mode === 'reader' && isActiveChapter, {
color: theme.palette.primary.contrastText,
}),
},
secondaryText: {
sx: applyStyles(mode === 'reader' && isActiveChapter, {
color: theme.palette.primary.contrastText,
}),
},
ternaryText: {
sx: applyStyles(mode === 'reader' && isActiveChapter, {
color: theme.palette.primary.contrastText,
}),
},
}}
/>
<DownloadStateIndicator
chapterId={chapter.id}
color={
mode === 'reader' && isActiveChapter
? theme.palette.primary.contrastText
: undefined
}
/>
2024-10-03 04:03:04 +02:00
<Stack sx={{ minHeight: '48px' }}>
{selected === null ? (
<CustomTooltip title={t('global.button.options')}>
2024-10-03 04:03:04 +02:00
<IconButton
ref={menuButtonRef}
{...MUIUtil.preventRippleProp(bindTrigger(popupState), {
onClick: (e: MouseEvent) => handleClickOpenMenu(e),
})}
2024-10-03 04:03:04 +02:00
aria-label="more"
sx={{
color: 'inherit',
...applyStyles(mode === 'reader' && isActiveChapter, {
color: 'primary.contrastText',
}),
}}
>
2024-10-03 04:03:04 +02:00
<MoreVertIcon />
</IconButton>
</CustomTooltip>
2024-10-03 04:03:04 +02:00
) : (
<CustomTooltip
2024-10-03 04:03:04 +02:00
title={t(selected ? 'global.button.deselect' : 'global.button.select')}
>
<Checkbox checked={selected} />
</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}
handleSelection={() => onSelect(chapter.id, true)}
canBeDownloaded={Chapters.isDownloadable(chapter)}
2024-10-03 04:03:04 +02:00
selectable={selectable}
/>
)}
</Menu>
)}
</Stack>
)}
</PopupState>
);
});