Files
suwayomi-material-you-webui/src/features/manga/components/cards/MangaCard.tsx

209 lines
8.4 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
2021-01-26 23:32:12 +03:30
* 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/.
*/
2021-01-26 23:32:12 +03:30
import PopupState, { bindMenu } from 'material-ui-popup-state';
2026-03-13 22:41:28 +01:00
import { memo, useCallback, useMemo } from 'react';
2024-04-05 02:03:00 +02:00
import { useLongPress } from 'use-long-press';
2026-03-11 00:11:29 +01:00
import type { SingleModeProps } from '@/features/manga/components/MangaActionMenuItems.tsx';
import { MangaActionMenuItems } from '@/features/manga/components/MangaActionMenuItems.tsx';
import { Menu } from '@/base/components/menu/Menu.tsx';
2025-08-15 22:02:58 +02:00
import { useManageMangaLibraryState } from '@/features/manga/hooks/useManageMangaLibraryState.tsx';
import { MangaGridCard } from '@/features/manga/components/cards/MangaGridCard.tsx';
import { MangaListCard } from '@/features/manga/components/cards/MangaListCard.tsx';
2026-03-11 00:11:29 +01:00
import type { MangaCardMode, MangaCardProps } from '@/features/manga/Manga.types.ts';
2025-08-15 22:02:58 +02:00
import { ContinueReadingButton } from '@/features/manga/components/ContinueReadingButton.tsx';
import { MangaBadges } from '@/features/manga/components/MangaBadges.tsx';
import { GridLayout } from '@/base/Base.types.ts';
import { AppRoutes } from '@/base/AppRoute.constants.ts';
2025-08-15 22:02:58 +02:00
import { useMetadataServerSettings } from '@/features/settings/services/ServerSettingsMetadata.ts';
2026-03-13 22:41:28 +01:00
import { makeToast } from '@/base/utils/Toast.ts';
import { Mangas } from '@/features/manga/services/Mangas.ts';
import { t } from '@lingui/core/macro';
import { useParams } from 'react-router-dom';
import { ReactRouter } from '@/lib/react-router/ReactRouter.ts';
import { MigrationOptionsDialog } from '@/features/migration/components/MigrationOptionsDialog.tsx';
import { AwaitableComponent } from 'awaitable-component';
import { defaultPromiseErrorHandler } from '@/lib/DefaultPromiseErrorHandler';
2024-03-28 17:30:34 +01:00
const getMangaLinkTo = (
mode: MangaCardMode,
mangaId: number,
sourceId: string | undefined,
mangaTitle: string,
): string => {
switch (mode) {
case 'default':
case 'source':
case 'duplicate':
2024-12-11 20:10:59 +01:00
return AppRoutes.manga.path(mangaId);
case 'migrate.search':
2026-03-13 22:41:28 +01:00
return AppRoutes.migrate.childRoutes.singleMangaSearch.path(sourceId ?? '-1', mangaId, mangaTitle);
case 'migrate.select':
return '';
default:
throw new Error(`getMangaLinkTo: unexpected MangaCardMode "${mode}"`);
}
};
2025-01-13 01:29:40 +01:00
export const MangaCard = memo((props: MangaCardProps) => {
2026-03-13 22:41:28 +01:00
const {
manga,
gridLayout,
inLibraryIndicator,
selected,
handleSelection,
mode = 'default',
onMigrateSelect,
} = props;
const { id, firstUnreadChapter, downloadCount, unreadCount } = manga;
2026-03-13 22:41:28 +01:00
const { mangaId: mangaIdAsString } = useParams<{ mangaId: string }>();
const migrationSourceMangaId = Number(mangaIdAsString);
const {
settings: { showContinueReadingButton },
} = useMetadataServerSettings();
2021-10-31 21:02:45 +03:30
const { updateLibraryState, isInLibrary } = useManageMangaLibraryState(manga, mode === 'source');
2024-07-08 18:02:50 +02:00
const mangaLinkTo = getMangaLinkTo(mode, manga.id, manga.sourceId, manga.title);
2025-01-13 01:29:40 +01:00
const handleClick = useCallback(
(event: React.MouseEvent | React.TouchEvent, openMenu?: () => void) => {
const isDefaultMode = mode === 'default';
const isSourceMode = mode === 'source';
const isMigrateSelectMode = mode === 'migrate.select';
const isSelectionMode = selected !== null;
const isLongPress = !!openMenu;
const shouldHandleClick =
isMigrateSelectMode || isSelectionMode || ((isDefaultMode || isSourceMode) && isLongPress);
if (!shouldHandleClick) {
return;
}
event.preventDefault();
if (isSourceMode) {
updateLibraryState();
return;
}
if (isSelectionMode) {
handleSelection?.(id, !selected, { selectRange: event.shiftKey });
return;
}
if (isDefaultMode) {
openMenu?.();
return;
}
if (isMigrateSelectMode) {
2026-03-13 22:41:28 +01:00
const isBulkMigrationManualSearch = !!onMigrateSelect;
if (isBulkMigrationManualSearch) {
onMigrateSelect(manga);
return;
}
const migrate = () => {
const optionsDialog = AwaitableComponent.showControlled(
MigrationOptionsDialog,
{
mangaIdToMigrateTo: id,
isMigrating: false,
startMigration: async (options) => {
makeToast(t`Migrating manga…`, 'info');
optionsDialog.update({ isMigrating: true });
try {
await Mangas.migrate(migrationSourceMangaId, id, options);
optionsDialog.submit(options);
ReactRouter.navigate(AppRoutes.manga.path(id), { replace: true });
} catch (e) {
optionsDialog.update({ isMigrating: false, startMigration: () => migrate() });
}
},
},
{ id: `manga-migration-single-manga-${migrationSourceMangaId}-${id}` },
);
optionsDialog.promise.catch(defaultPromiseErrorHandler('MangaCard::migrate'));
};
migrate();
2025-01-13 01:29:40 +01:00
}
},
2026-03-13 22:41:28 +01:00
[mode, selected, updateLibraryState, handleSelection, migrationSourceMangaId],
);
2025-01-13 01:29:40 +01:00
const longPressBind = useLongPress(
useCallback(
(e: any, { context }: any) => {
2026-03-10 22:28:19 +01:00
// oxlint-disable-next-line no-param-reassign
2025-01-13 01:29:40 +01:00
e.shiftKey = true;
2026-03-11 00:11:29 +01:00
handleClick(e, context as () => void);
2025-01-13 01:29:40 +01:00
},
[handleClick],
),
);
2025-01-13 01:29:40 +01:00
const MangaCardComponent = useMemo(
() => (gridLayout === GridLayout.List ? MangaListCard : MangaGridCard),
[gridLayout],
);
return (
2026-03-13 22:41:28 +01:00
<PopupState variant="popover" popupId="manga-card-action-menu">
{(popupState) => (
<>
<MangaCardComponent
{...props}
longPressBind={longPressBind}
popupState={popupState}
handleClick={handleClick}
mangaLinkTo={mangaLinkTo}
isInLibrary={isInLibrary}
inLibraryIndicator={inLibraryIndicator}
continueReadingButton={
<ContinueReadingButton
showContinueReadingButton={showContinueReadingButton && mode === 'default'}
chapter={firstUnreadChapter}
mangaLinkTo={mangaLinkTo}
/>
}
mangaBadges={
<MangaBadges
inLibraryIndicator={inLibraryIndicator}
isInLibrary={isInLibrary}
unread={unreadCount}
downloadCount={downloadCount}
updateLibraryState={updateLibraryState}
mode={mode}
/>
}
/>
{!!handleSelection && popupState.isOpen && (
<Menu {...bindMenu(popupState)}>
{(onClose, setHideMenu) => (
<MangaActionMenuItems
manga={manga as SingleModeProps['manga']}
handleSelection={handleSelection}
onClose={onClose}
setHideMenu={setHideMenu}
2025-01-13 01:29:40 +01:00
/>
2026-03-13 22:41:28 +01:00
)}
</Menu>
)}
</>
)}
</PopupState>
2020-12-25 17:42:22 +01:00
);
2025-01-13 01:29:40 +01:00
});