-
Notifications
You must be signed in to change notification settings - Fork 168
/
Copy pathwidget-catalog.tsx
89 lines (77 loc) · 2.58 KB
/
widget-catalog.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
import "./widget-catalog.scss";
import * as React from "react";
import * as SDK from "azure-devops-extension-sdk";
import * as Dashboard from "azure-devops-extension-api/Dashboard";
import { BuildResult } from "azure-devops-extension-api/Build";
import { getLastestBuild } from "./utility";
import { css } from "azure-devops-ui/Util";
import { showRootComponent } from "../../Common";
interface ISampleWidgetState {
title: string;
pipelineStatus: string;
blink: boolean;
}
class SampleWidget extends React.Component<{}, ISampleWidgetState> implements Dashboard.IConfigurableWidget {
componentDidMount() {
SDK.init().then(() => {
SDK.register("sample-widget", this);
});
}
render(): JSX.Element {
return (
this.state && (
<div className="content">
<h2 className="title">{this.state.title}</h2>
<div
className={css("status", this.state.blink ? "blink" : undefined)}
>
{this.state.pipelineStatus}
</div>
</div>
)
);
}
async preload(_widgetSettings: Dashboard.WidgetSettings) {
return Dashboard.WidgetStatusHelper.Success();
}
async load(widgetSettings: Dashboard.WidgetSettings): Promise<Dashboard.WidgetStatus> {
try {
await this.setStateFromWidgetSettings(widgetSettings);
return Dashboard.WidgetStatusHelper.Success();
} catch (e) {
return Dashboard.WidgetStatusHelper.Failure((e as any).toString());
}
}
async reload(widgetSettings: Dashboard.WidgetSettings): Promise<Dashboard.WidgetStatus> {
try {
await this.setStateFromWidgetSettings(widgetSettings);
return Dashboard.WidgetStatusHelper.Success();
} catch (e) {
return Dashboard.WidgetStatusHelper.Failure((e as any).toString());
}
}
private async setStateFromWidgetSettings(widgetSettings: Dashboard.WidgetSettings) {
this.setState({
title: widgetSettings.name,
});
try {
const deserialized: ISampleWidgetSettings | null = JSON.parse(widgetSettings.customSettings.data);
if (deserialized) {
const latestResult = (await getLastestBuild(deserialized.pipelineId))?.result;
if (latestResult) {
this.setState({
pipelineStatus:
latestResult == BuildResult.Succeeded ||
latestResult == BuildResult.PartiallySucceeded
? "👍"
: "👎",
blink: deserialized.blink,
});
return;
}
}
} catch { }
this.setState({ pipelineStatus: "🤷♂️", });
}
}
showRootComponent(<SampleWidget />);