queries.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import {useCallback, useLayoutEffect} from 'react';
  2. import styled from '@emotion/styled';
  3. import * as echarts from 'echarts/core';
  4. import Feature from 'sentry/components/acl/feature';
  5. import {t} from 'sentry/locale';
  6. import {space} from 'sentry/styles/space';
  7. import type {MetricWidgetQueryParams} from 'sentry/utils/metrics/types';
  8. import usePageFilters from 'sentry/utils/usePageFilters';
  9. import {DDM_CHART_GROUP} from 'sentry/views/ddm/constants';
  10. import {useDDMContext} from 'sentry/views/ddm/context';
  11. import {MetricQueryContextMenu} from 'sentry/views/ddm/contextMenu';
  12. import {QueryBuilder} from 'sentry/views/ddm/queryBuilder';
  13. import {QuerySymbol} from 'sentry/views/ddm/querySymbol';
  14. export function Queries() {
  15. const {
  16. widgets,
  17. updateWidget,
  18. setSelectedWidgetIndex,
  19. showQuerySymbols,
  20. selectedWidgetIndex,
  21. } = useDDMContext();
  22. const {selection} = usePageFilters();
  23. // Make sure all charts are connected to the same group whenever the widgets definition changes
  24. useLayoutEffect(() => {
  25. echarts.connect(DDM_CHART_GROUP);
  26. }, [widgets]);
  27. const handleChange = useCallback(
  28. (index: number, widget: Partial<MetricWidgetQueryParams>) => {
  29. updateWidget(index, widget);
  30. },
  31. [updateWidget]
  32. );
  33. return (
  34. <Wrapper showQuerySymbols={showQuerySymbols}>
  35. {widgets.map((widget, index) => (
  36. <Row key={index} onFocusCapture={() => setSelectedWidgetIndex(index)}>
  37. {showQuerySymbols && (
  38. <StyledQuerySymbol
  39. index={index}
  40. isSelected={index === selectedWidgetIndex}
  41. onClick={() => setSelectedWidgetIndex(index)}
  42. role="button"
  43. aria-label={t('Select query')}
  44. />
  45. )}
  46. <QueryBuilder
  47. onChange={data => handleChange(index, data)}
  48. metricsQuery={{
  49. mri: widget.mri,
  50. op: widget.op,
  51. groupBy: widget.groupBy,
  52. title: widget.title,
  53. query: widget.query,
  54. }}
  55. displayType={widget.displayType}
  56. isEdit
  57. projects={selection.projects}
  58. />
  59. <Feature
  60. hookName="feature-disabled:dashboards-edit"
  61. features="organizations:dashboards-edit"
  62. >
  63. {({hasFeature}) => (
  64. <MetricQueryContextMenu
  65. displayType={widget.displayType}
  66. widgetIndex={index}
  67. hasDashboardFeature={hasFeature}
  68. metricsQuery={{
  69. mri: widget.mri,
  70. query: widget.query,
  71. op: widget.op,
  72. groupBy: widget.groupBy,
  73. projects: selection.projects,
  74. datetime: selection.datetime,
  75. environments: selection.environments,
  76. title: widget.title,
  77. }}
  78. />
  79. )}
  80. </Feature>
  81. </Row>
  82. ))}
  83. </Wrapper>
  84. );
  85. }
  86. const StyledQuerySymbol = styled(QuerySymbol)`
  87. margin-top: 10px;
  88. cursor: pointer;
  89. `;
  90. const Wrapper = styled('div')<{showQuerySymbols: boolean}>`
  91. padding-bottom: ${space(2)};
  92. display: grid;
  93. grid-template-columns: 1fr max-content;
  94. gap: ${space(1)};
  95. ${p =>
  96. p.showQuerySymbols &&
  97. `
  98. grid-template-columns: min-content 1fr max-content;
  99. `}
  100. `;
  101. const Row = styled('div')`
  102. display: contents;
  103. `;