SubMenu.tsx 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { css } from '@emotion/css';
  2. import React, { PureComponent } from 'react';
  3. import { connect, MapStateToProps } from 'react-redux';
  4. import { AnnotationQuery } from '@grafana/data';
  5. import { StoreState } from '../../../../types';
  6. import { getSubMenuVariables, getVariablesState } from '../../../variables/state/selectors';
  7. import { VariableModel } from '../../../variables/types';
  8. import { DashboardModel } from '../../state';
  9. import { DashboardLink } from '../../state/DashboardModel';
  10. import { Annotations } from './Annotations';
  11. import { DashboardLinks } from './DashboardLinks';
  12. import { SubMenuItems } from './SubMenuItems';
  13. interface OwnProps {
  14. dashboard: DashboardModel;
  15. links: DashboardLink[];
  16. annotations: AnnotationQuery[];
  17. }
  18. interface ConnectedProps {
  19. variables: VariableModel[];
  20. }
  21. interface DispatchProps {}
  22. type Props = OwnProps & ConnectedProps & DispatchProps;
  23. class SubMenuUnConnected extends PureComponent<Props> {
  24. onAnnotationStateChanged = (updatedAnnotation: any) => {
  25. // we're mutating dashboard state directly here until annotations are in Redux.
  26. for (let index = 0; index < this.props.dashboard.annotations.list.length; index++) {
  27. const annotation = this.props.dashboard.annotations.list[index];
  28. if (annotation.name === updatedAnnotation.name) {
  29. annotation.enable = !annotation.enable;
  30. break;
  31. }
  32. }
  33. this.props.dashboard.startRefresh();
  34. this.forceUpdate();
  35. };
  36. render() {
  37. const { dashboard, variables, links, annotations } = this.props;
  38. if (!dashboard.isSubMenuVisible()) {
  39. return null;
  40. }
  41. return (
  42. <div className="submenu-controls">
  43. <form aria-label="Template variables" className={styles}>
  44. <SubMenuItems variables={variables} />
  45. </form>
  46. <Annotations
  47. annotations={annotations}
  48. onAnnotationChanged={this.onAnnotationStateChanged}
  49. events={dashboard.events}
  50. />
  51. <div className="gf-form gf-form--grow" />
  52. {dashboard && <DashboardLinks dashboard={dashboard} links={links} />}
  53. <div className="clearfix" />
  54. </div>
  55. );
  56. }
  57. }
  58. const mapStateToProps: MapStateToProps<ConnectedProps, OwnProps, StoreState> = (state, ownProps) => {
  59. const { uid } = ownProps.dashboard;
  60. const templatingState = getVariablesState(uid, state);
  61. return {
  62. variables: getSubMenuVariables(uid, templatingState.variables),
  63. };
  64. };
  65. const styles = css`
  66. display: flex;
  67. flex-wrap: wrap;
  68. display: contents;
  69. `;
  70. export const SubMenu = connect(mapStateToProps)(SubMenuUnConnected);
  71. SubMenu.displayName = 'SubMenu';