2021-03-26 04:17:02 +04:30
|
|
|
/*
|
|
|
|
|
* Copyright (C) Contributors to the Suwayomi project
|
|
|
|
|
*
|
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
2021-02-21 04:27:41 +03:30
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
2023-05-18 13:17:41 +02:00
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
2021-02-21 04:27:41 +03:30
|
|
|
|
2023-12-27 17:20:28 +01:00
|
|
|
import { useEffect, useMemo } from 'react';
|
2021-09-09 17:51:22 +04:30
|
|
|
import Button from '@mui/material/Button';
|
|
|
|
|
import DialogTitle from '@mui/material/DialogTitle';
|
|
|
|
|
import DialogContent from '@mui/material/DialogContent';
|
|
|
|
|
import DialogActions from '@mui/material/DialogActions';
|
|
|
|
|
import Dialog from '@mui/material/Dialog';
|
|
|
|
|
import FormGroup from '@mui/material/FormGroup';
|
2023-03-23 13:59:32 +01:00
|
|
|
import { useTranslation } from 'react-i18next';
|
2023-10-28 00:32:02 +02:00
|
|
|
import { requestManager } from '@/lib/requests/RequestManager.ts';
|
2023-12-25 21:37:45 +01:00
|
|
|
import { Mangas } from '@/lib/data/Mangas.ts';
|
|
|
|
|
import { useSelectableCollection } from '@/components/collection/useSelectableCollection.ts';
|
|
|
|
|
import { ThreeStateCheckboxInput } from '@/components/atoms/ThreeStateCheckboxInput.tsx';
|
2023-12-27 18:08:16 +01:00
|
|
|
import { TCategory } from '@/typings.ts';
|
2021-02-21 04:27:41 +03:30
|
|
|
|
2023-12-25 21:37:45 +01:00
|
|
|
type BaseProps = {
|
2023-02-06 10:06:33 +01:00
|
|
|
open: boolean;
|
2023-12-27 18:08:16 +01:00
|
|
|
onClose: (didUpdateCategories: boolean) => void;
|
2023-12-25 21:37:45 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type SingleMangaModeProps = {
|
2023-02-06 10:06:33 +01:00
|
|
|
mangaId: number;
|
2023-12-27 18:08:16 +01:00
|
|
|
addToLibrary?: boolean;
|
2023-12-25 21:37:45 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type MultiMangaModeProps = {
|
|
|
|
|
mangaIds: number[];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type Props =
|
|
|
|
|
| (BaseProps & SingleMangaModeProps & PropertiesNever<MultiMangaModeProps>)
|
|
|
|
|
| (BaseProps & PropertiesNever<SingleMangaModeProps> & MultiMangaModeProps);
|
|
|
|
|
|
|
|
|
|
const useGetMangaCategoryIds = (mangaId: number | undefined): number[] => {
|
|
|
|
|
const { data: mangaResult } = requestManager.useGetManga(mangaId ?? -1, { skip: mangaId === undefined });
|
|
|
|
|
|
|
|
|
|
return useMemo(() => {
|
|
|
|
|
if (mangaId === undefined || !mangaResult) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return mangaResult.manga.categories.nodes.map((category) => category.id);
|
|
|
|
|
}, [mangaResult?.manga.categories.nodes, mangaId]);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const getCategoryCheckedState = (
|
|
|
|
|
categoryId: number,
|
|
|
|
|
categoriesToAdd: number[],
|
|
|
|
|
categoriesToRemove: number[],
|
|
|
|
|
isSingleSelectionMode: boolean,
|
|
|
|
|
): boolean | undefined => {
|
|
|
|
|
if (categoriesToAdd.includes(categoryId)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isSingleSelectionMode) {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (categoriesToRemove.includes(categoryId)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-02-21 04:27:41 +03:30
|
|
|
|
2023-12-25 21:37:45 +01:00
|
|
|
return undefined;
|
|
|
|
|
};
|
|
|
|
|
|
2023-12-27 18:08:16 +01:00
|
|
|
const getDefaultCategoryIds = (categories: TCategory[]) =>
|
|
|
|
|
categories.filter(({ default: isDefault }) => isDefault).map(({ id }) => id);
|
|
|
|
|
|
2023-12-25 21:37:45 +01:00
|
|
|
export function CategorySelect(props: Props) {
|
2023-03-23 13:59:32 +01:00
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
2023-12-27 18:08:16 +01:00
|
|
|
const { open, onClose, mangaId, mangaIds: passedMangaIds, addToLibrary = false } = props;
|
2023-12-25 21:37:45 +01:00
|
|
|
|
|
|
|
|
const isSingleSelectionMode = mangaId !== undefined;
|
|
|
|
|
const mangaIds = passedMangaIds ?? [mangaId];
|
2021-02-21 04:27:41 +03:30
|
|
|
|
2023-12-27 17:20:28 +01:00
|
|
|
const mangaCategoryIds = useGetMangaCategoryIds(mangaId);
|
2023-09-01 20:39:30 +02:00
|
|
|
const { data } = requestManager.useGetCategories();
|
|
|
|
|
const categoriesData = data?.categories.nodes;
|
2022-11-02 21:48:22 +01:00
|
|
|
|
|
|
|
|
const allCategories = useMemo(() => {
|
|
|
|
|
const cats = [...(categoriesData ?? [])]; // make copy
|
|
|
|
|
if (cats.length > 0 && cats[0].name === 'Default') {
|
|
|
|
|
cats.shift(); // remove first category if it is 'Default'
|
|
|
|
|
}
|
|
|
|
|
return cats;
|
|
|
|
|
}, [categoriesData]);
|
2021-02-21 04:27:41 +03:30
|
|
|
|
2023-12-27 18:08:16 +01:00
|
|
|
const defaultCategoryIds = useMemo(
|
|
|
|
|
() => (addToLibrary ? getDefaultCategoryIds(allCategories) : []),
|
|
|
|
|
[allCategories],
|
|
|
|
|
);
|
|
|
|
|
|
2023-12-25 21:37:45 +01:00
|
|
|
const { handleSelection, setSelectionForKey, getSelectionForKey } = useSelectableCollection<
|
|
|
|
|
number,
|
|
|
|
|
'categoriesToAdd' | 'categoriesToRemove'
|
|
|
|
|
>(allCategories.length, {
|
|
|
|
|
currentKey: 'categoriesToAdd',
|
|
|
|
|
initialState: {
|
2023-12-27 18:08:16 +01:00
|
|
|
categoriesToAdd: [...mangaCategoryIds, ...defaultCategoryIds],
|
2023-12-25 21:37:45 +01:00
|
|
|
categoriesToRemove: [],
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2023-12-27 17:20:28 +01:00
|
|
|
useEffect(() => {
|
2023-12-27 18:08:16 +01:00
|
|
|
setSelectionForKey('categoriesToAdd', [...mangaCategoryIds, ...defaultCategoryIds]);
|
2023-12-27 17:20:28 +01:00
|
|
|
setSelectionForKey('categoriesToRemove', []);
|
|
|
|
|
}, [mangaCategoryIds]);
|
|
|
|
|
|
2023-12-25 21:37:45 +01:00
|
|
|
const categoriesToAdd = getSelectionForKey('categoriesToAdd');
|
|
|
|
|
const categoriesToRemove = getSelectionForKey('categoriesToRemove');
|
2021-02-21 04:27:41 +03:30
|
|
|
|
|
|
|
|
const handleCancel = () => {
|
2023-12-27 17:20:28 +01:00
|
|
|
setSelectionForKey('categoriesToAdd', mangaCategoryIds);
|
2023-12-25 21:37:45 +01:00
|
|
|
setSelectionForKey('categoriesToRemove', []);
|
2023-12-27 18:08:16 +01:00
|
|
|
onClose(false);
|
2021-02-21 04:27:41 +03:30
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleOk = () => {
|
2023-12-27 18:08:16 +01:00
|
|
|
onClose(true);
|
2021-02-21 04:27:41 +03:30
|
|
|
|
2023-12-25 21:37:45 +01:00
|
|
|
const addToCategories = isSingleSelectionMode
|
2023-12-27 17:20:28 +01:00
|
|
|
? categoriesToAdd.filter((categoryId) => !mangaCategoryIds.includes(categoryId))
|
2023-12-25 21:37:45 +01:00
|
|
|
: categoriesToAdd;
|
|
|
|
|
const removeFromCategories = isSingleSelectionMode
|
2023-12-27 17:20:28 +01:00
|
|
|
? mangaCategoryIds.filter((categoryId) => !categoriesToAdd.includes(categoryId))
|
2023-12-25 21:37:45 +01:00
|
|
|
: categoriesToRemove;
|
|
|
|
|
|
|
|
|
|
const isUpdateRequired = !!addToCategories.length || !!removeFromCategories.length;
|
|
|
|
|
if (!isUpdateRequired) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Mangas.performAction('change_categories', mangaIds, {
|
|
|
|
|
changeCategoriesPatch: {
|
|
|
|
|
addToCategories,
|
|
|
|
|
removeFromCategories,
|
2023-09-01 20:33:35 +02:00
|
|
|
},
|
2023-09-24 17:59:29 +02:00
|
|
|
});
|
2021-02-21 04:27:41 +03:30
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Dialog
|
2021-11-15 19:32:25 +03:30
|
|
|
sx={{
|
|
|
|
|
'.MuiDialog-paper': {
|
|
|
|
|
maxHeight: 435,
|
|
|
|
|
width: '80%',
|
|
|
|
|
},
|
|
|
|
|
}}
|
2021-02-21 04:27:41 +03:30
|
|
|
maxWidth="xs"
|
|
|
|
|
open={open}
|
|
|
|
|
>
|
2023-03-23 13:59:32 +01:00
|
|
|
<DialogTitle>{t('category.title.set_categories')}</DialogTitle>
|
2021-02-21 04:27:41 +03:30
|
|
|
<DialogContent dividers>
|
|
|
|
|
<FormGroup>
|
2023-02-06 10:06:33 +01:00
|
|
|
{allCategories.length === 0 && (
|
|
|
|
|
<span>
|
2023-03-23 13:59:32 +01:00
|
|
|
{t('category.error.no_categories_found.label.info')}
|
2023-02-06 10:06:33 +01:00
|
|
|
<br />
|
2023-03-23 13:59:32 +01:00
|
|
|
{t('category.error.no_categories_found.label.hint')}
|
2023-02-06 10:06:33 +01:00
|
|
|
</span>
|
|
|
|
|
)}
|
2022-11-02 21:48:22 +01:00
|
|
|
{allCategories.map((category) => (
|
2023-12-25 21:37:45 +01:00
|
|
|
<ThreeStateCheckboxInput
|
|
|
|
|
checked={getCategoryCheckedState(
|
|
|
|
|
category.id,
|
|
|
|
|
categoriesToAdd,
|
|
|
|
|
categoriesToRemove,
|
|
|
|
|
isSingleSelectionMode,
|
|
|
|
|
)}
|
|
|
|
|
onChange={(checked) => {
|
|
|
|
|
handleSelection(category.id, false, 'categoriesToAdd');
|
|
|
|
|
handleSelection(category.id, false, 'categoriesToRemove');
|
|
|
|
|
|
|
|
|
|
if (checked) {
|
|
|
|
|
handleSelection(category.id, true, 'categoriesToAdd');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (checked === false) {
|
|
|
|
|
handleSelection(category.id, true, 'categoriesToRemove');
|
|
|
|
|
}
|
|
|
|
|
}}
|
2022-11-02 21:48:22 +01:00
|
|
|
label={category.name}
|
|
|
|
|
key={category.id}
|
2021-02-21 04:27:41 +03:30
|
|
|
/>
|
|
|
|
|
))}
|
|
|
|
|
</FormGroup>
|
|
|
|
|
</DialogContent>
|
|
|
|
|
<DialogActions>
|
|
|
|
|
<Button autoFocus onClick={handleCancel} color="primary">
|
2023-03-23 13:59:32 +01:00
|
|
|
{t('global.button.cancel')}
|
2021-02-21 04:27:41 +03:30
|
|
|
</Button>
|
|
|
|
|
<Button onClick={handleOk} color="primary">
|
2023-03-23 13:59:32 +01:00
|
|
|
{t('global.button.ok')}
|
2021-02-21 04:27:41 +03:30
|
|
|
</Button>
|
|
|
|
|
</DialogActions>
|
|
|
|
|
</Dialog>
|
|
|
|
|
);
|
|
|
|
|
}
|