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/. */
import MoreHoriz from '@mui/icons-material/MoreHoriz';
2023-02-06 10:06:33 +01:00
import { Fab, Menu } from '@mui/material';
2022-11-09 18:09:15 +01:00
import { Box } from '@mui/system';
import React, { useRef, useState } from 'react';
2023-02-05 16:15:20 +01:00
import type { IChapterWithMeta } from 'components/manga/ChapterList';
import SelectionFABActionItem from 'components/manga/SelectionFABActionItem';
2023-03-23 13:59:32 +01:00
import { useTranslation } from 'react-i18next';
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={{
2023-02-06 10:06:33 +01:00
position: 'fixed',
bottom: '2em',
right: '3em',
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(
({ chapter: c, downloadChapter: dc }) => !c.downloaded && dc === undefined,
)}
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"
matchingChapters={selectedChapters.filter(({ chapter }) => chapter.downloaded)}
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"
matchingChapters={selectedChapters.filter(({ chapter }) => !chapter.bookmarked)}
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"
matchingChapters={selectedChapters.filter(({ chapter }) => chapter.bookmarked)}
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"
matchingChapters={selectedChapters.filter(({ chapter }) => !chapter.read)}
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"
matchingChapters={selectedChapters.filter(({ chapter }) => chapter.read)}
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;