2023-03-23 16:42:34 -07:00
|
|
|
import { useState } from 'react';
|
|
|
|
|
2023-04-04 05:11:03 -07:00
|
|
|
function useLoading(initialState: boolean = false) {
|
|
|
|
const [isLoading, setIsLoading] = useState<boolean>(initialState);
|
2023-03-23 16:42:34 -07:00
|
|
|
|
|
|
|
function setPromise<T>(promise: Promise<T>) {
|
|
|
|
setIsLoading(true);
|
|
|
|
|
|
|
|
promise
|
|
|
|
.then(() => setIsLoading(false))
|
|
|
|
.catch(() => setIsLoading(false));
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [isLoading, setPromise] as const;
|
|
|
|
}
|
|
|
|
|
|
|
|
export { useLoading };
|