Files
suwayomi-material-you-webui/src/modules/manga/hooks/useRefreshManga.ts
T

37 lines
1.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/.
*/
2022-11-09 18:09:15 +01:00
2023-12-30 14:30:04 +01:00
import { useCallback, useState } from 'react';
import { ApolloError } from '@apollo/client';
2024-10-06 01:27:51 +02:00
import { requestManager } from '@/lib/requests/RequestManager.ts';
2024-10-05 23:22:42 +02:00
import { baseCleanup } from '@/util/Strings.ts';
2022-11-09 18:09:15 +01:00
export const useRefreshManga = (mangaId: string) => {
const [fetchingOnline, setFetchingOnline] = useState(false);
2024-05-11 01:10:55 +02:00
const [error, setError] = useState(null);
2022-11-09 18:09:15 +01:00
const handleRefresh = useCallback(async () => {
setFetchingOnline(true);
2024-05-11 01:10:55 +02:00
setError(null);
2022-11-09 18:09:15 +01:00
await Promise.all([
2023-09-30 16:55:35 +02:00
requestManager.getMangaFetch(mangaId, { awaitRefetchQueries: true }).response,
requestManager.getMangaChaptersFetch(mangaId, { awaitRefetchQueries: true }).response,
2024-05-11 01:10:55 +02:00
])
.catch((e) => {
if (e instanceof ApolloError && baseCleanup(e.message) === 'no chapters found') {
return;
}
setError(e);
})
2024-05-11 01:10:55 +02:00
.finally(() => setFetchingOnline(false));
2022-11-09 18:09:15 +01:00
}, [mangaId]);
2024-05-11 01:10:55 +02:00
return [handleRefresh, { loading: fetchingOnline, error }] as const;
2022-11-09 18:09:15 +01:00
};