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/.
|
|
|
|
|
*/
|
|
|
|
|
|
2026-03-20 03:02:52 +01:00
|
|
|
import { STABLE_EMPTY_ARRAY } from '@/base/Base.constants.ts';
|
2023-12-30 04:04:59 +01:00
|
|
|
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 BookmarkRemove from '@mui/icons-material/BookmarkRemove';
|
|
|
|
|
import BookmarkAdd from '@mui/icons-material/BookmarkAdd';
|
|
|
|
|
import DoneAll from '@mui/icons-material/DoneAll';
|
2026-03-11 00:11:29 +01:00
|
|
|
import type { ComponentProps } from 'react';
|
|
|
|
|
import { useMemo } from 'react';
|
2026-01-10 19:45:02 +01:00
|
|
|
import { useLingui } from '@lingui/react/macro';
|
2026-03-11 00:11:29 +01:00
|
|
|
import type { SelectableCollectionReturnType } from '@/base/collection/hooks/useSelectableCollection.ts';
|
2025-08-15 22:02:58 +02:00
|
|
|
import { Chapters } from '@/features/chapter/services/Chapters.ts';
|
2025-08-16 02:48:46 +02:00
|
|
|
import { MenuItem } from '@/base/components/menu/MenuItem.tsx';
|
2024-10-05 16:09:34 +02:00
|
|
|
import {
|
|
|
|
|
createGetMenuItemTitle,
|
|
|
|
|
createIsMenuItemDisabled,
|
|
|
|
|
createShouldShowMenuItem,
|
2025-08-16 02:48:46 +02:00
|
|
|
} from '@/base/components/menu/Menu.utils.ts';
|
2024-10-05 16:09:34 +02:00
|
|
|
import { defaultPromiseErrorHandler } from '@/lib/DefaultPromiseErrorHandler.ts';
|
2025-08-15 22:02:58 +02:00
|
|
|
import { useMetadataServerSettings } from '@/features/settings/services/ServerSettingsMetadata.ts';
|
2026-03-11 00:11:29 +01:00
|
|
|
import type { ChapterCard } from '@/features/chapter/components/cards/ChapterCard.tsx';
|
2025-01-12 19:08:46 +01:00
|
|
|
import { requestManager } from '@/lib/requests/RequestManager.ts';
|
2026-03-11 00:11:29 +01:00
|
|
|
import type { GetChaptersMangaQuery } from '@/lib/graphql/generated/graphql.ts';
|
2025-12-22 14:00:44 +01:00
|
|
|
import { GET_CHAPTERS_MANGA } from '@/lib/graphql/chapter/ChapterQuery.ts';
|
2025-08-15 22:02:58 +02:00
|
|
|
import { CHAPTER_ACTION_TO_TRANSLATION } from '@/features/chapter/Chapter.constants.ts';
|
2026-03-11 00:11:29 +01:00
|
|
|
import type {
|
2025-05-05 01:52:51 +02:00
|
|
|
ChapterAction,
|
|
|
|
|
ChapterBookmarkInfo,
|
|
|
|
|
ChapterDownloadInfo,
|
|
|
|
|
ChapterIdInfo,
|
|
|
|
|
ChapterMangaInfo,
|
|
|
|
|
ChapterReadInfo,
|
|
|
|
|
ChapterRealUrlInfo,
|
2025-08-15 22:02:58 +02:00
|
|
|
} from '@/features/chapter/Chapter.types.ts';
|
2025-07-19 19:09:22 +02:00
|
|
|
import { IconWebView } from '@/assets/icons/IconWebView.tsx';
|
|
|
|
|
import { IconBrowser } from '@/assets/icons/IconBrowser.tsx';
|
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
|
|
|
handleSelection?: SelectableCollectionReturnType<TChapter['id']>['handleSelection'];
|
|
|
|
|
canBeDownloaded: boolean;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type SelectModeProps = {
|
2025-01-12 15:05:09 +01:00
|
|
|
selectedChapters: ComponentProps<typeof ChapterCard>['chapter'][];
|
2023-12-30 04:04:59 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type Props =
|
|
|
|
|
| (BaseProps & SingleModeProps & PropertiesNever<SelectModeProps>)
|
|
|
|
|
| (BaseProps & PropertiesNever<SingleModeProps> & SelectModeProps);
|
|
|
|
|
|
|
|
|
|
export const ChapterActionMenuItems = ({
|
|
|
|
|
chapter,
|
|
|
|
|
handleSelection,
|
|
|
|
|
canBeDownloaded = false,
|
2026-03-20 03:02:52 +01:00
|
|
|
selectedChapters = STABLE_EMPTY_ARRAY,
|
2023-12-30 04:04:59 +01:00
|
|
|
onClose,
|
2024-10-03 04:03:04 +02:00
|
|
|
selectable = true,
|
2023-12-30 04:04:59 +01:00
|
|
|
}: Props) => {
|
2026-01-10 19:45:02 +01:00
|
|
|
const { t } = useLingui();
|
2023-12-30 04:04:59 +01:00
|
|
|
|
|
|
|
|
const isSingleMode = !!chapter;
|
|
|
|
|
const { isDownloaded, isRead, isBookmarked } = chapter ?? {};
|
|
|
|
|
|
2025-01-12 19:08:46 +01:00
|
|
|
const mangaChaptersResponse = requestManager.useGetMangaChapters<GetChaptersMangaQuery>(
|
|
|
|
|
GET_CHAPTERS_MANGA,
|
|
|
|
|
chapter?.mangaId ?? -1,
|
|
|
|
|
{
|
|
|
|
|
skip: !chapter,
|
|
|
|
|
fetchPolicy: 'cache-only',
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
const allChapters = mangaChaptersResponse.data?.chapters.nodes ?? [];
|
|
|
|
|
|
2024-03-26 23:39:04 +01:00
|
|
|
const {
|
|
|
|
|
settings: { deleteChaptersWithBookmark },
|
|
|
|
|
} = useMetadataServerSettings();
|
|
|
|
|
|
2025-05-05 01:46:11 +02:00
|
|
|
const getMenuItemTitle = createGetMenuItemTitle(isSingleMode, CHAPTER_ACTION_TO_TRANSLATION);
|
2023-12-30 04:04:59 +01:00
|
|
|
const shouldShowMenuItem = createShouldShowMenuItem(isSingleMode);
|
|
|
|
|
const isMenuItemDisabled = createIsMenuItemDisabled(isSingleMode);
|
|
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
downloadableChapters,
|
|
|
|
|
downloadedChapters,
|
|
|
|
|
unbookmarkedChapters,
|
|
|
|
|
bookmarkedChapters,
|
|
|
|
|
unreadChapters,
|
|
|
|
|
readChapters,
|
|
|
|
|
} = useMemo(
|
|
|
|
|
() => ({
|
2025-01-12 15:05:09 +01:00
|
|
|
downloadableChapters: Chapters.getDownloadable(selectedChapters),
|
|
|
|
|
downloadedChapters: Chapters.getDownloaded(selectedChapters),
|
|
|
|
|
unbookmarkedChapters: Chapters.getNonBookmarked(selectedChapters),
|
|
|
|
|
bookmarkedChapters: Chapters.getBookmarked(selectedChapters),
|
|
|
|
|
unreadChapters: Chapters.getNonRead(selectedChapters),
|
|
|
|
|
readChapters: Chapters.getRead(selectedChapters),
|
2023-12-30 04:04:59 +01:00
|
|
|
}),
|
|
|
|
|
[selectedChapters],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const handleSelect = () => {
|
|
|
|
|
handleSelection?.(chapter.id, true);
|
|
|
|
|
onClose();
|
|
|
|
|
};
|
|
|
|
|
|
2025-01-12 15:05:09 +01:00
|
|
|
const performAction = (action: ChapterAction | 'mark_prev_as_read', chapters: TChapter[]) => {
|
2023-12-30 04:04:59 +01:00
|
|
|
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) {
|
2025-01-12 15:05:09 +01:00
|
|
|
return chapters;
|
2023-12-30 04:04:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!isMarkPrevAsRead) {
|
|
|
|
|
return [chapter];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const index = allChapters.findIndex(({ id: chapterId }) => chapterId === chapter.id);
|
|
|
|
|
|
|
|
|
|
const isFirstChapter = index + 1 > allChapters.length - 1;
|
|
|
|
|
if (isFirstChapter) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-25 01:50:04 +02:00
|
|
|
const previousChapters = allChapters.slice(index + 1);
|
|
|
|
|
|
|
|
|
|
return Chapters.getNonRead(previousChapters);
|
2023-12-30 04:04:59 +01:00
|
|
|
};
|
|
|
|
|
|
2025-01-12 15:05:09 +01:00
|
|
|
const chaptersToUpdate = getChapters();
|
2024-03-27 21:11:41 +01:00
|
|
|
|
2025-01-12 15:05:09 +01:00
|
|
|
if (!chaptersToUpdate.length) {
|
2024-03-27 21:11:41 +01:00
|
|
|
onClose();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-12 15:05:09 +01:00
|
|
|
Chapters.performAction(actualAction, Chapters.getIds(chaptersToUpdate), {
|
|
|
|
|
chapters: chaptersToUpdate,
|
2023-12-30 04:04:59 +01:00
|
|
|
wasManuallyMarkedAsRead: true,
|
2025-01-12 15:05:09 +01:00
|
|
|
trackProgressMangaId: chaptersToUpdate[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 && (
|
2026-01-10 19:45:02 +01:00
|
|
|
<MenuItem onClick={handleSelect} Icon={CheckBoxOutlineBlank} title={t`Select`} />
|
2023-12-30 04:04:59 +01:00
|
|
|
)}
|
2024-03-02 16:11:39 +01:00
|
|
|
{isSingleMode && (
|
2025-07-19 19:09:22 +02:00
|
|
|
<>
|
|
|
|
|
<MenuItem
|
|
|
|
|
Icon={IconBrowser}
|
|
|
|
|
disabled={!chapter!.realUrl}
|
|
|
|
|
onClick={() => {
|
|
|
|
|
window.open(chapter!.realUrl!, '_blank', 'noopener,noreferrer');
|
|
|
|
|
onClose();
|
|
|
|
|
}}
|
2026-01-10 19:45:02 +01:00
|
|
|
title={t`Open in browser`}
|
2025-07-19 19:09:22 +02:00
|
|
|
/>
|
|
|
|
|
<MenuItem
|
|
|
|
|
Icon={IconWebView}
|
|
|
|
|
disabled={!chapter!.realUrl}
|
|
|
|
|
onClick={() => {
|
|
|
|
|
window.open(
|
|
|
|
|
requestManager.getWebviewUrl(chapter!.realUrl!),
|
|
|
|
|
'_blank',
|
|
|
|
|
'noopener,noreferrer',
|
|
|
|
|
);
|
|
|
|
|
onClose();
|
|
|
|
|
}}
|
2026-01-10 19:45:02 +01:00
|
|
|
title={t`Open in WebView`}
|
2025-07-19 19:09:22 +02:00
|
|
|
/>
|
|
|
|
|
</>
|
2024-03-02 16:11:39 +01:00
|
|
|
)}
|
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={() =>
|
2025-01-12 15:05:09 +01:00
|
|
|
performAction('delete', Chapters.getDeletable(downloadedChapters, deleteChaptersWithBookmark))
|
2024-03-26 23:39:04 +01:00
|
|
|
}
|
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}
|
2026-01-10 19:45:02 +01:00
|
|
|
title={t`Mark previous as read`}
|
2023-12-30 04:04:59 +01:00
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
};
|