Skip to content

Commit 72cb2ba

Browse files
authored
feat(widget): header widget supports adding description (#276)
Adding support for description. Later can be used to add generic description parameters to every monitoring props (user ask). --- _By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license_
1 parent 48b778d commit 72cb2ba

File tree

7 files changed

+213
-7
lines changed

7 files changed

+213
-7
lines changed

API.md

Lines changed: 37 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/dashboard/widget/HeaderWidget.ts

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,47 @@ export enum HeaderLevel {
99
}
1010

1111
export class HeaderWidget extends TextWidget {
12-
constructor(text: string, level?: HeaderLevel) {
12+
constructor(
13+
text: string,
14+
level?: HeaderLevel,
15+
description?: string,
16+
descriptionHeight?: number
17+
) {
1318
super({
1419
width: FullWidth,
15-
height: 1,
16-
markdown: HeaderWidget.toMarkdown(text, level ?? HeaderLevel.LARGE),
20+
height: HeaderWidget.calculateHeight(description, descriptionHeight),
21+
markdown: HeaderWidget.toMarkdown(
22+
text,
23+
level ?? HeaderLevel.LARGE,
24+
description
25+
),
1726
});
1827
}
1928

20-
private static toMarkdown(text: string, level: HeaderLevel) {
29+
private static calculateHeight(
30+
description?: string,
31+
descriptionHeight?: number
32+
) {
33+
let result = 1;
34+
if (description) {
35+
result += descriptionHeight ?? 1;
36+
}
37+
return result;
38+
}
39+
40+
private static toMarkdown(
41+
text: string,
42+
level: HeaderLevel,
43+
description?: string
44+
) {
45+
const parts = [this.toHeaderMarkdown(text, level)];
46+
if (description) {
47+
parts.push(description);
48+
}
49+
return parts.join("\n\n");
50+
}
51+
52+
private static toHeaderMarkdown(text: string, level: HeaderLevel) {
2153
return "#".repeat(level + 1) + " " + text;
2254
}
2355
}

lib/dashboard/widget/MonitoringHeaderWidget.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,18 @@ export interface MonitoringHeaderWidgetProps {
44
readonly title: string;
55
readonly family?: string;
66
readonly goToLinkUrl?: string;
7+
readonly description?: string;
8+
readonly descriptionHeight?: number;
79
}
810

911
export class MonitoringHeaderWidget extends HeaderWidget {
1012
constructor(props: MonitoringHeaderWidgetProps) {
11-
super(MonitoringHeaderWidget.getText(props), HeaderLevel.SMALL);
13+
super(
14+
MonitoringHeaderWidget.getText(props),
15+
HeaderLevel.SMALL,
16+
props.description,
17+
props.descriptionHeight
18+
);
1219
}
1320

1421
private static getText(props: MonitoringHeaderWidgetProps): string {
@@ -19,7 +26,7 @@ export class MonitoringHeaderWidget extends HeaderWidget {
1926
}
2027

2128
if (props.family) {
22-
return `${props.family} **${title}**`;
29+
title = `${props.family} **${title}**`;
2330
}
2431

2532
return title;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { HeaderLevel, HeaderWidget } from "../../../lib/dashboard/widget";
2+
3+
test("snapshot test - title only", () => {
4+
const widget = new HeaderWidget("text");
5+
6+
expect(widget.toJson()).toMatchSnapshot();
7+
});
8+
9+
test("snapshot test - title and description, no custom height", () => {
10+
const widget = new HeaderWidget("text", HeaderLevel.LARGE, "description");
11+
12+
expect(widget.toJson()).toMatchSnapshot();
13+
});
14+
15+
test("snapshot test - title and description, with custom height", () => {
16+
const widget = new HeaderWidget(
17+
"text",
18+
HeaderLevel.LARGE,
19+
"very long description",
20+
5
21+
);
22+
23+
expect(widget.toJson()).toMatchSnapshot();
24+
});

test/dashboard/widget/MonitoringHeaderWidget.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,26 @@ test("snapshot test: with link and custom text", () => {
3636

3737
expect(widget.toJson()).toMatchSnapshot();
3838
});
39+
40+
test("snapshot test: with link, custom text and description", () => {
41+
const widget = new MonitoringHeaderWidget({
42+
title: "DummyTitle",
43+
family: "DummyFamily",
44+
goToLinkUrl: "http://amazon.com",
45+
description: "CustomDescription",
46+
});
47+
48+
expect(widget.toJson()).toMatchSnapshot();
49+
});
50+
51+
test("snapshot test: with link, custom text and description with custom height", () => {
52+
const widget = new MonitoringHeaderWidget({
53+
title: "DummyTitle",
54+
family: "DummyFamily",
55+
goToLinkUrl: "http://amazon.com",
56+
description: "CustomDescription",
57+
descriptionHeight: 10,
58+
});
59+
60+
expect(widget.toJson()).toMatchSnapshot();
61+
});

test/dashboard/widget/__snapshots__/HeaderWidget.test.ts.snap

Lines changed: 50 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/dashboard/widget/__snapshots__/MonitoringHeaderWidget.test.ts.snap

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)