Files
suwayomi-material-you-webui/src/components/manga/hooks.ts

48 lines
1.6 KiB
TypeScript
Raw Normal View History

/*
* 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/.
*/
2023-05-20 13:23:30 +02:00
import { useCallback, useEffect, useState } from 'react';
import { mutate } from 'swr';
2023-08-15 13:54:29 +02:00
import requestManager, { RequestManager } from '@/lib/requests/RequestManager.ts';
export const useRefreshManga = (mangaId: string) => {
const [fetchingOnline, setFetchingOnline] = useState(false);
const handleRefresh = useCallback(async () => {
setFetchingOnline(true);
await Promise.all([
2023-09-08 00:44:01 +02:00
requestManager.getMangaFetch(mangaId).response.then((res) => {
mutate(`${RequestManager.API_VERSION}manga/${mangaId}`, res, { revalidate: false });
}),
requestManager.getMangaChapters(mangaId, true).response.then((res) =>
mutate(`${RequestManager.API_VERSION}manga/${mangaId}/chapters`, res, {
revalidate: false,
}),
),
]).finally(() => setFetchingOnline(false));
}, [mangaId]);
return [handleRefresh, { loading: fetchingOnline }] as const;
};
2023-05-20 13:23:30 +02:00
export const useDebounce = <Value>(value: Value, delay: number): Value => {
const [debouncedValue, setDebouncedValue] = useState(value);
useEffect(() => {
const handler = setTimeout(() => {
setDebouncedValue(value);
}, delay);
return () => {
clearTimeout(handler);
};
}, [value, delay]);
return debouncedValue;
};