/* * 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 } from 'react-router-dom'; import { Avatar, Box, CardContent, Stack, styled, Tooltip } from '@mui/material'; import { useTranslation } from 'react-i18next'; import PopupState, { bindMenu } from 'material-ui-popup-state'; import { useState } from 'react'; 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'; 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 = styled(Typography)({ lineHeight: '1.5rem', maxHeight: '3rem', display: '-webkit-box', WebkitLineClamp: '2', WebkitBoxOrient: 'vertical', overflow: 'hidden', textOverflow: 'ellipsis', }); 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, 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 { manga, gridLayout, inLibraryIndicator, selected, handleSelection, mode = 'default' } = props; const { id, title, downloadCount, unreadCount: unread, inLibrary, latestReadChapter, 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 = (latestReadChapter?.sourceOrder ?? 0) + 1; const isLatestChapterRead = chapters?.totalCount === latestReadChapter?.sourceOrder; const [isMigrateDialogOpen, setIsMigrateDialogOpen] = useState(false); if (gridLayout !== GridLayout.List) { return ( <> {isMigrateDialogOpen && ( setIsMigrateDialogOpen(false)} /> )} {(popupState) => ( <> { const isMigrateSelectMode = mode === 'migrate.select'; const isSelectionMode = selected !== null; const handleClick = isMigrateSelectMode || isSelectionMode; if (!handleClick) { return; } e.preventDefault(); if (isMigrateSelectMode) { setIsMigrateDialogOpen(true); return; } handleSelection?.(id, !selected); }} to={mangaLinkTo} style={{ textDecoration: '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) => ( <> { if (selected === null) { return; } e.preventDefault(); handleSelection?.(id, !selected); }} > {title} {inLibraryIndicator && inLibrary && ( {t('manga.button.in_library')} )} {showUnreadBadge && unread! > 0 && ( {unread} )} {showDownloadBadge && downloadCount! > 0 && ( {downloadCount} )} {!!handleSelection && popupState.isOpen && ( {(onClose, setHideMenu) => ( )} )} )} ); };