scratchpad.tsx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import styled from '@emotion/styled';
  2. import * as echarts from 'echarts/core';
  3. import {Button} from 'sentry/components/button';
  4. import Panel from 'sentry/components/panels/panel';
  5. import {IconAdd} from 'sentry/icons';
  6. import {space} from 'sentry/styles/space';
  7. import usePageFilters from 'sentry/utils/usePageFilters';
  8. import {DDM_CHART_GROUP, MIN_WIDGET_WIDTH} from 'sentry/views/ddm/constants';
  9. import {MetricWidget, useMetricWidgets} from './widget';
  10. export function MetricScratchpad() {
  11. const {widgets, onChange, addWidget} = useMetricWidgets();
  12. const {selection} = usePageFilters();
  13. const Wrapper =
  14. widgets.length === 1 ? StyledSingleWidgetWrapper : StyledMetricDashboard;
  15. echarts.connect(DDM_CHART_GROUP);
  16. return (
  17. <Wrapper>
  18. {widgets.map(widget => (
  19. <MetricWidget
  20. key={widget.position}
  21. widget={{
  22. ...widget,
  23. onChange: data => {
  24. onChange(widget.position, data);
  25. },
  26. }}
  27. datetime={selection.datetime}
  28. projects={selection.projects}
  29. environments={selection.environments}
  30. />
  31. ))}
  32. <AddWidgetPanel onClick={addWidget}>
  33. <Button priority="primary" icon={<IconAdd isCircled />}>
  34. Add widget
  35. </Button>
  36. </AddWidgetPanel>
  37. </Wrapper>
  38. );
  39. }
  40. const StyledMetricDashboard = styled('div')`
  41. display: grid;
  42. grid-template-columns: repeat(3, minmax(${MIN_WIDGET_WIDTH}px, 1fr));
  43. gap: ${space(2)};
  44. @media (max-width: ${props => props.theme.breakpoints.xxlarge}) {
  45. grid-template-columns: repeat(2, minmax(${MIN_WIDGET_WIDTH}px, 1fr));
  46. }
  47. @media (max-width: ${props => props.theme.breakpoints.xlarge}) {
  48. grid-template-columns: repeat(1, minmax(${MIN_WIDGET_WIDTH}px, 1fr));
  49. }
  50. `;
  51. const StyledSingleWidgetWrapper = styled('div')`
  52. display: flex;
  53. flex-direction: column;
  54. gap: ${space(2)};
  55. `;
  56. const AddWidgetPanel = styled(Panel)`
  57. margin-bottom: 0;
  58. padding: ${space(4)};
  59. min-width: ${MIN_WIDGET_WIDTH};
  60. font-size: ${p => p.theme.fontSizeExtraLarge};
  61. display: flex;
  62. justify-content: center;
  63. align-items: center;
  64. border: 1px dashed ${p => p.theme.border};
  65. &:hover {
  66. background-color: ${p => p.theme.backgroundSecondary};
  67. cursor: pointer;
  68. }
  69. `;