Amba-Agent-App/lib/hooks/useGlobalLoading.ts
2026-01-16 00:22:35 +03:00

31 lines
925 B
TypeScript

import { useCallback } from 'react';
import { useUiStore } from '../stores/uiStore';
export const useGlobalLoading = () => {
const showLoader = useUiStore((state) => state.showLoader);
const hideLoader = useUiStore((state) => state.hideLoader);
const setLoading = useUiStore((state) => state.setLoading);
const isGlobalLoading = useUiStore((state) => state.isLoading);
const withLoading = useCallback(<T,>(operation: () => Promise<T> | T): Promise<T> => {
showLoader();
try {
const result = operation();
return Promise.resolve(result).finally(() => {
hideLoader();
});
} catch (error) {
hideLoader();
return Promise.reject(error);
}
}, [showLoader, hideLoader]);
return {
showLoader,
hideLoader,
setLoading,
withLoading,
isGlobalLoading
};
};