2024-04-07 15:33:08 +02: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/.
|
|
|
|
|
*/
|
|
|
|
|
|
2024-05-08 13:45:42 +02:00
|
|
|
import { useCallback, useState } from 'react';
|
2024-04-07 15:33:08 +02:00
|
|
|
import { useTranslation } from 'react-i18next';
|
2024-05-04 14:43:41 +02:00
|
|
|
import { useNavigate } from 'react-router-dom';
|
2024-07-09 23:46:59 +02:00
|
|
|
import gql from 'graphql-tag';
|
2024-10-05 21:39:58 +02:00
|
|
|
import { useCategorySelect } from '@/modules/category/hooks/useCategorySelect.tsx';
|
2024-10-06 01:27:51 +02:00
|
|
|
import { requestManager } from '@/lib/requests/RequestManager.ts';
|
2024-10-05 23:22:42 +02:00
|
|
|
import { makeToast } from '@/modules/core/utils/Toast.ts';
|
2024-10-05 22:53:22 +02:00
|
|
|
import { getMetadataServerSettings } from '@/modules/settings/services/ServerSettingsMetadata.ts';
|
2024-10-05 21:39:58 +02:00
|
|
|
import { Categories } from '@/modules/category/services/Categories.ts';
|
2024-10-05 16:09:34 +02:00
|
|
|
import { defaultPromiseErrorHandler } from '@/lib/DefaultPromiseErrorHandler.ts';
|
2024-10-05 19:21:26 +02:00
|
|
|
import { Mangas } from '@/modules/manga/services/Mangas.ts';
|
2024-10-05 23:22:42 +02:00
|
|
|
import { awaitConfirmation } from '@/modules/core/utils/AwaitableDialog.tsx';
|
2024-07-08 18:02:50 +02:00
|
|
|
import { GetCategoriesBaseQuery, GetCategoriesBaseQueryVariables, MangaType } from '@/lib/graphql/generated/graphql.ts';
|
2024-07-01 21:40:27 +02:00
|
|
|
import { GET_CATEGORIES_BASE } from '@/lib/graphql/queries/CategoryQuery.ts';
|
2024-12-11 20:10:59 +01:00
|
|
|
import { AppRoutes } from '@/modules/core/AppRoute.constants.ts';
|
2024-12-20 17:24:40 +01:00
|
|
|
import { getErrorMessage } from '@/lib/HelperFunctions.ts';
|
2024-04-07 15:33:08 +02:00
|
|
|
|
2024-04-21 03:50:41 +02:00
|
|
|
export const useManageMangaLibraryState = (
|
2024-07-08 18:02:50 +02:00
|
|
|
manga: Pick<MangaType, 'id' | 'title'> & Partial<Pick<MangaType, 'inLibrary'>>,
|
2024-04-21 03:50:41 +02:00
|
|
|
confirmRemoval: boolean = false,
|
|
|
|
|
) => {
|
2024-04-07 15:33:08 +02:00
|
|
|
const { t } = useTranslation();
|
2024-05-04 14:43:41 +02:00
|
|
|
const navigate = useNavigate();
|
2024-04-07 15:33:08 +02:00
|
|
|
|
2024-07-08 18:02:50 +02:00
|
|
|
const [isInLibrary, setIsInLibrary] = useState(!!manga.inLibrary);
|
2024-04-07 15:33:08 +02:00
|
|
|
|
|
|
|
|
const addToLibrary = useCallback(
|
|
|
|
|
(didSubmit: boolean, addToCategories: number[] = [], removeFromCategories: number[] = []) => {
|
|
|
|
|
if (!didSubmit) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
requestManager
|
|
|
|
|
.updateManga(manga.id, {
|
|
|
|
|
updateManga: { inLibrary: true },
|
|
|
|
|
updateMangaCategories: { addToCategories, removeFromCategories },
|
|
|
|
|
})
|
|
|
|
|
.response.then(() => makeToast(t('library.info.label.added_to_library'), 'success'))
|
|
|
|
|
.then(() => setIsInLibrary(true))
|
2024-12-20 17:24:40 +01:00
|
|
|
.catch((e) => {
|
|
|
|
|
makeToast(t('library.error.label.add_to_library'), 'error', getErrorMessage(e));
|
2024-04-07 15:33:08 +02:00
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
[manga.id],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const removeFromLibrary = useCallback(async () => {
|
2024-04-21 03:50:41 +02:00
|
|
|
if (confirmRemoval) {
|
|
|
|
|
await awaitConfirmation({
|
|
|
|
|
title: t('global.label.are_you_sure'),
|
|
|
|
|
message: t('manga.action.library.remove.dialog.label.message', { title: manga.title }),
|
|
|
|
|
actions: {
|
|
|
|
|
confirm: { title: t('global.button.remove') },
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-07 15:33:08 +02:00
|
|
|
await Mangas.removeFromLibrary([manga.id]);
|
|
|
|
|
setIsInLibrary(false);
|
2024-04-21 03:50:41 +02:00
|
|
|
}, [manga.id, confirmRemoval]);
|
2024-04-07 15:33:08 +02:00
|
|
|
|
|
|
|
|
const { openCategorySelect, CategorySelectComponent } = useCategorySelect({
|
|
|
|
|
mangaId: manga.id,
|
|
|
|
|
addToLibrary: true,
|
|
|
|
|
onClose: addToLibrary,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const updateLibraryState = useCallback(() => {
|
2024-05-04 14:43:41 +02:00
|
|
|
const update = async () => {
|
|
|
|
|
if (isInLibrary) {
|
|
|
|
|
removeFromLibrary().catch(
|
|
|
|
|
defaultPromiseErrorHandler('useManageMangaLibraryState::updateLibraryState::removeFromLibrary'),
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-04-07 15:33:08 +02:00
|
|
|
|
2024-05-08 13:45:42 +02:00
|
|
|
let showAddToLibraryCategorySelectDialog: boolean;
|
|
|
|
|
try {
|
|
|
|
|
showAddToLibraryCategorySelectDialog = (await getMetadataServerSettings())
|
|
|
|
|
.showAddToLibraryCategorySelectDialog;
|
|
|
|
|
} catch (e) {
|
2024-12-20 17:24:40 +01:00
|
|
|
makeToast(t('global.error.label.failed_to_load_data'), 'error', getErrorMessage(e));
|
2024-05-04 14:43:41 +02:00
|
|
|
return;
|
|
|
|
|
}
|
2024-04-07 15:33:08 +02:00
|
|
|
|
2024-07-01 21:40:27 +02:00
|
|
|
let categories: Awaited<
|
|
|
|
|
ReturnType<
|
|
|
|
|
typeof requestManager.getCategories<GetCategoriesBaseQuery, GetCategoriesBaseQueryVariables>
|
|
|
|
|
>['response']
|
|
|
|
|
>;
|
2024-05-08 13:45:42 +02:00
|
|
|
try {
|
2024-07-01 21:40:27 +02:00
|
|
|
categories = await requestManager.getCategories<
|
|
|
|
|
GetCategoriesBaseQuery,
|
|
|
|
|
GetCategoriesBaseQueryVariables
|
|
|
|
|
>(GET_CATEGORIES_BASE).response;
|
2024-05-08 13:45:42 +02:00
|
|
|
} catch (e) {
|
2024-12-20 17:24:40 +01:00
|
|
|
makeToast(t('category.error.label.request_failure'), 'error', getErrorMessage(e));
|
2024-05-04 14:43:41 +02:00
|
|
|
return;
|
|
|
|
|
}
|
2024-05-08 13:45:42 +02:00
|
|
|
const userCreatedCategories = Categories.getUserCreated(categories.data.categories.nodes);
|
2024-04-07 15:33:08 +02:00
|
|
|
|
2024-05-04 14:43:41 +02:00
|
|
|
let duplicatedLibraryMangas:
|
|
|
|
|
| Awaited<ReturnType<typeof Mangas.getDuplicateLibraryMangas>['response']>
|
|
|
|
|
| undefined;
|
|
|
|
|
try {
|
|
|
|
|
duplicatedLibraryMangas = await Mangas.getDuplicateLibraryMangas(manga.title).response;
|
2024-12-21 23:27:03 +01:00
|
|
|
} catch (e) {
|
2024-05-04 14:43:41 +02:00
|
|
|
await awaitConfirmation({
|
|
|
|
|
title: t('global.error.label.failed_to_load_data'),
|
|
|
|
|
message: t('manga.action.library.add.dialog.duplicate.label.failure', {
|
2024-12-21 23:27:03 +01:00
|
|
|
error: getErrorMessage(e),
|
2024-05-04 14:43:41 +02:00
|
|
|
}),
|
|
|
|
|
actions: {
|
2024-05-04 14:50:27 +02:00
|
|
|
extra: { show: true, title: t('global.button.retry'), contain: true },
|
2024-05-04 14:43:41 +02:00
|
|
|
confirm: { title: t('global.button.add') },
|
|
|
|
|
},
|
|
|
|
|
onExtra: () =>
|
|
|
|
|
update().catch(
|
|
|
|
|
defaultPromiseErrorHandler('useManageMangaLibraryState::update: retry duplicate check'),
|
|
|
|
|
),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const doDuplicatesExist = duplicatedLibraryMangas?.data.mangas.totalCount;
|
|
|
|
|
if (doDuplicatesExist) {
|
|
|
|
|
await awaitConfirmation({
|
|
|
|
|
title: t('global.label.are_you_sure'),
|
|
|
|
|
message: t('manga.action.library.add.dialog.duplicate.label.info'),
|
|
|
|
|
actions: {
|
2024-05-04 14:50:27 +02:00
|
|
|
extra: { show: true, title: t('migrate.dialog.action.button.show_entry'), contain: true },
|
2024-05-04 14:43:41 +02:00
|
|
|
confirm: { title: t('global.button.add') },
|
|
|
|
|
},
|
2024-12-11 20:10:59 +01:00
|
|
|
onExtra: () => navigate(AppRoutes.manga.path(duplicatedLibraryMangas!.data.mangas.nodes[0].id)),
|
2024-05-04 14:43:41 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const showCategorySelectDialog = showAddToLibraryCategorySelectDialog && !!userCreatedCategories.length;
|
|
|
|
|
if (!showCategorySelectDialog) {
|
|
|
|
|
addToLibrary(true, Categories.getIds(Categories.getDefaults(userCreatedCategories!)));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
openCategorySelect(true);
|
|
|
|
|
};
|
2024-04-07 15:33:08 +02:00
|
|
|
|
2024-05-04 14:43:41 +02:00
|
|
|
update().catch(defaultPromiseErrorHandler('useManageMangaLibraryState::updateLibraryState'));
|
2024-05-08 13:45:42 +02:00
|
|
|
}, [isInLibrary, removeFromLibrary, addToLibrary]);
|
2024-04-07 15:33:08 +02:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
CategorySelectComponent,
|
|
|
|
|
updateLibraryState,
|
|
|
|
|
/**
|
|
|
|
|
* In case of browsing the source, the data has to be fetched via a mutation.
|
|
|
|
|
* Thus, the source browse data never has the updated in library state unless it has to rerender, which does not get
|
|
|
|
|
* triggered by updating the manga in this hook.
|
|
|
|
|
*
|
|
|
|
|
* To work around this issue, the currently known in library state gets returned here
|
|
|
|
|
*/
|
2024-07-09 23:46:59 +02:00
|
|
|
isInLibrary:
|
|
|
|
|
Mangas.getFromCache(
|
|
|
|
|
manga.id,
|
|
|
|
|
gql`
|
|
|
|
|
fragment MangaInLibraryState on MangaType {
|
|
|
|
|
inLibrary
|
|
|
|
|
}
|
|
|
|
|
`,
|
|
|
|
|
'MangaInLibraryState',
|
|
|
|
|
)?.inLibrary ?? isInLibrary,
|
2024-04-07 15:33:08 +02:00
|
|
|
};
|
|
|
|
|
};
|