Skip to content

Commit 54d13c8

Browse files
committed
Handle empty search
1 parent 0868185 commit 54d13c8

File tree

2 files changed

+43
-15
lines changed

2 files changed

+43
-15
lines changed

site/src/modules/workspaces/WorkspaceTiming/Chart/Chart.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export type ChartProps = {
6464
};
6565

6666
export const Chart: FC<ChartProps> = ({ data, onBarClick }) => {
67-
const totalDuration = duration(data.flatMap((d) => d.timings));
67+
const totalDuration = combineDurations(data.flatMap((d) => d.timings));
6868
const totalTime = durationTime(totalDuration);
6969

7070
// XAxis ticks
@@ -205,7 +205,14 @@ const durationTime = (duration: Duration): number => {
205205

206206
// Combine multiple durations into a single duration by using the initial start
207207
// time and the final end time.
208-
export const duration = (durations: readonly Duration[]): Duration => {
208+
export const combineDurations = (durations: readonly Duration[]): Duration => {
209+
// If there are no durations, return a duration with the same start and end
210+
// times. This prevents the chart from breaking when calculating the start and
211+
// end times from an empty array.
212+
if (durations.length === 0) {
213+
return { startedAt: new Date(), endedAt: new Date() };
214+
}
215+
209216
const sortedDurations = durations
210217
.slice()
211218
.sort((a, b) => a.startedAt.getTime() - b.startedAt.getTime());

site/src/modules/workspaces/WorkspaceTiming/WorkspaceTimings.tsx

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import type { ProvisionerTiming } from "api/typesGenerated";
2-
import { Chart, type Duration, type Timing, duration } from "./Chart/Chart";
2+
import {
3+
Chart,
4+
type Duration,
5+
type Timing,
6+
combineDurations,
7+
} from "./Chart/Chart";
38
import { useState, type FC } from "react";
49
import type { Interpolation, Theme } from "@emotion/react";
510
import ChevronRight from "@mui/icons-material/ChevronRight";
@@ -101,17 +106,33 @@ export const WorkspaceTimings: FC<WorkspaceTimingsProps> = ({
101106
</div>
102107
)}
103108

104-
<Chart
105-
data={data}
106-
onBarClick={(stage, section) => {
107-
setView({
108-
name: "advanced",
109-
selectedStage: stage,
110-
parentSection: section,
111-
filter: "",
112-
});
113-
}}
114-
/>
109+
{data.flatMap((section) => section.timings).length > 0 ? (
110+
<Chart
111+
data={data}
112+
onBarClick={(stage, section) => {
113+
setView({
114+
name: "advanced",
115+
selectedStage: stage,
116+
parentSection: section,
117+
filter: "",
118+
});
119+
}}
120+
/>
121+
) : (
122+
<div
123+
css={{
124+
width: "100%",
125+
height: "100%",
126+
display: "flex",
127+
justifyContent: "center",
128+
alignItems: "center",
129+
}}
130+
>
131+
{view.name === "basic"
132+
? "No data found"
133+
: `No resource found for "${view.filter}"`}
134+
</div>
135+
)}
115136
</div>
116137
);
117138
};
@@ -137,7 +158,7 @@ export const selectChartData = (
137158
const durations = timings
138159
.filter((t) => t.stage === stage)
139160
.map(extractDuration);
140-
const stageDuration = duration(durations);
161+
const stageDuration = combineDurations(durations);
141162
const stageTiming: Timing = {
142163
label: stage,
143164
childrenCount: durations.length,

0 commit comments

Comments
 (0)