-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathAddToDashboardButton.tsx
More file actions
56 lines (49 loc) · 1.38 KB
/
AddToDashboardButton.tsx
File metadata and controls
56 lines (49 loc) · 1.38 KB
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
import React from 'react';
import { useDispatch, useSelector } from 'react-redux';
import type { PanelDefinition } from '@perses-dev/core';
import { Button } from '@patternfly/react-core';
import { dashboardsAddPersesPanelExternally } from '../../store/actions';
function createPanelDefinition(query: string): PanelDefinition {
return {
kind: 'Panel',
spec: {
display: {
name: query,
},
plugin: {
kind: 'TimeSeriesChart',
spec: {},
},
queries: [
{
kind: 'TimeSeriesQuery',
spec: {
plugin: {
kind: 'PrometheusTimeSeriesQuery',
spec: {
query: query,
},
},
},
},
],
},
};
}
type AddToDashboardButtonProps = {
query: string;
};
export const AddToDashboardButton: React.FC<AddToDashboardButtonProps> = ({ query }) => {
const dispatch = useDispatch();
const isCustomDashboardOpen: boolean = useSelector(
(s: any) => s.plugins?.mp?.dashboards?.isOpened,
);
const addToPersesDashboard = React.useCallback(() => {
const panelDefinition = createPanelDefinition(query);
dispatch(dashboardsAddPersesPanelExternally(panelDefinition));
}, [query, dispatch]);
if (!isCustomDashboardOpen) {
return null;
}
return <Button onClick={addToPersesDashboard}>Add to dashboard</Button>;
};