import {useCallback, useState} from 'react' export function useLoadingState(){ const [loadingState, setLoadingState] = useState>({}) const updateLoading = useCallback(async (key: string, promise?: Promise|void) => { if(!promise || promise.then === undefined) return setLoadingState({...loadingState, [key]: false}) setLoadingState({...loadingState, [key]: true}) await promise.then(() => setLoadingState({...loadingState, [key]: false})) }, [loadingState]) return {loadingState, updateLoading} } export function useLoadingStateKey(){ const [loadingState, setLoadingState] = useState(false) const updateLoading = useCallback(async (promise?: Promise|void) => { if(!promise || promise.then === undefined) return setLoadingState(false) setLoadingState(true) await promise.then(() => setLoadingState(false)) }, [loadingState]) return {loadingState, updateLoading} }