-
-
Notifications
You must be signed in to change notification settings - Fork 656
/
UpdatesPerEnvironmentTypeChart.tsx
106 lines (94 loc) · 3.31 KB
/
UpdatesPerEnvironmentTypeChart.tsx
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
import { useMemo, type VFC } from 'react';
import 'chartjs-adapter-date-fns';
import { useTheme } from '@mui/material';
import type {
InstanceInsightsSchema,
InstanceInsightsSchemaEnvironmentTypeTrendsItem,
} from 'openapi';
import {
LineChart,
NotEnoughData,
} from 'component/insights/components/LineChart/LineChart';
import { usePlaceholderData } from 'component/insights/hooks/usePlaceholderData';
import { UpdatesPerEnvironmentTypeChartTooltip } from './UpdatesPerEnvironmentTypeChartTooltip/UpdatesPerEnvironmentTypeChartTooltip';
interface IUpdatesPerEnvironmnetTypeChart {
environmentTypeTrends: InstanceInsightsSchema['environmentTypeTrends'];
isLoading?: boolean;
}
const groupByDate = (
items: InstanceInsightsSchemaEnvironmentTypeTrendsItem[],
): Record<string, InstanceInsightsSchemaEnvironmentTypeTrendsItem[]> => {
if (!items) {
return {};
}
const grouped = items.reduce(
(acc, item) => {
const key = item.environmentType;
if (!acc[key]) {
acc[key] = [];
}
acc[key].push(item);
return acc;
},
{} as Record<string, InstanceInsightsSchemaEnvironmentTypeTrendsItem[]>,
);
return grouped;
};
const useEnvironmentTypeColor = () => {
const theme = useTheme();
return (environmentType: string) => {
switch (environmentType) {
case 'production':
return theme.palette.charts.series[3];
case 'staging':
return theme.palette.charts.series[1];
case 'development':
return theme.palette.charts.series[0];
case 'test':
return theme.palette.charts.series[2];
default:
return theme.palette.charts.series[4];
}
};
};
export const UpdatesPerEnvironmentTypeChart: VFC<
IUpdatesPerEnvironmnetTypeChart
> = ({ environmentTypeTrends, isLoading }) => {
const theme = useTheme();
const getEnvironmentTypeColor = useEnvironmentTypeColor();
const notEnoughData = environmentTypeTrends?.length < 2;
const placeholderData = usePlaceholderData({ fill: true, type: 'double' });
const data = useMemo(() => {
const grouped = groupByDate(environmentTypeTrends);
const datasets = Object.entries(grouped).map(
([environmentType, trends]) => {
const color = getEnvironmentTypeColor(environmentType);
return {
label: environmentType,
data: trends,
borderColor: color,
backgroundColor: color,
fill: false,
};
},
);
return { datasets };
}, [theme, environmentTypeTrends]);
return (
<LineChart
data={notEnoughData || isLoading ? placeholderData : data}
overrideOptions={
notEnoughData
? {}
: {
parsing: {
yAxisKey: 'totalUpdates',
xAxisKey: 'date',
},
}
}
TooltipComponent={UpdatesPerEnvironmentTypeChartTooltip}
cover={notEnoughData ? <NotEnoughData /> : isLoading}
/>
);
};