mirror of
https://github.com/grafana/grafana.git
synced 2025-02-13 00:55:47 -06:00
33 lines
874 B
TypeScript
33 lines
874 B
TypeScript
import { FolderDTO } from 'app/types';
|
|
import { useDispatch } from 'react-redux';
|
|
import { useUnifiedAlertingSelector } from './useUnifiedAlertingSelector';
|
|
import { useEffect } from 'react';
|
|
import { fetchFolderIfNotFetchedAction } from '../state/actions';
|
|
import { initialAsyncRequestState } from '../utils/redux';
|
|
|
|
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,
|
|
};
|
|
}
|