queries.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import {useCallback, useLayoutEffect} from 'react';
  2. import styled from '@emotion/styled';
  3. import * as echarts from 'echarts/core';
  4. import {space} from 'sentry/styles/space';
  5. import {MetricWidgetQueryParams} from 'sentry/utils/metrics';
  6. import usePageFilters from 'sentry/utils/usePageFilters';
  7. import {DDM_CHART_GROUP} from 'sentry/views/ddm/constants';
  8. import {useDDMContext} from 'sentry/views/ddm/context';
  9. import {MetricQueryContextMenu} from 'sentry/views/ddm/contextMenu';
  10. import {QueryBuilder} from 'sentry/views/ddm/queryBuilder';
  11. import {QuerySymbol} from 'sentry/views/ddm/querySymbol';
  12. export function Queries() {
  13. const {widgets, updateWidget, setSelectedWidgetIndex, showQuerySymbols} =
  14. useDDMContext();
  15. const {selection} = usePageFilters();
  16. // Make sure all charts are connected to the same group whenever the widgets definition changes
  17. useLayoutEffect(() => {
  18. echarts.connect(DDM_CHART_GROUP);
  19. }, [widgets]);
  20. const handleChange = useCallback(
  21. (index: number, widget: Partial<MetricWidgetQueryParams>) => {
  22. updateWidget(index, widget);
  23. },
  24. [updateWidget]
  25. );
  26. return (
  27. <Wrapper showQuerySymbols={showQuerySymbols}>
  28. {widgets.map((widget, index) => (
  29. <Row key={index} onFocusCapture={() => setSelectedWidgetIndex(index)}>
  30. {showQuerySymbols && <StyledQuerySymbol index={index} />}
  31. <QueryBuilder
  32. onChange={data => handleChange(index, data)}
  33. metricsQuery={{
  34. mri: widget.mri,
  35. op: widget.op,
  36. groupBy: widget.groupBy,
  37. title: widget.title,
  38. }}
  39. displayType={widget.displayType}
  40. isEdit
  41. projects={selection.projects}
  42. />
  43. <MetricQueryContextMenu
  44. displayType={widget.displayType}
  45. widgetIndex={index}
  46. metricsQuery={{
  47. mri: widget.mri,
  48. query: widget.query,
  49. op: widget.op,
  50. groupBy: widget.groupBy,
  51. projects: selection.projects,
  52. datetime: selection.datetime,
  53. environments: selection.environments,
  54. title: widget.title,
  55. }}
  56. />
  57. </Row>
  58. ))}
  59. </Wrapper>
  60. );
  61. }
  62. const StyledQuerySymbol = styled(QuerySymbol)`
  63. margin-top: 10px;
  64. `;
  65. const Wrapper = styled('div')<{showQuerySymbols: boolean}>`
  66. padding-bottom: ${space(2)};
  67. display: grid;
  68. grid-template-columns: 1fr max-content;
  69. gap: ${space(1)};
  70. ${p =>
  71. p.showQuerySymbols &&
  72. `
  73. grid-template-columns: min-content 1fr max-content;
  74. `}
  75. `;
  76. const Row = styled('div')`
  77. display: contents;
  78. `;