scratchpad.tsx 2.3 KB

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