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
|
2023-05-18 13:17:41 +02:00
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
2022-11-09 18:09:15 +01:00
|
|
|
|
2023-05-20 13:23:30 +02:00
|
|
|
import { useCallback, useEffect, useState } from 'react';
|
2022-11-09 18:09:15 +01:00
|
|
|
import { mutate } from 'swr';
|
2023-05-18 13:25:19 +02:00
|
|
|
import requestManager from 'lib/RequestManager';
|
2022-11-09 18:09:15 +01:00
|
|
|
|
|
|
|
|
// eslint-disable-next-line import/prefer-default-export
|
|
|
|
|
export const useRefreshManga = (mangaId: string) => {
|
|
|
|
|
const [fetchingOnline, setFetchingOnline] = useState(false);
|
|
|
|
|
|
|
|
|
|
const handleRefresh = useCallback(async () => {
|
|
|
|
|
setFetchingOnline(true);
|
|
|
|
|
await Promise.all([
|
2023-05-18 13:25:19 +02:00
|
|
|
requestManager
|
|
|
|
|
.getClient()
|
|
|
|
|
.get(`/api/v1/manga/${mangaId}/?onlineFetch=true`)
|
|
|
|
|
.then((res) => mutate(`/api/v1/manga/${mangaId}`, res.data, { revalidate: false })),
|
|
|
|
|
requestManager
|
|
|
|
|
.getClient()
|
|
|
|
|
.get(`/api/v1/manga/${mangaId}/chapters?onlineFetch=true`)
|
|
|
|
|
.then((res) =>
|
|
|
|
|
mutate(`/api/v1/manga/${mangaId}/chapters`, res.data, {
|
|
|
|
|
revalidate: false,
|
|
|
|
|
}),
|
|
|
|
|
),
|
2022-11-09 18:09:15 +01:00
|
|
|
]).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;
|
|
|
|
|
};
|