GitStart 846da08cbd
refactor: antdv5 notfications (#2161)
Co-authored-by: gitstart <gitstart@users.noreply.github.com>
Co-authored-by: Nitesh Singh <nitesh.singh@gitstart.dev>
Co-authored-by: gitstart-app[bot] <57568882+gitstart-app[bot]@users.noreply.github.com>
Co-authored-by: Rubens Rafael <70234898+RubensRafael@users.noreply.github.com>
Co-authored-by: RubensRafael <rubensrafael2@live.com>
Co-authored-by: niteshsingh1357 <niteshsingh1357@gmail.com>
Co-authored-by: gitstart_bot <gitstart_bot@users.noreply.github.com>
Co-authored-by: Palash Gupta <palashgdev@gmail.com>
2023-02-02 11:38:32 +05:30

107 lines
2.4 KiB
TypeScript

import { notification } from 'antd';
import getTriggeredApi from 'api/alerts/getTriggered';
import Spinner from 'components/Spinner';
import { State } from 'hooks/useFetch';
import React, { useCallback, useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { PayloadProps } from 'types/api/alerts/getTriggered';
import TriggerComponent from './TriggeredAlert';
function TriggeredAlerts(): JSX.Element {
const [groupState, setGroupState] = useState<State<PayloadProps>>({
error: false,
errorMessage: '',
loading: true,
success: false,
payload: [],
});
const { t } = useTranslation(['common']);
const [notifications, NotificationElement] = notification.useNotification();
const fetchData = useCallback(async () => {
try {
setGroupState((state) => ({
...state,
loading: true,
}));
const response = await getTriggeredApi({
active: true,
inhibited: true,
silenced: false,
});
if (response.statusCode === 200) {
setGroupState((state) => ({
...state,
loading: false,
payload: response.payload || [],
}));
} else {
setGroupState((state) => ({
...state,
loading: false,
error: true,
errorMessage: response.error || 'Something went wrong',
}));
}
} catch (error) {
setGroupState((state) => ({
...state,
error: true,
loading: false,
errorMessage: 'Something went wrong',
}));
}
}, []);
useEffect(() => {
fetchData();
}, [fetchData]);
useEffect(() => {
if (groupState.error) {
notifications.error({
message: groupState.errorMessage || t('something_went_wrong'),
});
}
}, [groupState.error, groupState.errorMessage, t, notifications]);
if (groupState.error) {
return (
<>
{NotificationElement}
<TriggerComponent allAlerts={[]} />
</>
);
}
if (groupState.loading || groupState.payload === undefined) {
return (
<>
{NotificationElement}
<Spinner height="75vh" tip="Loading Alerts..." />
</>
);
}
// commented the reduce() call as we no longer use /alerts/groups
// API from alert manager, which returns a group for each receiver
// const initialAlerts: Alerts[] = [];
// const allAlerts: Alerts[] = groupState.payload.reduce((acc, curr) => {
// return [...acc, ...curr.alerts];
// }, initialAlerts);
return (
<>
{NotificationElement}
<TriggerComponent allAlerts={groupState.payload} />
</>
);
}
export default TriggeredAlerts;