-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathAlertProvider.tsx
More file actions
41 lines (40 loc) · 1.21 KB
/
AlertProvider.tsx
File metadata and controls
41 lines (40 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import React from 'react';
import { useQuery } from 'react-query';
import { Loader } from '@scality/core-ui/dist/components/loader/Loader.component';
import { useAuth } from '../auth/AuthProvider';
import { getAlerts } from './services/alertManager';
import { AlertContext } from './alertContext';
/**
* A wrapper fetching alerts and ensuring their accuracy via a polling refresh strategy.
*
* @param string alert manager url
* @param React.ReactNode children react node
* @returns
*/
export default function AlertProvider({
alertManagerUrl,
children,
}: {
alertManagerUrl: string;
children: React.ReactNode;
}) {
const { getToken } = useAuth();
const query = useQuery(
['activeAlerts'],
async () => getAlerts(alertManagerUrl, await getToken()),
{
refetchInterval: 30000,
// TODO manage this refresh interval globally
// avoid stucking at the hard loading state before alertmanager is ready
initialData: [],
},
);
return (
<AlertContext.Provider value={{ ...query }}>
{query.status === 'loading' && (
<Loader size="massive" centered={true} aria-label="loading" />
)}
{query.status !== 'loading' && children}
</AlertContext.Provider>
);
}