-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathDashboardAlerts.tsx
More file actions
139 lines (136 loc) · 4.17 KB
/
DashboardAlerts.tsx
File metadata and controls
139 lines (136 loc) · 4.17 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { Banner, Icon, spacing, Text, TextBadge } from '@scality/core-ui';
import { Box } from '@scality/core-ui/dist/next';
import { useMemo } from 'react';
import { useIntl } from 'react-intl';
import styled from 'styled-components';
import { useAlertLibrary, useAlerts } from '../containers/AlertProvider';
import {
useDiscoveredViews,
useLinkOpener,
} from '../containers/ConfigProvider';
import { getChildrenAlerts } from '../services/alertUtils';
import { useBasenameRelativeNavigate } from '@scality/module-federation';
const AlertsContainer = styled.div`
display: flex;
flex-direction: column;
`;
const BadgesContainer = styled.div`
display: flex;
& > div {
margin-right: ${spacing.r16};
}
`;
const Link = styled.div`
color: ${(props) => props.theme.textLink};
cursor: pointer;
margin-left: auto;
text-decoration: none;
text-align: right;
&:hover {
text-decoration: underline;
}
`;
const DashboardAlerts = () => {
const { openLink } = useLinkOpener();
const navigate = useBasenameRelativeNavigate();
const discoveredViews = useDiscoveredViews();
const alertView = discoveredViews.find((view) => {
return view.isFederated && view.view.path === '/alerts';
});
const intl = useIntl();
const alertsLibrary = useAlertLibrary();
const topLevelAlerts = useAlerts(alertsLibrary.getPlatformAlertSelectors());
const alerts = useAlerts({});
// in MetalK8s dashboard, we want to display the number of the alerts only for metalk8s namespace
const metalk8sAtomicAlerts = useMemo(() => {
if (topLevelAlerts?.alerts?.length && alerts?.alerts?.length)
return getChildrenAlerts(
topLevelAlerts.alerts.map((alert) => alert.childrenJsonPath) || [],
alerts.alerts,
);
else return [];
}, [JSON.stringify(alerts.alerts), JSON.stringify(topLevelAlerts.alerts)]);
const criticalAlerts = metalk8sAtomicAlerts.filter(
(alert) => alert.severity === 'critical',
);
const warningAlerts = metalk8sAtomicAlerts.filter(
(alert) => alert.severity === 'warning',
);
const totalAlerts = criticalAlerts.length + warningAlerts.length;
return (
<AlertsContainer>
<Box display="flex" alignItems="center" gap={spacing.r8}>
<div>
<Text isEmphazed>
{intl.formatMessage({
id: 'platform_active_alerts',
})}
</Text>
<TextBadge
variant="infoPrimary"
data-testid="all-alert-badge"
text={totalAlerts}
/>
{totalAlerts === 0 && (
<div>
<Text variant="Smaller" color="textSecondary">
{intl.formatMessage({
id: 'no_active_alerts',
})}
</Text>
</div>
)}
</div>
{alerts.error && (
<div style={{ flex: 1 }}>
<Banner
variant="warning"
icon={<Icon name="Exclamation-circle" />}
title={intl.formatMessage({
id: 'monitoring_information_unavailable',
})}
>
{intl.formatMessage({
id: 'some_data_not_retrieved',
})}
</Banner>
</div>
)}
</Box>
{totalAlerts === 0 ? null : (
<Box pr={24}>
<BadgesContainer>
<div>
<Text>Critical</Text>
<TextBadge
variant="statusCritical"
data-testid="critical-alert-badge"
text={criticalAlerts.length}
/>
</div>
<div>
<Text>Warning</Text>
<TextBadge
variant="statusWarning"
data-testid="warning-alert-badge"
text={warningAlerts.length}
/>
</div>
</BadgesContainer>
<Link
onClick={() => {
openLink(alertView);
navigate('/alerts', { replace: true });
}}
data-testid="view-all-link"
>
{intl.formatMessage({
id: 'view_all',
})}
</Link>
</Box>
)}
</AlertsContainer>
);
};
export default DashboardAlerts;