forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPanelEditorTabs.tsx
138 lines (121 loc) · 4.46 KB
/
PanelEditorTabs.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
import { css } from '@emotion/css';
import { memo, useCallback, useEffect } from 'react';
import { Subscription } from 'rxjs';
import { GrafanaTheme2 } from '@grafana/data';
import { config, reportInteraction } from '@grafana/runtime';
import { Tab, TabContent, TabsBar, toIconName, useForceUpdate, useStyles2 } from '@grafana/ui';
import { PanelAlertTab } from 'app/features/alerting/unified/PanelAlertTab';
import { PanelAlertTabContent } from 'app/features/alerting/unified/PanelAlertTabContent';
import { PanelQueriesChangedEvent, PanelTransformationsChangedEvent } from 'app/types/events';
import { DashboardModel, PanelModel } from '../../state';
import { TransformationsEditor } from '../TransformationsEditor/TransformationsEditor';
import { PanelEditorQueries } from './PanelEditorQueries';
import { PanelEditorTab, PanelEditorTabId } from './types';
interface PanelEditorTabsProps {
panel: PanelModel;
dashboard: DashboardModel;
tabs: PanelEditorTab[];
onChangeTab: (tab: PanelEditorTab) => void;
isMfeEditPanel?: boolean;
}
export const PanelEditorTabs = memo(({ panel, dashboard, tabs, onChangeTab, isMfeEditPanel }: PanelEditorTabsProps) => {
const forceUpdate = useForceUpdate();
const styles = useStyles2(getStyles);
const instrumentedOnChangeTab = useCallback(
(tab: PanelEditorTab) => {
let eventName = 'panel_editor_tabs_changed';
if (config.featureToggles.transformationsRedesign) {
eventName = 'transformations_redesign_' + eventName;
}
if (!tab.active) {
reportInteraction(eventName, { tab_id: tab.id });
}
onChangeTab(tab);
},
[onChangeTab]
);
useEffect(() => {
const eventSubs = new Subscription();
eventSubs.add(panel.events.subscribe(PanelQueriesChangedEvent, forceUpdate));
eventSubs.add(panel.events.subscribe(PanelTransformationsChangedEvent, forceUpdate));
return () => eventSubs.unsubscribe();
}, [panel, dashboard, forceUpdate]);
const activeTab = !isMfeEditPanel? tabs.find((item) => item.active)!: tabs.find((item) => item.id === PanelEditorTabId.Query)!;
if (tabs.length === 0) {
return null;
}
const alertingEnabled = config.unifiedAlertingEnabled;
return (
<div className={styles.wrapper}>
<TabsBar className={styles.tabBar} hideBorder>
{!isMfeEditPanel ? tabs.map((tab) => {
if (tab.id === PanelEditorTabId.Alert && alertingEnabled) {
return (
<PanelAlertTab
key={tab.id}
label={tab.text}
active={tab.active}
onChangeTab={() => onChangeTab(tab)}
icon={toIconName(tab.icon)}
panel={panel}
dashboard={dashboard}
/>
);
}
return (
<Tab
key={tab.id}
label={tab.text}
active={tab.active}
onChangeTab={() => instrumentedOnChangeTab(tab)}
icon={toIconName(tab.icon)}
counter={getCounter(panel, tab)}
/>
);
}): null}
</TabsBar>
<TabContent className={styles.tabContent}>
{activeTab.id === PanelEditorTabId.Query && <PanelEditorQueries panel={panel} queries={panel.targets} />}
{activeTab.id === PanelEditorTabId.Alert && <PanelAlertTabContent panel={panel} dashboard={dashboard} />}
{activeTab.id === PanelEditorTabId.Transform && <TransformationsEditor panel={panel} />}
</TabContent>
</div>
);
});
PanelEditorTabs.displayName = 'PanelEditorTabs';
function getCounter(panel: PanelModel, tab: PanelEditorTab) {
switch (tab.id) {
case PanelEditorTabId.Query:
return panel.targets.length;
case PanelEditorTabId.Alert:
return panel.alert ? 1 : 0;
case PanelEditorTabId.Transform:
const transformations = panel.getTransformations() ?? [];
return transformations.length;
}
return null;
}
const getStyles = (theme: GrafanaTheme2) => {
return {
wrapper: css({
display: 'flex',
flexDirection: 'column',
height: '100%',
}),
tabBar: css({
paddingLeft: theme.spacing(2),
}),
tabContent: css({
padding: 0,
display: 'flex',
flexDirection: 'column',
flex: 1,
minHeight: 0,
background: theme.colors.background.primary,
border: `1px solid ${theme.components.panel.borderColor}`,
borderLeft: 'none',
borderBottom: 'none',
borderTopRightRadius: theme.shape.borderRadius(1.5),
}),
};
};