VisualizationButton.tsx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { css } from '@emotion/css';
  2. import React, { FC } from 'react';
  3. import { useDispatch, useSelector } from 'react-redux';
  4. import { GrafanaTheme } from '@grafana/data';
  5. import { selectors } from '@grafana/e2e-selectors';
  6. import { ToolbarButton, ButtonGroup, useStyles } from '@grafana/ui';
  7. import { StoreState } from 'app/types';
  8. import { PanelModel } from '../../state';
  9. import { getPanelPluginWithFallback } from '../../state/selectors';
  10. import { setPanelEditorUIState, toggleVizPicker } from './state/reducers';
  11. type Props = {
  12. panel: PanelModel;
  13. };
  14. export const VisualizationButton: FC<Props> = ({ panel }) => {
  15. const styles = useStyles(getStyles);
  16. const dispatch = useDispatch();
  17. const plugin = useSelector(getPanelPluginWithFallback(panel.type));
  18. const isPanelOptionsVisible = useSelector((state: StoreState) => state.panelEditor.ui.isPanelOptionsVisible);
  19. const isVizPickerOpen = useSelector((state: StoreState) => state.panelEditor.isVizPickerOpen);
  20. const onToggleOpen = () => {
  21. dispatch(toggleVizPicker(!isVizPickerOpen));
  22. };
  23. const onToggleOptionsPane = () => {
  24. dispatch(setPanelEditorUIState({ isPanelOptionsVisible: !isPanelOptionsVisible }));
  25. };
  26. if (!plugin) {
  27. return null;
  28. }
  29. return (
  30. <div className={styles.wrapper}>
  31. <ButtonGroup>
  32. <ToolbarButton
  33. className={styles.vizButton}
  34. tooltip="Click to change visualization"
  35. imgSrc={plugin.meta.info.logos.small}
  36. isOpen={isVizPickerOpen}
  37. onClick={onToggleOpen}
  38. aria-label={selectors.components.PanelEditor.toggleVizPicker}
  39. fullWidth
  40. >
  41. {plugin.meta.name}
  42. </ToolbarButton>
  43. <ToolbarButton
  44. tooltip={isPanelOptionsVisible ? 'Close options pane' : 'Show options pane'}
  45. icon={isPanelOptionsVisible ? 'angle-right' : 'angle-left'}
  46. onClick={onToggleOptionsPane}
  47. aria-label={selectors.components.PanelEditor.toggleVizOptions}
  48. />
  49. </ButtonGroup>
  50. </div>
  51. );
  52. };
  53. VisualizationButton.displayName = 'VisualizationTab';
  54. const getStyles = (theme: GrafanaTheme) => {
  55. return {
  56. wrapper: css`
  57. display: flex;
  58. flex-direction: column;
  59. `,
  60. vizButton: css`
  61. text-align: left;
  62. `,
  63. };
  64. };