Reduce active apollo queries
This commit is contained in:
@@ -6,13 +6,13 @@
|
|||||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { useCallback, useMemo, useState } from 'react';
|
import { useCallback, useState } from 'react';
|
||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
import { useNavigate } from 'react-router-dom';
|
import { useNavigate } from 'react-router-dom';
|
||||||
import { useCategorySelect } from '@/components/navbar/action/useCategorySelect.tsx';
|
import { useCategorySelect } from '@/components/navbar/action/useCategorySelect.tsx';
|
||||||
import { requestManager } from '@/lib/requests/RequestManager.ts';
|
import { requestManager } from '@/lib/requests/RequestManager.ts';
|
||||||
import { makeToast } from '@/components/util/Toast.tsx';
|
import { makeToast } from '@/components/util/Toast.tsx';
|
||||||
import { useMetadataServerSettings } from '@/lib/metadata/metadataServerSettings.ts';
|
import { getMetadataServerSettings } from '@/lib/metadata/metadataServerSettings.ts';
|
||||||
import { Categories } from '@/lib/data/Categories.ts';
|
import { Categories } from '@/lib/data/Categories.ts';
|
||||||
import { defaultPromiseErrorHandler } from '@/util/defaultPromiseErrorHandler.ts';
|
import { defaultPromiseErrorHandler } from '@/util/defaultPromiseErrorHandler.ts';
|
||||||
import { Mangas } from '@/lib/data/Mangas.ts';
|
import { Mangas } from '@/lib/data/Mangas.ts';
|
||||||
@@ -28,17 +28,6 @@ export const useManageMangaLibraryState = (
|
|||||||
|
|
||||||
const [isInLibrary, setIsInLibrary] = useState(manga.inLibrary);
|
const [isInLibrary, setIsInLibrary] = useState(manga.inLibrary);
|
||||||
|
|
||||||
const {
|
|
||||||
settings: { showAddToLibraryCategorySelectDialog },
|
|
||||||
loading: areSettingsLoading,
|
|
||||||
} = useMetadataServerSettings();
|
|
||||||
|
|
||||||
const categories = requestManager.useGetCategories();
|
|
||||||
const userCreatedCategories = useMemo(
|
|
||||||
() => Categories.getUserCreated(categories.data?.categories.nodes ?? []),
|
|
||||||
[categories.data?.categories.nodes],
|
|
||||||
);
|
|
||||||
|
|
||||||
const addToLibrary = useCallback(
|
const addToLibrary = useCallback(
|
||||||
(didSubmit: boolean, addToCategories: number[] = [], removeFromCategories: number[] = []) => {
|
(didSubmit: boolean, addToCategories: number[] = [], removeFromCategories: number[] = []) => {
|
||||||
if (!didSubmit) {
|
if (!didSubmit) {
|
||||||
@@ -89,22 +78,23 @@ export const useManageMangaLibraryState = (
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (areSettingsLoading || categories.loading) {
|
let showAddToLibraryCategorySelectDialog: boolean;
|
||||||
makeToast(t('global.label.load_in_progress'), 'info');
|
try {
|
||||||
|
showAddToLibraryCategorySelectDialog = (await getMetadataServerSettings())
|
||||||
|
.showAddToLibraryCategorySelectDialog;
|
||||||
|
} catch (e) {
|
||||||
|
makeToast(t('global.error.label.failed_to_load_data'), 'error');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (categories.error) {
|
let categories: Awaited<ReturnType<typeof requestManager.getCategories>['response']>;
|
||||||
|
try {
|
||||||
|
categories = await requestManager.getCategories().response;
|
||||||
|
} catch (e) {
|
||||||
makeToast(t('category.error.label.request_failure'), 'error');
|
makeToast(t('category.error.label.request_failure'), 'error');
|
||||||
categories
|
|
||||||
.refetch()
|
|
||||||
.catch(
|
|
||||||
defaultPromiseErrorHandler(
|
|
||||||
'useManageMangaLibraryState::updateLibraryState: refetch categories',
|
|
||||||
),
|
|
||||||
);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
const userCreatedCategories = Categories.getUserCreated(categories.data.categories.nodes);
|
||||||
|
|
||||||
let duplicatedLibraryMangas:
|
let duplicatedLibraryMangas:
|
||||||
| Awaited<ReturnType<typeof Mangas.getDuplicateLibraryMangas>['response']>
|
| Awaited<ReturnType<typeof Mangas.getDuplicateLibraryMangas>['response']>
|
||||||
@@ -151,7 +141,7 @@ export const useManageMangaLibraryState = (
|
|||||||
};
|
};
|
||||||
|
|
||||||
update().catch(defaultPromiseErrorHandler('useManageMangaLibraryState::updateLibraryState'));
|
update().catch(defaultPromiseErrorHandler('useManageMangaLibraryState::updateLibraryState'));
|
||||||
}, [isInLibrary, removeFromLibrary, addToLibrary, areSettingsLoading, categories.loading]);
|
}, [isInLibrary, removeFromLibrary, addToLibrary]);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
CategorySelectComponent,
|
CategorySelectComponent,
|
||||||
|
|||||||
@@ -2036,6 +2036,19 @@ export class RequestManager {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public getCategories(
|
||||||
|
options?: QueryOptions<GetCategoriesQueryVariables, GetCategoriesQuery>,
|
||||||
|
): AbortabaleApolloQueryResponse<GetCategoriesQuery> {
|
||||||
|
return this.doRequest(
|
||||||
|
GQLMethod.QUERY,
|
||||||
|
GET_CATEGORIES,
|
||||||
|
{
|
||||||
|
orderBy: CategoryOrderBy.Order,
|
||||||
|
},
|
||||||
|
options,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
public createCategory(
|
public createCategory(
|
||||||
input: CreateCategoryInput,
|
input: CreateCategoryInput,
|
||||||
options?: MutationOptions<CreateCategoryMutation, CreateCategoryMutationVariables>,
|
options?: MutationOptions<CreateCategoryMutation, CreateCategoryMutationVariables>,
|
||||||
|
|||||||
Reference in New Issue
Block a user