grafana/public/app/features/alerting/unified/hooks/useFolder.ts
kay delaney 64bbb7a7ce
Chore: Update and enforce usage of typed react-redux hooks (#55349)
* Chore: Update and enforce usage of typed react-redux hooks
2022-09-19 10:49:35 +01:00

35 lines
847 B
TypeScript

import { useEffect } from 'react';
import { FolderDTO, useDispatch } from 'app/types';
import { fetchFolderIfNotFetchedAction } from '../state/actions';
import { initialAsyncRequestState } from '../utils/redux';
import { useUnifiedAlertingSelector } from './useUnifiedAlertingSelector';
interface ReturnBag {
folder?: FolderDTO;
loading: boolean;
}
export function useFolder(uid?: string): ReturnBag {
const dispatch = useDispatch();
const folderRequests = useUnifiedAlertingSelector((state) => state.folders);
useEffect(() => {
if (uid) {
dispatch(fetchFolderIfNotFetchedAction(uid));
}
}, [dispatch, uid]);
if (uid) {
const request = folderRequests[uid] || initialAsyncRequestState;
return {
folder: request.result,
loading: request.loading,
};
}
return {
loading: false,
};
}