Files
suwayomi-material-you-webui/src/components/manga/SelectionFAB.tsx
T

107 lines
4.3 KiB
TypeScript
Raw Normal View History

2022-11-09 18:09:15 +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/.
*/
2022-11-09 18:09:15 +01:00
import MoreHoriz from '@mui/icons-material/MoreHoriz';
2023-06-04 21:08:39 +02:00
import { Fab, Menu, Box } from '@mui/material';
2022-11-09 18:09:15 +01:00
import React, { useRef, useState } from 'react';
2023-03-23 13:59:32 +01:00
import { useTranslation } from 'react-i18next';
2023-06-05 01:42:39 +02:00
import type { IChapterWithMeta } from '@/components/manga/ChapterList';
import SelectionFABActionItem from '@/components/manga/SelectionFABActionItem';
import { DEFAULT_FAB_STYLE } from '@/components/util/StyledFab';
2022-11-21 01:33:21 +01:00
2023-02-08 18:19:26 +01:00
export type SelectionAction = 'download' | 'delete' | 'bookmark' | 'unbookmark' | 'mark_as_read' | 'mark_as_unread';
2022-11-09 18:09:15 +01:00
2023-02-06 10:06:33 +01:00
interface SelectionFABProps {
selectedChapters: IChapterWithMeta[];
onAction: (action: SelectionAction, chapters: IChapterWithMeta[]) => void;
2022-11-09 18:09:15 +01:00
}
const SelectionFAB: React.FC<SelectionFABProps> = (props) => {
2023-03-23 13:59:32 +01:00
const { t } = useTranslation();
2022-11-09 18:09:15 +01:00
const { selectedChapters, onAction } = props;
const count = selectedChapters.length;
const anchorEl = useRef<HTMLElement>();
const [open, setOpen] = useState(false);
const handleClose = () => setOpen(false);
2022-11-21 01:33:21 +01:00
const handleAction = (action: SelectionAction, chapters: IChapterWithMeta[]) => {
onAction(action, chapters);
handleClose();
};
2022-11-09 18:09:15 +01:00
return (
<Box
sx={{
...DEFAULT_FAB_STYLE,
height: `calc(${DEFAULT_FAB_STYLE.height} + 1)`,
2023-02-06 10:06:33 +01:00
pt: 1,
2022-11-09 18:09:15 +01:00
}}
ref={anchorEl}
>
2023-02-08 18:19:26 +01:00
<Fab variant="extended" color="primary" id="selectionMenuButton" onClick={() => setOpen(true)}>
2023-03-23 13:59:32 +01:00
{`${count} ${t('chapter.title', { count })}`}
2022-11-09 18:09:15 +01:00
<MoreHoriz sx={{ ml: 1 }} />
</Fab>
<Menu
id="selectionMenu"
anchorEl={anchorEl.current}
open={open}
onClose={handleClose}
anchorOrigin={{ horizontal: 'right', vertical: 'top' }}
transformOrigin={{ horizontal: 'right', vertical: 'bottom' }}
MenuListProps={{
'aria-labelledby': 'selectionMenuButton',
}}
>
2022-11-21 01:33:21 +01:00
<SelectionFABActionItem
action="download"
matchingChapters={selectedChapters.filter(
2023-09-30 16:55:35 +02:00
({ chapter: c, downloadChapter: dc }) => !c.isDownloaded && dc === undefined,
2022-11-21 01:33:21 +01:00
)}
onClick={handleAction}
2023-03-23 13:59:32 +01:00
title={t('chapter.action.download.add.button.selected')}
2022-11-21 01:33:21 +01:00
/>
<SelectionFABActionItem
action="delete"
2023-09-30 16:55:35 +02:00
matchingChapters={selectedChapters.filter(({ chapter }) => chapter.isDownloaded)}
2022-11-21 01:33:21 +01:00
onClick={handleAction}
2023-03-23 13:59:32 +01:00
title={t('chapter.action.download.delete.button.selected')}
2022-11-21 01:33:21 +01:00
/>
<SelectionFABActionItem
action="bookmark"
2023-09-30 16:55:35 +02:00
matchingChapters={selectedChapters.filter(({ chapter }) => !chapter.isBookmarked)}
2022-11-21 01:33:21 +01:00
onClick={handleAction}
2023-03-23 13:59:32 +01:00
title={t('chapter.action.bookmark.add.button.selected')}
2022-11-21 01:33:21 +01:00
/>
<SelectionFABActionItem
action="unbookmark"
2023-09-30 16:55:35 +02:00
matchingChapters={selectedChapters.filter(({ chapter }) => chapter.isBookmarked)}
2022-11-21 01:33:21 +01:00
onClick={handleAction}
2023-03-23 13:59:32 +01:00
title={t('chapter.action.bookmark.remove.button.selected')}
2022-11-21 01:33:21 +01:00
/>
<SelectionFABActionItem
action="mark_as_read"
2023-09-30 16:55:35 +02:00
matchingChapters={selectedChapters.filter(({ chapter }) => !chapter.isRead)}
2022-11-21 01:33:21 +01:00
onClick={handleAction}
2023-03-23 13:59:32 +01:00
title={t('chapter.action.mark_as_read.add.button.selected')}
2022-11-21 01:33:21 +01:00
/>
<SelectionFABActionItem
action="mark_as_unread"
2023-09-30 16:55:35 +02:00
matchingChapters={selectedChapters.filter(({ chapter }) => chapter.isRead)}
2022-11-21 01:33:21 +01:00
onClick={handleAction}
2023-03-23 13:59:32 +01:00
title={t('chapter.action.mark_as_read.remove.button.selected')}
2022-11-21 01:33:21 +01:00
/>
2022-11-09 18:09:15 +01:00
</Menu>
</Box>
);
};
export default SelectionFAB;