Skip to content

Commit c1a4b3b

Browse files
Merge pull request #549 from Fewwy/fix-alerts-chart-empty-state
OBSINTA-789: Fix alerts chart empty state
2 parents f74ea2c + 6102a5f commit c1a4b3b

File tree

4 files changed

+43
-39
lines changed

4 files changed

+43
-39
lines changed

web/src/components/Incidents/AlertsChart/AlertsChart.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { useTranslation } from 'react-i18next';
3030
import { AlertsChartBar } from '../model';
3131
import { setAlertsAreLoading } from '../../../store/actions';
3232
import { MonitoringState } from '../../../store/store';
33+
import { isEmpty } from 'lodash-es';
3334

3435
const AlertsChart = ({ theme }: { theme: 'light' | 'dark' }) => {
3536
const dispatch = useDispatch();
@@ -99,7 +100,7 @@ const AlertsChart = ({ theme }: { theme: 'light' | 'dark' }) => {
99100
<Card className="alerts-chart-card" style={{ overflow: 'visible' }}>
100101
<div ref={containerRef}>
101102
<CardTitle>Alerts Timeline</CardTitle>
102-
{alertsAreLoading ? (
103+
{alertsAreLoading || isEmpty(incidentsActiveFilters.groupId) ? (
103104
<EmptyState
104105
variant="lg"
105106
style={{

web/src/components/Incidents/IncidentsChart/IncidentsChart.tsx

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useMemo, useRef, useState } from 'react';
1+
import React, { useEffect, useMemo, useRef, useState } from 'react';
22

33
import {
44
Chart,
@@ -23,35 +23,31 @@ import {
2323
t_global_color_status_info_default,
2424
t_global_color_status_warning_default,
2525
} from '@patternfly/react-tokens';
26-
import { useDispatch, useSelector } from 'react-redux';
2726
import '../incidents-styles.css';
2827
import { IncidentsTooltip } from '../IncidentsTooltip';
2928
import { Incident } from '../model';
30-
import { createIncidentsChartBars, generateDateArray, updateBrowserUrl } from '../utils';
29+
import { createIncidentsChartBars, generateDateArray } from '../utils';
3130
import { dateTimeFormatter } from '../../console/utils/datetime';
3231
import { useTranslation } from 'react-i18next';
33-
import { MonitoringState } from '../../../store/store';
34-
import { setAlertsAreLoading, setIncidentsActiveFilters } from '../../../store/actions';
3532

3633
const IncidentsChart = ({
3734
incidentsData,
3835
chartDays,
3936
theme,
37+
selectedGroupId,
38+
onIncidentClick,
4039
}: {
4140
incidentsData: Array<Incident>;
4241
chartDays: number;
4342
theme: 'light' | 'dark';
43+
selectedGroupId: string;
44+
onIncidentClick: (groupId: string) => void;
4445
}) => {
45-
const dispatch = useDispatch();
4646
const [isLoading, setIsLoading] = useState(true);
4747
const [chartContainerHeight, setChartContainerHeight] = useState<number>();
4848
const [chartHeight, setChartHeight] = useState<number>();
4949
const dateValues = useMemo(() => generateDateArray(chartDays), [chartDays]);
5050

51-
const incidentsActiveFilters = useSelector(
52-
(state: MonitoringState) => state.plugins.mcp.incidentsData.incidentsActiveFilters,
53-
);
54-
const selectedGroupId = incidentsActiveFilters.groupId?.[0] ?? null;
5551
const { i18n } = useTranslation();
5652

5753
const chartData = useMemo(() => {
@@ -81,36 +77,13 @@ const IncidentsChart = ({
8177
}
8278
};
8379
useEffect(() => {
84-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
85-
// @ts-ignore
8680
const observer = getResizeObserver(containerRef.current, handleResize);
8781
handleResize();
8882
return () => observer();
8983
}, []);
9084

9185
const clickHandler = (data, datum) => {
92-
if (datum.datum.group_id === selectedGroupId) {
93-
dispatch(
94-
setIncidentsActiveFilters({
95-
incidentsActiveFilters: {
96-
...incidentsActiveFilters,
97-
groupId: [],
98-
},
99-
}),
100-
);
101-
updateBrowserUrl(incidentsActiveFilters, '');
102-
dispatch(setAlertsAreLoading({ alertsAreLoading: true }));
103-
} else {
104-
dispatch(
105-
setIncidentsActiveFilters({
106-
incidentsActiveFilters: {
107-
...incidentsActiveFilters,
108-
groupId: [datum.datum.group_id],
109-
},
110-
}),
111-
);
112-
updateBrowserUrl(incidentsActiveFilters, datum.datum.group_id);
113-
}
86+
onIncidentClick(datum.datum.group_id);
11487
};
11588

11689
return (
@@ -235,4 +208,4 @@ const IncidentsChart = ({
235208
);
236209
};
237210

238-
export default IncidentsChart;
211+
export default React.memo(IncidentsChart);

web/src/components/Incidents/IncidentsPage.tsx

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable react-hooks/exhaustive-deps */
2-
import { useMemo, useState, useEffect } from 'react';
2+
import { useMemo, useState, useEffect, useCallback } from 'react';
33
import { useSafeFetch } from '../console/utils/safe-fetch-hook';
44
import { createAlertsQuery, fetchDataForIncidentsAndAlerts } from './api';
55
import { useTranslation } from 'react-i18next';
@@ -322,6 +322,31 @@ const IncidentsPage = () => {
322322
}
323323
}, [incidentsActiveFilters, filteredData, dispatch]);
324324

325+
const handleIncidentChartClick = useCallback(
326+
(groupId) => {
327+
if (groupId === selectedGroupId) {
328+
dispatch(
329+
setIncidentsActiveFilters({
330+
incidentsActiveFilters: {
331+
...incidentsActiveFilters,
332+
groupId: [],
333+
},
334+
}),
335+
);
336+
} else {
337+
dispatch(
338+
setIncidentsActiveFilters({
339+
incidentsActiveFilters: {
340+
...incidentsActiveFilters,
341+
groupId: [groupId],
342+
},
343+
}),
344+
);
345+
}
346+
},
347+
[dispatch, incidentsActiveFilters, selectedGroupId],
348+
);
349+
325350
return (
326351
<>
327352
<Helmet>
@@ -512,6 +537,8 @@ const IncidentsPage = () => {
512537
incidentsData={filteredData}
513538
chartDays={timeRanges.length}
514539
theme={theme}
540+
selectedGroupId={selectedGroupId}
541+
onIncidentClick={handleIncidentChartClick}
515542
/>
516543
</StackItem>
517544
<StackItem>

web/src/components/Incidents/IncidentsTable.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
} from '@patternfly/react-core';
1010
import { SearchIcon, AngleDownIcon, AngleRightIcon } from '@patternfly/react-icons';
1111
import { ExpandableRowContent, Table, Tbody, Td, Th, Thead, Tr } from '@patternfly/react-table';
12-
import * as _ from 'lodash-es';
12+
import { isEmpty } from 'lodash-es';
1313
import { useState, useEffect } from 'react';
1414
import { useSelector } from 'react-redux';
1515
import { MonitoringState } from '../../store/store';
@@ -33,6 +33,9 @@ export const IncidentsTable = () => {
3333
const alertsAreLoading = useSelector(
3434
(state: MonitoringState) => state.plugins.mcp.incidentsData.alertsAreLoading,
3535
);
36+
const incidentsActiveFilters = useSelector(
37+
(state: MonitoringState) => state.plugins.mcp.incidentsData.incidentsActiveFilters,
38+
);
3639

3740
const [expandedAlerts, setExpandedAlerts] = useState<Array<string>>([]);
3841
const setAlertExpanded = (alert: GroupedAlert, isExpanding = true) =>
@@ -92,7 +95,7 @@ export const IncidentsTable = () => {
9295
return Math.min(...alert.alertsExpandedRowData.map((alertData) => alertData.alertsStartFiring));
9396
};
9497

95-
if (_.isEmpty(alertsTableData) || alertsAreLoading) {
98+
if (isEmpty(alertsTableData) || alertsAreLoading || isEmpty(incidentsActiveFilters.groupId)) {
9699
return (
97100
<Card>
98101
<CardBody>

0 commit comments

Comments
 (0)