Files
suwayomi-material-you-webui/src/components/manga/useRefreshManga.ts
schroda d2f68041cb Handle missing chapters when refreshing manga
The server throws an error in case the source returns an empty chapter list.
However, this is not an error and thus should not be handled as one
2024-08-14 14:49:54 +02:00

37 lines
1.3 KiB
TypeScript

/*
* 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 { useCallback, useState } from 'react';
import { ApolloError } from '@apollo/client';
import { requestManager } from '@/lib/requests/RequestManager.ts';
import { baseCleanup } from '@/lib/data/Strings.ts';
export const useRefreshManga = (mangaId: string) => {
const [fetchingOnline, setFetchingOnline] = useState(false);
const [error, setError] = useState(null);
const handleRefresh = useCallback(async () => {
setFetchingOnline(true);
setError(null);
await Promise.all([
requestManager.getMangaFetch(mangaId, { awaitRefetchQueries: true }).response,
requestManager.getMangaChaptersFetch(mangaId, { awaitRefetchQueries: true }).response,
])
.catch((e) => {
if (e instanceof ApolloError && baseCleanup(e.message) === 'no chapters found') {
return;
}
setError(e);
})
.finally(() => setFetchingOnline(false));
}, [mangaId]);
return [handleRefresh, { loading: fetchingOnline, error }] as const;
};