Move collection files into new folder
This commit is contained in:
@@ -25,15 +25,15 @@ import { ResumeFab } from '@/modules/manga/components/ResumeFAB.tsx';
|
||||
import { filterAndSortChapters } from '@/modules/chapter/utils/ChapterList.util.tsx';
|
||||
import { EmptyViewAbsoluteCentered } from '@/modules/core/components/placeholder/EmptyViewAbsoluteCentered.tsx';
|
||||
import { ChaptersToolbarMenu } from '@/modules/chapter/components/ChaptersToolbarMenu.tsx';
|
||||
import { SelectionFAB } from '@/components/collection/SelectionFAB.tsx';
|
||||
import { SelectionFAB } from '@/modules/collection/components/SelectionFAB.tsx';
|
||||
import { DEFAULT_FULL_FAB_HEIGHT } from '@/modules/core/components/buttons/StyledFab.tsx';
|
||||
import {
|
||||
GetChaptersMangaQuery,
|
||||
GetChaptersMangaQueryVariables,
|
||||
MangaScreenFieldsFragment,
|
||||
} from '@/lib/graphql/generated/graphql.ts';
|
||||
import { useSelectableCollection } from '@/components/collection/useSelectableCollection.ts';
|
||||
import { SelectableCollectionSelectAll } from '@/components/collection/SelectableCollectionSelectAll.tsx';
|
||||
import { useSelectableCollection } from '@/modules/collection/hooks/useSelectableCollection.ts';
|
||||
import { SelectableCollectionSelectAll } from '@/modules/collection/components/SelectableCollectionSelectAll.tsx';
|
||||
import { Chapters } from '@/modules/chapter/services/Chapters.ts';
|
||||
import { ChaptersWithMeta, ChapterWithMetaType } from '@/modules/chapter/services/ChaptersWithMeta.ts';
|
||||
import { ChapterActionMenuItems } from '@/modules/chapter/components/actions/ChapterActionMenuItems.tsx';
|
||||
|
||||
@@ -17,7 +17,7 @@ import BookmarkAdd from '@mui/icons-material/BookmarkAdd';
|
||||
import DoneAll from '@mui/icons-material/DoneAll';
|
||||
import { useMemo } from 'react';
|
||||
import LaunchIcon from '@mui/icons-material/Launch';
|
||||
import { SelectableCollectionReturnType } from '@/components/collection/useSelectableCollection.ts';
|
||||
import { SelectableCollectionReturnType } from '@/modules/collection/hooks/useSelectableCollection.ts';
|
||||
import {
|
||||
actionToTranslationKey,
|
||||
ChapterAction,
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* 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 Tooltip from '@mui/material/Tooltip';
|
||||
import Checkbox from '@mui/material/Checkbox';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
|
||||
export const SelectableCollectionSelectAll = ({
|
||||
areAllItemsSelected,
|
||||
areNoItemsSelected,
|
||||
onChange,
|
||||
}: {
|
||||
areAllItemsSelected: boolean;
|
||||
areNoItemsSelected: boolean;
|
||||
onChange: (checked: boolean) => void;
|
||||
}) => {
|
||||
const { t } = useTranslation();
|
||||
|
||||
return (
|
||||
<Tooltip title={t(!areAllItemsSelected ? 'global.button.select_all' : 'global.button.clear')}>
|
||||
<Checkbox
|
||||
sx={{
|
||||
padding: '8px',
|
||||
color: 'inherit',
|
||||
'&.Mui-checked, &.MuiCheckbox-indeterminate': {
|
||||
color: 'inherit',
|
||||
},
|
||||
}}
|
||||
checked={areAllItemsSelected}
|
||||
indeterminate={!areNoItemsSelected && !areAllItemsSelected}
|
||||
onChange={(_, checked) => onChange(checked)}
|
||||
/>
|
||||
</Tooltip>
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* 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 Tooltip from '@mui/material/Tooltip';
|
||||
import Checkbox from '@mui/material/Checkbox';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import ClearIcon from '@mui/icons-material/Clear';
|
||||
import { SelectableCollectionSelectAll } from '@/modules/collection/components/SelectableCollectionSelectAll.tsx';
|
||||
|
||||
export const SelectableCollectionSelectMode = ({
|
||||
isActive,
|
||||
areAllItemsSelected,
|
||||
areNoItemsSelected,
|
||||
onSelectAll,
|
||||
onModeChange,
|
||||
}: {
|
||||
isActive: boolean;
|
||||
areAllItemsSelected: boolean;
|
||||
areNoItemsSelected: boolean;
|
||||
onSelectAll: (selectAll: boolean) => void;
|
||||
onModeChange: (checked: boolean) => void;
|
||||
}) => {
|
||||
const { t } = useTranslation();
|
||||
|
||||
return (
|
||||
<>
|
||||
{isActive && (
|
||||
<SelectableCollectionSelectAll
|
||||
areAllItemsSelected={areAllItemsSelected}
|
||||
areNoItemsSelected={areNoItemsSelected}
|
||||
onChange={onSelectAll}
|
||||
/>
|
||||
)}
|
||||
<Tooltip title={t(!isActive ? 'global.button.select_all' : 'global.button.cancel')}>
|
||||
<Checkbox
|
||||
checkedIcon={<ClearIcon />}
|
||||
sx={{
|
||||
padding: '8px',
|
||||
color: 'inherit',
|
||||
'&.Mui-checked': {
|
||||
color: 'inherit',
|
||||
},
|
||||
}}
|
||||
checked={isActive}
|
||||
onChange={(_, checked) => onModeChange(checked)}
|
||||
/>
|
||||
</Tooltip>
|
||||
</>
|
||||
);
|
||||
};
|
||||
64
src/modules/collection/components/SelectionFAB.tsx
Normal file
64
src/modules/collection/components/SelectionFAB.tsx
Normal file
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* 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';
|
||||
import Fab from '@mui/material/Fab';
|
||||
import Box from '@mui/material/Box';
|
||||
import { styled } from '@mui/material/styles';
|
||||
import React from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import PopupState, { bindMenu, bindTrigger } from 'material-ui-popup-state';
|
||||
import { DEFAULT_FAB_STYLE } from '@/modules/core/components/buttons/StyledFab.tsx';
|
||||
import { Menu } from '@/modules/core/components/menu/Menu.tsx';
|
||||
import { TranslationKey } from '@/Base.types.ts';
|
||||
|
||||
interface SelectionFABProps {
|
||||
children: (handleClose: () => void, setHideMenu: (hide: boolean) => void) => JSX.Element;
|
||||
selectedItemsCount: number;
|
||||
title: TranslationKey;
|
||||
}
|
||||
|
||||
const FabContainer = styled(Box)(({ theme }) => ({
|
||||
...DEFAULT_FAB_STYLE,
|
||||
height: `calc(${DEFAULT_FAB_STYLE.height} + 1)`,
|
||||
paddingTop: '8px',
|
||||
zIndex: 1, // the "Checkbox" (MUI) component of the "ChapterCard" has z-index 1, which causes it to take over the mouse events
|
||||
[theme.breakpoints.down('md')]: {
|
||||
marginBottom: '64px',
|
||||
},
|
||||
}));
|
||||
|
||||
export const SelectionFAB: React.FC<SelectionFABProps> = ({ children, selectedItemsCount, title }) => {
|
||||
const { t } = useTranslation();
|
||||
|
||||
return (
|
||||
<PopupState variant="popover" popupId="selection-fab-menu">
|
||||
{(popupState) => (
|
||||
<>
|
||||
<FabContainer {...bindTrigger(popupState)}>
|
||||
<Fab variant="extended" color="primary" id="selectionMenuButton">
|
||||
{`${selectedItemsCount} ${t(title, { count: selectedItemsCount })}`}
|
||||
<MoreHoriz sx={{ ml: 1 }} />
|
||||
</Fab>
|
||||
</FabContainer>
|
||||
<Menu
|
||||
{...bindMenu(popupState)}
|
||||
id="selectionMenu"
|
||||
anchorOrigin={{ horizontal: 'right', vertical: 'top' }}
|
||||
transformOrigin={{ horizontal: 'right', vertical: 'bottom' }}
|
||||
MenuListProps={{
|
||||
'aria-labelledby': 'selectionMenuButton',
|
||||
}}
|
||||
>
|
||||
{(onClose, setHideMenu) => children(onClose, setHideMenu)}
|
||||
</Menu>
|
||||
</>
|
||||
)}
|
||||
</PopupState>
|
||||
);
|
||||
};
|
||||
136
src/modules/collection/hooks/useSelectableCollection.ts
Normal file
136
src/modules/collection/hooks/useSelectableCollection.ts
Normal file
@@ -0,0 +1,136 @@
|
||||
/*
|
||||
* 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 { useRef, useState } from 'react';
|
||||
|
||||
export type SelectableCollectionReturnType<Id extends number | string, Key extends string = string> = {
|
||||
selectedItemIds: Id[];
|
||||
keySelectedItemIds: Id[];
|
||||
areAllItemsSelected: boolean;
|
||||
areNoItemsSelected: boolean;
|
||||
areAllItemsForKeySelected: boolean;
|
||||
areNoItemsForKeySelected: boolean;
|
||||
handleSelection: (id: Id, selected: boolean, options?: { selectRange?: boolean; key?: Key }) => void;
|
||||
handleSelectAll: (selectAll: boolean, ids: Id[], key?: Key) => void;
|
||||
setSelectionForKey: (key: Key, ids: Id[]) => void;
|
||||
getSelectionForKey: (key: Key) => Id[];
|
||||
clearSelection: () => void;
|
||||
};
|
||||
|
||||
export const useSelectableCollection = <Id extends number | string, Key extends string = 'default'>(
|
||||
totalCount: number,
|
||||
{
|
||||
itemIds = [],
|
||||
keyCount = totalCount,
|
||||
currentKey,
|
||||
initialState = {} as Record<Key, Id[]>,
|
||||
}: {
|
||||
itemIds?: Id[];
|
||||
keyCount?: number;
|
||||
currentKey: Key;
|
||||
initialState?: Record<Key, Id[]>;
|
||||
},
|
||||
): SelectableCollectionReturnType<Id, Key> => {
|
||||
const [keyToSelectedItemIds, setKeyToSelectedItemIds] = useState<Record<string, Id[]>>(initialState);
|
||||
|
||||
const lastSelectedItemInfoRef = useRef<{ id: Id; key: Key }>();
|
||||
|
||||
const selectedItemIds = [...new Set(Object.values(keyToSelectedItemIds).flat())];
|
||||
const areAllItemsSelected = selectedItemIds.length === totalCount;
|
||||
const areNoItemsSelected = !selectedItemIds.length;
|
||||
|
||||
const keySelectedItemIds = keyToSelectedItemIds[currentKey] ?? [];
|
||||
const areAllItemsForKeySelected = keySelectedItemIds.length === keyCount;
|
||||
const areNoItemsForKeySelected = keySelectedItemIds.length === 0;
|
||||
|
||||
if (areNoItemsForKeySelected) {
|
||||
lastSelectedItemInfoRef.current = undefined;
|
||||
}
|
||||
|
||||
const handleSelection: SelectableCollectionReturnType<Id, Key>['handleSelection'] = (
|
||||
id,
|
||||
selected,
|
||||
{ selectRange = false, key = currentKey } = {},
|
||||
) => {
|
||||
const deselect = !selected;
|
||||
|
||||
const { id: lastSelectedItemId, key: lastSelectedItemIdKey } = lastSelectedItemInfoRef.current ?? {};
|
||||
lastSelectedItemInfoRef.current = { id, key };
|
||||
|
||||
const isSelectRange = selectRange && key === lastSelectedItemIdKey && lastSelectedItemId !== undefined;
|
||||
|
||||
const indexOfLastSelectedItemId = isSelectRange ? itemIds.indexOf(lastSelectedItemId) : -1;
|
||||
const indexOfSelectedId = isSelectRange ? itemIds.indexOf(id) : -1;
|
||||
|
||||
const selectedIds = isSelectRange
|
||||
? itemIds.slice(
|
||||
Math.min(indexOfLastSelectedItemId, indexOfSelectedId),
|
||||
Math.max(indexOfLastSelectedItemId, indexOfSelectedId) + 1,
|
||||
)
|
||||
: [id];
|
||||
|
||||
if (deselect) {
|
||||
setKeyToSelectedItemIds((prevState) => ({
|
||||
...prevState,
|
||||
[key]: prevState[key]?.filter((selectedItemId) => !selectedIds.includes(selectedItemId)) ?? [],
|
||||
}));
|
||||
return;
|
||||
}
|
||||
|
||||
setKeyToSelectedItemIds((prevState) => ({
|
||||
...prevState,
|
||||
[key]: [...new Set([...(prevState[key] ?? []), ...selectedIds])],
|
||||
}));
|
||||
};
|
||||
|
||||
const handleSelectAll = (selectAll: boolean, ids: Id[], key: Key = currentKey) => {
|
||||
switch (selectAll) {
|
||||
case true:
|
||||
setKeyToSelectedItemIds((prevState) => ({
|
||||
...prevState,
|
||||
[key]: [...ids],
|
||||
}));
|
||||
break;
|
||||
case false:
|
||||
setKeyToSelectedItemIds((prevState) => ({
|
||||
...prevState,
|
||||
[key]: [],
|
||||
}));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
const setSelectionForKey = (key: Key, ids: Id[]) => {
|
||||
setKeyToSelectedItemIds((prevState) => ({
|
||||
...prevState,
|
||||
[key]: [...ids],
|
||||
}));
|
||||
};
|
||||
|
||||
const getSelectionForKey = (key: Key) => keyToSelectedItemIds[key];
|
||||
|
||||
const clearSelection = () => {
|
||||
setKeyToSelectedItemIds({});
|
||||
};
|
||||
|
||||
return {
|
||||
selectedItemIds,
|
||||
keySelectedItemIds,
|
||||
handleSelection,
|
||||
handleSelectAll,
|
||||
areAllItemsSelected,
|
||||
areNoItemsSelected,
|
||||
areAllItemsForKeySelected,
|
||||
areNoItemsForKeySelected,
|
||||
setSelectionForKey,
|
||||
getSelectionForKey,
|
||||
clearSelection,
|
||||
};
|
||||
};
|
||||
@@ -9,7 +9,7 @@
|
||||
import { LongPressPointerHandlers, LongPressResult } from 'use-long-press/lib/use-long-press.types';
|
||||
import { PopupState } from 'material-ui-popup-state/hooks';
|
||||
import { GridLayout } from '@/components/context/LibraryOptionsContext.tsx';
|
||||
import { SelectableCollectionReturnType } from '@/components/collection/useSelectableCollection.ts';
|
||||
import { SelectableCollectionReturnType } from '@/modules/collection/hooks/useSelectableCollection.ts';
|
||||
import { useManageMangaLibraryState } from '@/modules/manga/hooks/useManageMangaLibraryState.tsx';
|
||||
import { MangaThumbnailInfo } from '@/modules/manga/services/Mangas.ts';
|
||||
import { ChapterType, MangaType } from '@/lib/graphql/generated/graphql.ts';
|
||||
|
||||
@@ -27,7 +27,7 @@ import {
|
||||
Mangas,
|
||||
MangaUnreadInfo,
|
||||
} from '@/modules/manga/services/Mangas.ts';
|
||||
import { SelectableCollectionReturnType } from '@/components/collection/useSelectableCollection.ts';
|
||||
import { SelectableCollectionReturnType } from '@/modules/collection/hooks/useSelectableCollection.ts';
|
||||
import { MenuItem } from '@/modules/core/components/menu/MenuItem.tsx';
|
||||
import {
|
||||
createGetMenuItemTitle,
|
||||
|
||||
@@ -26,7 +26,7 @@ import { LoadingPlaceholder } from '@/modules/core/components/placeholder/Loadin
|
||||
import { MangaCard } from '@/modules/manga/components/cards/MangaCard.tsx';
|
||||
import { GridLayout } from '@/components/context/LibraryOptionsContext';
|
||||
import { useLocalStorage, useSessionStorage } from '@/modules/core/hooks/useStorage.tsx';
|
||||
import { SelectableCollectionReturnType } from '@/components/collection/useSelectableCollection.ts';
|
||||
import { SelectableCollectionReturnType } from '@/modules/collection/hooks/useSelectableCollection.ts';
|
||||
import { DEFAULT_FULL_FAB_HEIGHT } from '@/modules/core/components/buttons/StyledFab.tsx';
|
||||
import { AppStorage } from '@/lib/AppStorage.ts';
|
||||
import { MangaCardProps } from '@/modules/manga/MangaCard.types.tsx';
|
||||
|
||||
@@ -15,7 +15,7 @@ import IconButton from '@mui/material/IconButton';
|
||||
import MoreVertIcon from '@mui/icons-material/MoreVert';
|
||||
import { PopupState } from 'material-ui-popup-state/hooks';
|
||||
import { bindTrigger } from 'material-ui-popup-state';
|
||||
import { SelectableCollectionReturnType } from '@/components/collection/useSelectableCollection.ts';
|
||||
import { SelectableCollectionReturnType } from '@/modules/collection/hooks/useSelectableCollection.ts';
|
||||
import { MediaQuery } from '@/lib/ui/MediaQuery.tsx';
|
||||
import { MangaType } from '@/lib/graphql/generated/graphql.ts';
|
||||
|
||||
|
||||
Reference in New Issue
Block a user