useFolder.ts 877 B

1234567891011121314151617181920212223242526272829303132333435
  1. import { useEffect } from 'react';
  2. import { useDispatch } from 'react-redux';
  3. import { FolderDTO } from 'app/types';
  4. import { fetchFolderIfNotFetchedAction } from '../state/actions';
  5. import { initialAsyncRequestState } from '../utils/redux';
  6. import { useUnifiedAlertingSelector } from './useUnifiedAlertingSelector';
  7. interface ReturnBag {
  8. folder?: FolderDTO;
  9. loading: boolean;
  10. }
  11. export function useFolder(uid?: string): ReturnBag {
  12. const dispatch = useDispatch();
  13. const folderRequests = useUnifiedAlertingSelector((state) => state.folders);
  14. useEffect(() => {
  15. if (uid) {
  16. dispatch(fetchFolderIfNotFetchedAction(uid));
  17. }
  18. }, [dispatch, uid]);
  19. if (uid) {
  20. const request = folderRequests[uid] || initialAsyncRequestState;
  21. return {
  22. folder: request.result,
  23. loading: request.loading,
  24. };
  25. }
  26. return {
  27. loading: false,
  28. };
  29. }