/
DashboardList.tsx
147 lines (130 loc) · 3.84 KB
/
DashboardList.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import React, { PureComponent } from 'react';
import { Icon } from '@grafana/ui';
import { DashboardInfo, SMOptions } from 'datasource/types';
import { listAppDashboards, importDashboard, removeDashboard } from 'dashboards/loader';
interface Props {
checkUpdates: boolean;
options: SMOptions;
onChange?: (options: SMOptions) => void;
}
interface State {
dashboards: DashboardInfo[];
}
export class DashboardList extends PureComponent<Props, State> {
state: State = {
dashboards: this.props.options.dashboards,
};
async componentDidMount() {
const { checkUpdates, onChange, options } = this.props;
if (!checkUpdates || !onChange) {
return;
}
const latestDashboards = await listAppDashboards();
// merge dashboards known to grafana with latest in the app.
const dashboards = latestDashboards.map(template => {
const existingDashboard = options.dashboards.find(existing => template.uid === existing.uid);
if (!existingDashboard) {
return template;
}
return {
...existingDashboard,
latestVersion: template.latestVersion,
};
});
this.setState({ dashboards });
}
onImport = (dashboard: DashboardInfo) => async () => {
const { onChange, options } = this.props;
if (!onChange) {
return;
}
const updatedDashboard = await importDashboard(dashboard.json, options);
const dashboards = options.dashboards.map(savedDashboard => {
if (savedDashboard.uid === updatedDashboard.uid) {
return updatedDashboard;
}
return savedDashboard;
});
const updatedOptions = {
...options,
dashboards,
};
this.setState({ dashboards });
return onChange(updatedOptions);
};
onRemove = (dashboard: DashboardInfo) => async () => {
const { onChange } = this.props;
if (!onChange) {
return;
}
let options = { ...this.props.options };
await removeDashboard(dashboard);
let i = options.dashboards.findIndex(item => {
return item.uid === dashboard.uid;
});
if (i >= 0) {
options.dashboards.splice(i, 1);
}
let dashboards = [...this.state.dashboards];
i = dashboards.findIndex(item => {
return item.uid === dashboard.uid;
});
if (i >= 0) {
dashboards[i].version = 0;
}
this.setState({ dashboards });
return onChange(options);
};
withImports(dashboard: DashboardInfo) {
const { checkUpdates } = this.props;
if (!checkUpdates) {
return;
}
function buttonText(dashboard: DashboardInfo) {
return (dashboard.latestVersion || 0) > (dashboard.version || 0) ? 'Update' : 'Re-import';
}
return (
<td style={{ textAlign: 'right' }}>
{dashboard.version === 0 ? (
<button className="btn btn-secondary btn-small" onClick={this.onImport(dashboard)}>
Import
</button>
) : (
<button className="btn btn-secondary btn-small" onClick={this.onImport(dashboard)}>
{buttonText(dashboard)}
</button>
)}
{dashboard.version !== 0 && (
<button className="btn btn-danger btn-small" onClick={this.onRemove(dashboard)}>
<Icon name="trash-alt" />
</button>
)}
</td>
);
}
render() {
const { dashboards } = this.state;
return (
<div>
<h3>Dashboards:</h3>
<table className="filter-table">
<tbody>
{dashboards.map(d => {
return (
<tr key={d.uid}>
<td className="width-1">
<Icon name="apps" />
</td>
<td>
<a href={`d/${d.uid}/`}>{d.title}</a>
</td>
{this.withImports(d)}
</tr>
);
})}
</tbody>
</table>
</div>
);
}
}