Skip to content

Commit 05c5807

Browse files
authored
Merge pull request #198 from Minnowo/master
Fix crash from reduce of empty array
2 parents a189202 + 8a41247 commit 05c5807

File tree

3 files changed

+12
-46
lines changed

3 files changed

+12
-46
lines changed

ui/src/dashboard/Entry/DashboardEntry.tsx

+10-44
Original file line numberDiff line numberDiff line change
@@ -67,63 +67,29 @@ const SpecificDashboardEntry: React.FC<{entry: Dashboards_dashboards_items; rang
6767
</Center>
6868
);
6969
}
70+
const firstEntries: Stats_stats_entries[] =
71+
(stats.data && stats.data.stats && stats.data.stats[0] && stats.data.stats[0].entries) || [];
72+
if (firstEntries.length === 0) {
73+
return (
74+
<Center>
75+
<Typography>no data</Typography>
76+
</Center>
77+
);
78+
}
7079

7180
const entries = (stats.data && stats.data.stats) || [];
7281
switch (entry.entryType) {
7382
case EntryType.PieChart:
74-
const data: Stats_stats_entries[] = (stats.data && stats.data.stats && stats.data.stats[0].entries) || [];
75-
if (data.length === 0) {
76-
return (
77-
<Center>
78-
<Typography>no data</Typography>
79-
</Center>
80-
);
81-
}
82-
return <DashboardPieChart entries={data} />;
83+
return <DashboardPieChart entries={firstEntries} />;
8384
case EntryType.BarChart:
84-
if (entries.length === 0) {
85-
return (
86-
<Center>
87-
<Typography>no data</Typography>
88-
</Center>
89-
);
90-
}
9185
return <DashboardBarChart entries={entries} interval={interval} type="normal" total={entry.total} />;
9286
case EntryType.StackedBarChart:
93-
if (entries.length === 0) {
94-
return (
95-
<Center>
96-
<Typography>no data</Typography>
97-
</Center>
98-
);
99-
}
10087
return <DashboardBarChart entries={entries} interval={interval} type="stacked" total={entry.total} />;
10188
case EntryType.LineChart:
102-
if (entries.length === 0) {
103-
return (
104-
<Center>
105-
<Typography>no data</Typography>
106-
</Center>
107-
);
108-
}
10989
return <DashboardLineChart entries={entries} interval={interval} total={entry.total} />;
11090
case EntryType.VerticalTable:
111-
if (entries.length === 0) {
112-
return (
113-
<Center>
114-
<Typography>no data</Typography>
115-
</Center>
116-
);
117-
}
11891
return <DashboardTable mode="vertical" entries={entries} interval={interval} total={entry.total} />;
11992
case EntryType.HorizontalTable:
120-
if (entries.length === 0) {
121-
return (
122-
<Center>
123-
<Typography>no data</Typography>
124-
</Center>
125-
);
126-
}
12793
return <DashboardTable mode="horizontal" entries={entries} interval={interval} total={entry.total} />;
12894
default:
12995
return expectNever(entry.entryType);

ui/src/dashboard/Entry/DashboardPieChart.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ const CustomTooltip = ({active, payload, total}: CustomTooltipProps) => {
4848
<Paper style={{padding: 10}} elevation={4}>
4949
<Typography>
5050
{first.payload.key}:{first.payload.value}: {prettyMs(first.payload.timeSpendInSeconds * 1000)} (
51-
{((first.payload.timeSpendInSeconds / total) * 100).toFixed(2)}%)
51+
{total > 0 ? ((first.payload.timeSpendInSeconds / total) * 100).toFixed(2) : '0.00'}%)
5252
</Typography>
5353
</Paper>
5454
);

ui/src/dashboard/Entry/DashboardTable.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export const DashboardTable: React.FC<DashboardTableProps> = ({entries, interval
3131
}, {}),
3232
};
3333
if (total) {
34-
result.data['Total'] = Object.values(result.data).reduce((acc, value) => acc + value);
34+
result.data['Total'] = Object.values(result.data).reduce((acc, value) => acc + value, 0);
3535
}
3636
return result;
3737
})

0 commit comments

Comments
 (0)