utils.tsx 661 B

123456789101112131415161718192021222324252627282930
  1. import {useCallback, useState} from 'react';
  2. import useTimeout from 'sentry/utils/useTimeout';
  3. const HOVERCARD_CONTENT_DELAY = 400;
  4. export function useDelayedLoadingState() {
  5. const [shouldShowLoadingState, setShouldShowLoadingState] = useState(false);
  6. const onTimeout = useCallback(() => {
  7. setShouldShowLoadingState(true);
  8. }, []);
  9. const {start, end, cancel} = useTimeout({
  10. timeMs: HOVERCARD_CONTENT_DELAY,
  11. onTimeout,
  12. });
  13. const reset = useCallback(() => {
  14. setShouldShowLoadingState(false);
  15. cancel();
  16. }, [cancel]);
  17. return {
  18. shouldShowLoadingState,
  19. onRequestBegin: start,
  20. onRequestEnd: end,
  21. reset,
  22. };
  23. }