scratchpad.tsx 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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, MIN_WIDGET_WIDTH} from 'sentry/views/ddm/constants';
  8. import {useDDMContext} from 'sentry/views/ddm/context';
  9. import {MetricWidget} from './widget';
  10. export function MetricScratchpad() {
  11. const {
  12. setSelectedWidgetIndex,
  13. selectedWidgetIndex,
  14. widgets,
  15. updateWidget,
  16. focusArea,
  17. addFocusArea,
  18. removeFocusArea,
  19. showQuerySymbols,
  20. } = useDDMContext();
  21. const {selection} = usePageFilters();
  22. // Make sure all charts are connected to the same group whenever the widgets definition changes
  23. useLayoutEffect(() => {
  24. echarts.connect(DDM_CHART_GROUP);
  25. }, [widgets]);
  26. const handleChange = useCallback(
  27. (index: number, widget: Partial<MetricWidgetQueryParams>) => {
  28. updateWidget(index, widget);
  29. },
  30. [updateWidget]
  31. );
  32. const Wrapper =
  33. widgets.length === 1 ? StyledSingleWidgetWrapper : StyledMetricDashboard;
  34. return (
  35. <Wrapper>
  36. {widgets.map((widget, index) => (
  37. <MetricWidget
  38. key={index}
  39. index={index}
  40. onSelect={setSelectedWidgetIndex}
  41. isSelected={selectedWidgetIndex === index}
  42. hasSiblings={widgets.length > 1}
  43. onChange={handleChange}
  44. widget={widget}
  45. datetime={selection.datetime}
  46. projects={selection.projects}
  47. environments={selection.environments}
  48. addFocusArea={addFocusArea}
  49. removeFocusArea={removeFocusArea}
  50. showQuerySymbols={showQuerySymbols}
  51. focusArea={focusArea}
  52. />
  53. ))}
  54. </Wrapper>
  55. );
  56. }
  57. const StyledMetricDashboard = styled('div')`
  58. display: grid;
  59. grid-template-columns: repeat(3, minmax(${MIN_WIDGET_WIDTH}px, 1fr));
  60. gap: ${space(2)};
  61. @media (max-width: ${props => props.theme.breakpoints.xxlarge}) {
  62. grid-template-columns: repeat(2, minmax(${MIN_WIDGET_WIDTH}px, 1fr));
  63. }
  64. @media (max-width: ${props => props.theme.breakpoints.xlarge}) {
  65. grid-template-columns: repeat(1, minmax(${MIN_WIDGET_WIDTH}px, 1fr));
  66. }
  67. grid-auto-rows: auto;
  68. `;
  69. const StyledSingleWidgetWrapper = styled('div')`
  70. display: grid;
  71. grid-template-columns: repeat(1, minmax(${MIN_WIDGET_WIDTH}px, 1fr));
  72. `;