2023-12-30 04:04:59 +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
|
|
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import CheckBoxOutlineBlank from '@mui/icons-material/CheckBoxOutlineBlank';
|
|
|
|
|
import Delete from '@mui/icons-material/Delete';
|
|
|
|
|
import Download from '@mui/icons-material/Download';
|
|
|
|
|
import RemoveDone from '@mui/icons-material/RemoveDone';
|
|
|
|
|
import Done from '@mui/icons-material/Done';
|
|
|
|
|
import { useTranslation } from 'react-i18next';
|
|
|
|
|
import BookmarkRemove from '@mui/icons-material/BookmarkRemove';
|
|
|
|
|
import BookmarkAdd from '@mui/icons-material/BookmarkAdd';
|
|
|
|
|
import DoneAll from '@mui/icons-material/DoneAll';
|
|
|
|
|
import { useMemo } from 'react';
|
2024-08-30 04:34:28 +02:00
|
|
|
import LaunchIcon from '@mui/icons-material/Launch';
|
2024-10-05 19:24:45 +02:00
|
|
|
import { SelectableCollectionReturnType } from '@/modules/collection/hooks/useSelectableCollection.ts';
|
2023-12-30 04:04:59 +01:00
|
|
|
import {
|
|
|
|
|
actionToTranslationKey,
|
|
|
|
|
ChapterAction,
|
|
|
|
|
ChapterBookmarkInfo,
|
|
|
|
|
ChapterDownloadInfo,
|
2024-04-20 23:15:02 +02:00
|
|
|
ChapterIdInfo,
|
|
|
|
|
ChapterMangaInfo,
|
2023-12-30 04:04:59 +01:00
|
|
|
ChapterReadInfo,
|
2024-03-02 16:11:39 +01:00
|
|
|
ChapterRealUrlInfo,
|
2023-12-30 04:04:59 +01:00
|
|
|
Chapters,
|
2024-10-05 19:15:25 +02:00
|
|
|
} from '@/modules/chapter/services/Chapters.ts';
|
2024-10-05 16:09:34 +02:00
|
|
|
import { MenuItem } from '@/modules/core/components/menu/MenuItem.tsx';
|
2024-10-05 19:15:25 +02:00
|
|
|
import { IChapterWithMeta } from '@/modules/chapter/components/ChapterList.tsx';
|
|
|
|
|
import { ChaptersWithMeta } from '@/modules/chapter/services/ChaptersWithMeta.ts';
|
2024-10-05 16:09:34 +02:00
|
|
|
import {
|
|
|
|
|
createGetMenuItemTitle,
|
|
|
|
|
createIsMenuItemDisabled,
|
|
|
|
|
createShouldShowMenuItem,
|
|
|
|
|
} from '@/modules/core/components/menu/Menu.utils.ts';
|
|
|
|
|
import { defaultPromiseErrorHandler } from '@/lib/DefaultPromiseErrorHandler.ts';
|
2024-10-05 22:53:22 +02:00
|
|
|
import { useMetadataServerSettings } from '@/modules/settings/services/ServerSettingsMetadata.ts';
|
2023-12-30 04:04:59 +01:00
|
|
|
|
2024-10-03 04:03:04 +02:00
|
|
|
type BaseProps = { onClose: () => void; selectable?: boolean };
|
2023-12-30 04:04:59 +01:00
|
|
|
|
2024-07-02 16:20:28 +02:00
|
|
|
type TChapter = ChapterIdInfo &
|
|
|
|
|
ChapterMangaInfo &
|
|
|
|
|
ChapterDownloadInfo &
|
|
|
|
|
ChapterBookmarkInfo &
|
|
|
|
|
ChapterReadInfo &
|
|
|
|
|
ChapterRealUrlInfo;
|
|
|
|
|
|
2023-12-30 04:04:59 +01:00
|
|
|
type SingleModeProps = {
|
2024-07-02 16:20:28 +02:00
|
|
|
chapter: TChapter;
|
2023-12-30 04:04:59 +01:00
|
|
|
allChapters: TChapter[];
|
|
|
|
|
handleSelection?: SelectableCollectionReturnType<TChapter['id']>['handleSelection'];
|
|
|
|
|
canBeDownloaded: boolean;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type SelectModeProps = {
|
|
|
|
|
selectedChapters: IChapterWithMeta[];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type Props =
|
|
|
|
|
| (BaseProps & SingleModeProps & PropertiesNever<SelectModeProps>)
|
|
|
|
|
| (BaseProps & PropertiesNever<SingleModeProps> & SelectModeProps);
|
|
|
|
|
|
|
|
|
|
export const ChapterActionMenuItems = ({
|
|
|
|
|
chapter,
|
|
|
|
|
allChapters,
|
|
|
|
|
handleSelection,
|
|
|
|
|
canBeDownloaded = false,
|
|
|
|
|
selectedChapters = [],
|
|
|
|
|
onClose,
|
2024-10-03 04:03:04 +02:00
|
|
|
selectable = true,
|
2023-12-30 04:04:59 +01:00
|
|
|
}: Props) => {
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
|
|
|
|
const isSingleMode = !!chapter;
|
|
|
|
|
const { isDownloaded, isRead, isBookmarked } = chapter ?? {};
|
|
|
|
|
|
2024-03-26 23:39:04 +01:00
|
|
|
const {
|
|
|
|
|
settings: { deleteChaptersWithBookmark },
|
|
|
|
|
} = useMetadataServerSettings();
|
|
|
|
|
|
2023-12-30 14:30:04 +01:00
|
|
|
const getMenuItemTitle = createGetMenuItemTitle(isSingleMode, actionToTranslationKey);
|
2023-12-30 04:04:59 +01:00
|
|
|
const shouldShowMenuItem = createShouldShowMenuItem(isSingleMode);
|
|
|
|
|
const isMenuItemDisabled = createIsMenuItemDisabled(isSingleMode);
|
|
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
downloadableChapters,
|
|
|
|
|
downloadedChapters,
|
|
|
|
|
unbookmarkedChapters,
|
|
|
|
|
bookmarkedChapters,
|
|
|
|
|
unreadChapters,
|
|
|
|
|
readChapters,
|
|
|
|
|
} = useMemo(
|
|
|
|
|
() => ({
|
|
|
|
|
downloadableChapters: ChaptersWithMeta.getDownloadable(selectedChapters),
|
|
|
|
|
downloadedChapters: ChaptersWithMeta.getDownloaded(selectedChapters),
|
|
|
|
|
unbookmarkedChapters: ChaptersWithMeta.getNonBookmarked(selectedChapters),
|
|
|
|
|
bookmarkedChapters: ChaptersWithMeta.getBookmarked(selectedChapters),
|
|
|
|
|
unreadChapters: ChaptersWithMeta.getNonRead(selectedChapters),
|
|
|
|
|
readChapters: ChaptersWithMeta.getRead(selectedChapters),
|
|
|
|
|
}),
|
|
|
|
|
[selectedChapters],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const handleSelect = () => {
|
|
|
|
|
handleSelection?.(chapter.id, true);
|
|
|
|
|
onClose();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const performAction = (action: ChapterAction | 'mark_prev_as_read', chaptersWithMeta: IChapterWithMeta[]) => {
|
|
|
|
|
const isMarkPrevAsRead = action === 'mark_prev_as_read';
|
|
|
|
|
const actualAction: ChapterAction = isMarkPrevAsRead ? 'mark_as_read' : action;
|
|
|
|
|
|
2024-03-26 23:39:04 +01:00
|
|
|
if (actualAction === 'delete' && chapter) {
|
|
|
|
|
const isDeletable = Chapters.isDeletable(chapter, deleteChaptersWithBookmark);
|
|
|
|
|
if (!isDeletable) {
|
|
|
|
|
onClose();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-20 23:15:02 +02:00
|
|
|
const getChapters = (): SingleModeProps['chapter'][] => {
|
2023-12-30 04:04:59 +01:00
|
|
|
// select mode
|
|
|
|
|
if (!chapter) {
|
|
|
|
|
return ChaptersWithMeta.getChapters(chaptersWithMeta);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!isMarkPrevAsRead) {
|
|
|
|
|
return [chapter];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const index = allChapters.findIndex(({ id: chapterId }) => chapterId === chapter.id);
|
|
|
|
|
|
|
|
|
|
const isFirstChapter = index + 1 > allChapters.length - 1;
|
|
|
|
|
if (isFirstChapter) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return allChapters.slice(index + 1);
|
|
|
|
|
};
|
|
|
|
|
|
2024-03-27 21:11:41 +01:00
|
|
|
const chapters = getChapters();
|
|
|
|
|
|
|
|
|
|
if (!chapters.length) {
|
|
|
|
|
onClose();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-20 22:49:47 +02:00
|
|
|
Chapters.performAction(actualAction, Chapters.getIds(chapters), {
|
|
|
|
|
chapters,
|
2023-12-30 04:04:59 +01:00
|
|
|
wasManuallyMarkedAsRead: true,
|
2024-04-20 23:15:02 +02:00
|
|
|
trackProgressMangaId: chapters[0]?.mangaId,
|
2024-03-02 15:28:58 +01:00
|
|
|
}).catch(defaultPromiseErrorHandler('ChapterActionMenuItems::performAction'));
|
2023-12-30 04:04:59 +01:00
|
|
|
onClose();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
2024-10-03 04:03:04 +02:00
|
|
|
{isSingleMode && selectable && (
|
2023-12-30 04:04:59 +01:00
|
|
|
<MenuItem onClick={handleSelect} Icon={CheckBoxOutlineBlank} title={t('chapter.action.label.select')} />
|
|
|
|
|
)}
|
2024-03-02 16:11:39 +01:00
|
|
|
{isSingleMode && (
|
|
|
|
|
<MenuItem
|
2024-08-30 04:34:28 +02:00
|
|
|
Icon={LaunchIcon}
|
2024-03-02 16:11:39 +01:00
|
|
|
disabled={!chapter!.realUrl}
|
|
|
|
|
onClick={() => {
|
|
|
|
|
window.open(chapter!.realUrl!, '_blank', 'noopener,noreferrer');
|
|
|
|
|
onClose();
|
|
|
|
|
}}
|
|
|
|
|
title={t('chapter.action.label.open_on_source')}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2023-12-30 04:04:59 +01:00
|
|
|
{shouldShowMenuItem(canBeDownloaded) && (
|
|
|
|
|
<MenuItem
|
|
|
|
|
Icon={Download}
|
2024-01-29 20:54:56 +01:00
|
|
|
disabled={isMenuItemDisabled(!downloadableChapters.length)}
|
2023-12-30 04:04:59 +01:00
|
|
|
onClick={() => performAction('download', downloadableChapters)}
|
|
|
|
|
title={getMenuItemTitle('download', downloadableChapters.length)}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
{shouldShowMenuItem(isDownloaded) && (
|
|
|
|
|
<MenuItem
|
|
|
|
|
Icon={Delete}
|
2024-01-29 20:54:56 +01:00
|
|
|
disabled={isMenuItemDisabled(!downloadedChapters.length)}
|
2024-03-26 23:39:04 +01:00
|
|
|
onClick={() =>
|
|
|
|
|
performAction(
|
|
|
|
|
'delete',
|
|
|
|
|
ChaptersWithMeta.getDeletable(downloadedChapters, deleteChaptersWithBookmark),
|
|
|
|
|
)
|
|
|
|
|
}
|
2023-12-30 04:04:59 +01:00
|
|
|
title={getMenuItemTitle('delete', downloadedChapters.length)}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
{shouldShowMenuItem(!isBookmarked) && (
|
|
|
|
|
<MenuItem
|
|
|
|
|
Icon={BookmarkAdd}
|
2024-01-29 20:54:56 +01:00
|
|
|
disabled={isMenuItemDisabled(!unbookmarkedChapters.length)}
|
2023-12-30 04:04:59 +01:00
|
|
|
onClick={() => performAction('bookmark', unbookmarkedChapters)}
|
|
|
|
|
title={getMenuItemTitle('bookmark', unbookmarkedChapters.length)}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
{shouldShowMenuItem(isBookmarked) && (
|
|
|
|
|
<MenuItem
|
|
|
|
|
Icon={BookmarkRemove}
|
2024-01-29 20:54:56 +01:00
|
|
|
disabled={isMenuItemDisabled(!bookmarkedChapters.length)}
|
2023-12-30 04:04:59 +01:00
|
|
|
onClick={() => performAction('unbookmark', bookmarkedChapters)}
|
|
|
|
|
title={getMenuItemTitle('unbookmark', bookmarkedChapters.length)}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
{shouldShowMenuItem(!isRead) && (
|
|
|
|
|
<MenuItem
|
|
|
|
|
Icon={Done}
|
2024-01-29 20:54:56 +01:00
|
|
|
disabled={isMenuItemDisabled(!unreadChapters.length)}
|
2023-12-30 04:04:59 +01:00
|
|
|
onClick={() => performAction('mark_as_read', unreadChapters)}
|
|
|
|
|
title={getMenuItemTitle('mark_as_read', unreadChapters.length)}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
{shouldShowMenuItem(isRead) && (
|
|
|
|
|
<MenuItem
|
|
|
|
|
Icon={RemoveDone}
|
2024-01-29 20:54:56 +01:00
|
|
|
disabled={isMenuItemDisabled(!readChapters.length)}
|
2023-12-30 04:04:59 +01:00
|
|
|
onClick={() => performAction('mark_as_unread', readChapters)}
|
|
|
|
|
title={getMenuItemTitle('mark_as_unread', readChapters.length)}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
{isSingleMode && (
|
|
|
|
|
<MenuItem
|
|
|
|
|
onClick={() => performAction('mark_prev_as_read', [])}
|
|
|
|
|
Icon={DoneAll}
|
|
|
|
|
title={t('chapter.action.mark_as_read.add.label.action.previous')}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
};
|