queries.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. query: widget.query,
  53. }}
  54. displayType={widget.displayType}
  55. isEdit
  56. projects={selection.projects}
  57. />
  58. <Feature
  59. hookName="feature-disabled:dashboards-edit"
  60. features="organizations:dashboards-edit"
  61. >
  62. {({hasFeature}) => (
  63. <MetricQueryContextMenu
  64. displayType={widget.displayType}
  65. widgetIndex={index}
  66. hasDashboardFeature={hasFeature}
  67. metricsQuery={{
  68. mri: widget.mri,
  69. query: widget.query,
  70. op: widget.op,
  71. groupBy: widget.groupBy,
  72. projects: selection.projects,
  73. datetime: selection.datetime,
  74. environments: selection.environments,
  75. }}
  76. />
  77. )}
  78. </Feature>
  79. </Row>
  80. ))}
  81. </Wrapper>
  82. );
  83. }
  84. const StyledQuerySymbol = styled(QuerySymbol)`
  85. margin-top: 10px;
  86. cursor: pointer;
  87. `;
  88. const Wrapper = styled('div')<{showQuerySymbols: boolean}>`
  89. padding-bottom: ${space(2)};
  90. display: grid;
  91. grid-template-columns: 1fr max-content;
  92. gap: ${space(1)};
  93. ${p =>
  94. p.showQuerySymbols &&
  95. `
  96. grid-template-columns: min-content 1fr max-content;
  97. `}
  98. `;
  99. const Row = styled('div')`
  100. display: contents;
  101. `;