scratchpad.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import {useCallback, useLayoutEffect} from 'react';
  2. import styled from '@emotion/styled';
  3. import * as Sentry from '@sentry/react';
  4. import * as echarts from 'echarts/core';
  5. import {Button} from 'sentry/components/button';
  6. import Panel from 'sentry/components/panels/panel';
  7. import {IconAdd} from 'sentry/icons';
  8. import {t} from 'sentry/locale';
  9. import {space} from 'sentry/styles/space';
  10. import {trackAnalytics} from 'sentry/utils/analytics';
  11. import {MetricWidgetQueryParams} from 'sentry/utils/metrics';
  12. import useOrganization from 'sentry/utils/useOrganization';
  13. import usePageFilters from 'sentry/utils/usePageFilters';
  14. import {DDM_CHART_GROUP, MIN_WIDGET_WIDTH} from 'sentry/views/ddm/constants';
  15. import {useDDMContext} from 'sentry/views/ddm/context';
  16. import {MetricWidget} from './widget';
  17. export function MetricScratchpad() {
  18. const {
  19. setSelectedWidgetIndex,
  20. selectedWidgetIndex,
  21. widgets,
  22. updateWidget,
  23. addWidget,
  24. focusArea,
  25. addFocusArea,
  26. removeFocusArea,
  27. } = useDDMContext();
  28. const {selection} = usePageFilters();
  29. const organization = useOrganization();
  30. // Make sure all charts are connected to the same group whenever the widgets definition changes
  31. useLayoutEffect(() => {
  32. echarts.connect(DDM_CHART_GROUP);
  33. }, [widgets]);
  34. const handleChange = useCallback(
  35. (index: number, widget: Partial<MetricWidgetQueryParams>) => {
  36. updateWidget(index, widget);
  37. },
  38. [updateWidget]
  39. );
  40. const hasEmptyWidget = widgets.length === 0 || widgets.some(widget => !widget.mri);
  41. const Wrapper =
  42. widgets.length === 1 ? StyledSingleWidgetWrapper : StyledMetricDashboard;
  43. return (
  44. <Wrapper>
  45. {widgets.map((widget, index) => (
  46. <MetricWidget
  47. key={index}
  48. index={index}
  49. onSelect={setSelectedWidgetIndex}
  50. isSelected={selectedWidgetIndex === index}
  51. hasSiblings={widgets.length > 1}
  52. onChange={handleChange}
  53. widget={widget}
  54. datetime={selection.datetime}
  55. projects={selection.projects}
  56. environments={selection.environments}
  57. addFocusArea={addFocusArea}
  58. removeFocusArea={removeFocusArea}
  59. focusArea={focusArea}
  60. />
  61. ))}
  62. <AddWidgetPanel
  63. disabled={hasEmptyWidget}
  64. onClick={
  65. !hasEmptyWidget
  66. ? () => {
  67. trackAnalytics('ddm.widget.add', {
  68. organization,
  69. });
  70. Sentry.metrics.increment('ddm.widget.add');
  71. addWidget();
  72. }
  73. : undefined
  74. }
  75. >
  76. <Button disabled={hasEmptyWidget} icon={<IconAdd isCircled />}>
  77. {t('Add widget')}
  78. </Button>
  79. </AddWidgetPanel>
  80. </Wrapper>
  81. );
  82. }
  83. const StyledMetricDashboard = styled('div')`
  84. display: grid;
  85. grid-template-columns: repeat(3, minmax(${MIN_WIDGET_WIDTH}px, 1fr));
  86. gap: ${space(2)};
  87. @media (max-width: ${props => props.theme.breakpoints.xxlarge}) {
  88. grid-template-columns: repeat(2, minmax(${MIN_WIDGET_WIDTH}px, 1fr));
  89. }
  90. @media (max-width: ${props => props.theme.breakpoints.xlarge}) {
  91. grid-template-columns: repeat(1, minmax(${MIN_WIDGET_WIDTH}px, 1fr));
  92. }
  93. grid-auto-rows: auto;
  94. `;
  95. const StyledSingleWidgetWrapper = styled('div')`
  96. display: grid;
  97. grid-template-columns: minmax(${MIN_WIDGET_WIDTH}px, 90%) minmax(180px, 10%);
  98. @media (max-width: ${props => props.theme.breakpoints.xlarge}) {
  99. grid-template-columns: repeat(1, minmax(${MIN_WIDGET_WIDTH}px, 1fr));
  100. }
  101. gap: ${space(2)};
  102. grid-auto-rows: auto;
  103. `;
  104. const AddWidgetPanel = styled(Panel)<{disabled: boolean}>`
  105. height: 100%;
  106. margin-bottom: 0;
  107. padding: ${space(4)};
  108. font-size: ${p => p.theme.fontSizeExtraLarge};
  109. display: flex;
  110. justify-content: center;
  111. align-items: center;
  112. border: 1px dashed ${p => (p.disabled ? p.theme.disabledBorder : p.theme.border)};
  113. ${p =>
  114. !p.disabled &&
  115. `
  116. &:hover {
  117. background-color: ${p.theme.backgroundSecondary};
  118. cursor: pointer;
  119. }
  120. `}
  121. `;