/* * 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 Card from '@mui/material/Card'; import CardActionArea from '@mui/material/CardActionArea'; import Typography from '@mui/material/Typography'; import { Link as RouterLink } from 'react-router-dom'; import { Avatar, Box, CardContent, Link, Stack, styled, Tooltip } from '@mui/material'; import { useTranslation } from 'react-i18next'; import PopupState, { bindMenu } from 'material-ui-popup-state'; import { useRef, useState } from 'react'; import { useLongPress } from 'use-long-press'; import { GridLayout, useLibraryOptionsContext } from '@/components/context/LibraryOptionsContext'; import { SpinnerImage } from '@/components/util/SpinnerImage'; import { TManga, TPartialManga } from '@/typings.ts'; import { ContinueReadingButton } from '@/components/manga/ContinueReadingButton.tsx'; import { SelectableCollectionReturnType } from '@/components/collection/useSelectableCollection.ts'; import { MangaOptionButton } from '@/components/manga/MangaOptionButton.tsx'; import { MangaActionMenuItems, SingleModeProps } from '@/components/manga/MangaActionMenuItems.tsx'; import { Menu } from '@/components/menu/Menu.tsx'; import { MigrateDialog } from '@/components/MigrateDialog.tsx'; import { Mangas } from '@/lib/data/Mangas.ts'; import { TypographyMaxLines } from '@/components/atoms/TypographyMaxLines.tsx'; const BottomGradient = styled('div')({ position: 'absolute', bottom: 0, width: '100%', height: '30%', background: 'linear-gradient(180deg, rgba(0,0,0,0) 0%, rgba(0,0,0,1) 100%)', }); const BottomGradientDoubledDown = styled('div')({ position: 'absolute', bottom: 0, width: '100%', height: '20%', background: 'linear-gradient(180deg, rgba(0,0,0,0) 0%, rgba(0,0,0,1) 100%)', }); const MangaTitle = TypographyMaxLines; const GridMangaTitle = styled(MangaTitle)({ fontSize: '1.05rem', }); const BadgeContainer = styled('div')({ display: 'flex', height: 'fit-content', borderRadius: '5px', overflow: 'hidden', '& p': { color: 'white', padding: '0.1em', paddingInline: '0.2em', fontSize: '1.05rem', }, }); type MangaCardMode = 'default' | 'migrate.search' | 'migrate.select'; export interface MangaCardProps { manga: TPartialManga; gridLayout?: GridLayout; inLibraryIndicator?: boolean; selected?: boolean | null; handleSelection?: SelectableCollectionReturnType['handleSelection']; mode?: MangaCardMode; } const getMangaLinkTo = ( mode: MangaCardMode, mangaId: number, sourceId: string | undefined, mangaTitle: string, ): string => { switch (mode) { case 'default': return `/manga/${mangaId}/`; case 'migrate.search': return `/migrate/source/${sourceId}/manga/${mangaId}/search?query=${mangaTitle}`; case 'migrate.select': return ''; default: throw new Error(`getMangaLinkTo: unexpected MangaCardMode "${mode}"`); } }; export const MangaCard = (props: MangaCardProps) => { const { t } = useTranslation(); const optionButtonRef = useRef(null); const { manga, gridLayout, inLibraryIndicator, selected, handleSelection, mode = 'default' } = props; const { id, title, downloadCount, unreadCount: unread, inLibrary, latestReadChapter, firstUnreadChapter, chapters, } = manga; const thumbnailUrl = Mangas.getThumbnailUrl(manga); const { options: { showContinueReadingButton, showUnreadBadge, showDownloadBadge }, } = useLibraryOptionsContext(); const mangaLinkTo = getMangaLinkTo(mode, manga.id, manga.source?.id, manga.title); const nextChapterIndexToRead = firstUnreadChapter?.sourceOrder ?? 1; const isLatestChapterRead = chapters?.totalCount === latestReadChapter?.sourceOrder; const [isMigrateDialogOpen, setIsMigrateDialogOpen] = useState(false); const handleClick = (event: React.MouseEvent | React.TouchEvent, openMenu?: () => void) => { const isDefaultMode = mode === 'default'; const isMigrateSelectMode = mode === 'migrate.select'; const isSelectionMode = selected !== null; const shouldHandleClick = isMigrateSelectMode || isSelectionMode || (isDefaultMode && !!openMenu); if (!shouldHandleClick) { return; } event.preventDefault(); if (isSelectionMode) { handleSelection?.(id, !selected, { selectRange: event.shiftKey }); return; } if (isDefaultMode) { openMenu?.(); return; } if (isMigrateSelectMode) { setIsMigrateDialogOpen(true); } }; const longPressBind = useLongPress((e, { context }) => { e.shiftKey = true; handleClick(e, context as () => {}); }); if (gridLayout !== GridLayout.List) { return ( <> {isMigrateDialogOpen && ( setIsMigrateDialogOpen(false)} /> )} {(popupState) => ( <> popupState.open(optionButtonRef.current))} onClick={handleClick} to={mangaLinkTo} sx={{ textDecoration: 'none', touchCallout: 'none' }} > theme.palette.primary.main, backgroundColor: (theme) => (selected ? theme.palette.primary.main : undefined), '@media (hover: hover) and (pointer: fine)': { '&:hover .manga-option-button': { visibility: 'visible', pointerEvents: 'all', }, }, }} > {inLibraryIndicator && inLibrary && ( {t('manga.button.in_library')} )} {showUnreadBadge && (unread ?? 0) > 0 && ( {unread} )} {showDownloadBadge && (downloadCount ?? 0) > 0 && ( {downloadCount} )} <> {gridLayout !== GridLayout.Comfortable && ( <> )} {gridLayout !== GridLayout.Comfortable && ( {title} )} {gridLayout === GridLayout.Comfortable && ( {title} )} {!!handleSelection && popupState.isOpen && ( {(onClose, setHideMenu) => ( )} )} )} ); } return ( <> {isMigrateDialogOpen && ( setIsMigrateDialogOpen(false)} /> )} {(popupState) => ( <> {title} {inLibraryIndicator && inLibrary && ( {t('manga.button.in_library')} )} {showUnreadBadge && unread! > 0 && ( {unread} )} {showDownloadBadge && downloadCount! > 0 && ( {downloadCount} )} {!!handleSelection && popupState.isOpen && ( {(onClose, setHideMenu) => ( )} )} )} ); };