GraphiteQueryEditor.tsx 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { css } from '@emotion/css';
  2. import React from 'react';
  3. import { GrafanaTheme2 } from '@grafana/data';
  4. import { Button, useStyles2 } from '@grafana/ui';
  5. import { actions } from '../state/actions';
  6. import { GraphiteQueryEditorContext, GraphiteQueryEditorProps, useDispatch, useGraphiteState } from '../state/context';
  7. import { FunctionsSection } from './FunctionsSection';
  8. import { GraphiteTextEditor } from './GraphiteTextEditor';
  9. import { SeriesSection } from './SeriesSection';
  10. export function GraphiteQueryEditor({
  11. datasource,
  12. onRunQuery,
  13. onChange,
  14. query,
  15. range,
  16. queries,
  17. }: GraphiteQueryEditorProps) {
  18. return (
  19. <GraphiteQueryEditorContext
  20. datasource={datasource}
  21. onRunQuery={onRunQuery}
  22. onChange={onChange}
  23. query={query}
  24. queries={queries}
  25. range={range}
  26. >
  27. <GraphiteQueryEditorContent />
  28. </GraphiteQueryEditorContext>
  29. );
  30. }
  31. function GraphiteQueryEditorContent() {
  32. const dispatch = useDispatch();
  33. const state = useGraphiteState();
  34. const styles = useStyles2(getStyles);
  35. return (
  36. <div className={styles.container}>
  37. <div className={styles.visualEditor}>
  38. {state.target?.textEditor && <GraphiteTextEditor rawQuery={state.target.target} />}
  39. {!state.target?.textEditor && (
  40. <>
  41. <SeriesSection state={state} />
  42. <FunctionsSection functions={state.queryModel?.functions} funcDefs={state.funcDefs!} />
  43. </>
  44. )}
  45. </div>
  46. <Button
  47. className={styles.toggleButton}
  48. icon="pen"
  49. variant="secondary"
  50. aria-label="Toggle editor mode"
  51. onClick={() => {
  52. dispatch(actions.toggleEditorMode());
  53. }}
  54. />
  55. </div>
  56. );
  57. }
  58. function getStyles(theme: GrafanaTheme2) {
  59. return {
  60. container: css`
  61. display: flex;
  62. `,
  63. visualEditor: css`
  64. flex-grow: 1;
  65. `,
  66. toggleButton: css`
  67. margin-left: ${theme.spacing(0.5)};
  68. `,
  69. };
  70. }