forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSubMenu.tsx
131 lines (110 loc) · 3.79 KB
/
SubMenu.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
import { css } from '@emotion/css';
import FilterListIcon from '@mui/icons-material/FilterList';
import { Box, styled } from '@mui/material';
import React, { FC, PureComponent } from 'react';
import { connect, MapStateToProps } from 'react-redux';
import { AnnotationQuery, DataQuery, GrafanaTheme2 } from '@grafana/data';
import { stylesFactory, Themeable2, withTheme2 } from '@grafana/ui';
import { StoreState } from '../../../../types';
import { getSubMenuVariables, getVariablesState } from '../../../variables/state/selectors';
import { VariableModel } from '../../../variables/types';
import { DashboardModel } from '../../state';
import { DashboardLink } from '../../state/DashboardModel';
import { Annotations } from './Annotations';
import { DashboardLinks } from './DashboardLinks';
import { SubMenuItems } from './SubMenuItems';
interface OwnProps extends Themeable2 {
dashboard: DashboardModel;
links: DashboardLink[];
annotations: AnnotationQuery[];
}
interface ConnectedProps {
variables: VariableModel[];
}
interface DispatchProps {}
type Props = OwnProps & ConnectedProps & DispatchProps;
class SubMenuUnConnected extends PureComponent<Props> {
onAnnotationStateChanged = (updatedAnnotation: AnnotationQuery<DataQuery>) => {
// we're mutating dashboard state directly here until annotations are in Redux.
for (let index = 0; index < this.props.dashboard.annotations.list.length; index++) {
const annotation = this.props.dashboard.annotations.list[index];
if (annotation.name === updatedAnnotation.name) {
annotation.enable = !annotation.enable;
break;
}
}
this.props.dashboard.startRefresh();
this.forceUpdate();
};
disableSubmitOnEnter = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
};
render() {
const { dashboard, variables, links, annotations, theme } = this.props;
const styles = getStyles(theme);
if (!dashboard.isSubMenuVisible()) {
return null;
}
const readOnlyVariables = dashboard.meta.isSnapshot ?? false;
return (
<div className={styles.submenu}>
<form aria-label="Template variables" className={styles.formStyles} onSubmit={this.disableSubmitOnEnter}>
<FilterWithIcon />
<SubMenuItems variables={variables} readOnly={readOnlyVariables} />
</form>
<Annotations
annotations={annotations}
onAnnotationChanged={this.onAnnotationStateChanged}
events={dashboard.events}
/>
<div className={styles.spacer} />
{dashboard && <DashboardLinks dashboard={dashboard} links={links} />}
</div>
);
}
}
const mapStateToProps: MapStateToProps<ConnectedProps, OwnProps, StoreState> = (state, ownProps) => {
const { uid } = ownProps.dashboard;
const templatingState = getVariablesState(uid, state);
return {
variables: getSubMenuVariables(uid, templatingState.variables),
};
};
const getStyles = stylesFactory((theme: GrafanaTheme2) => {
return {
formStyles: css`
display: flex;
flex-wrap: wrap;
display: contents;
`,
submenu: css`
display: flex;
flex-direction: row;
flex-wrap: wrap;
align-content: flex-start;
align-items: flex-start;
gap: ${theme.spacing(1)} ${theme.spacing(2)};
padding: 0 0 ${theme.spacing(1)} 0;
`,
spacer: css({
flexGrow: 1,
}),
};
});
export const SubMenu = withTheme2(connect(mapStateToProps)(SubMenuUnConnected));
SubMenu.displayName = 'SubMenu';
const FilterWithIcon: FC = () => (
<FilterWithIconStyled>
<FilterListIcon sx={{ color: '#3A785E' }} />
FILTERS
</FilterWithIconStyled>
);
const FilterWithIconStyled = styled(Box)({
display: 'flex',
gap: 1,
alignItems: 'center',
color: '#3A785E',
fontWeight: 600,
lineHeight: '160%',
fontSize: 12,
});