scratchpad.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 {getMetricsCorrelationSpanUrl} from 'sentry/utils/metrics';
  6. import {hasDDMExperimentalFeature} from 'sentry/utils/metrics/features';
  7. import type {MetricWidgetQueryParams} from 'sentry/utils/metrics/types';
  8. import useOrganization from 'sentry/utils/useOrganization';
  9. import usePageFilters from 'sentry/utils/usePageFilters';
  10. import useProjects from 'sentry/utils/useProjects';
  11. import useRouter from 'sentry/utils/useRouter';
  12. import {DDM_CHART_GROUP, MIN_WIDGET_WIDTH} from 'sentry/views/ddm/constants';
  13. import {useDDMContext} from 'sentry/views/ddm/context';
  14. import {useGetCachedChartPalette} from 'sentry/views/ddm/metricsChartPalette';
  15. import type {Sample} from './widget';
  16. import {MetricWidget} from './widget';
  17. export function MetricScratchpad() {
  18. const {
  19. setSelectedWidgetIndex,
  20. selectedWidgetIndex,
  21. widgets,
  22. updateWidget,
  23. showQuerySymbols,
  24. highlightedSampleId,
  25. focusArea,
  26. } = useDDMContext();
  27. const {selection} = usePageFilters();
  28. const router = useRouter();
  29. const organization = useOrganization();
  30. const {projects} = useProjects();
  31. const getChartPalette = useGetCachedChartPalette();
  32. // Make sure all charts are connected to the same group whenever the widgets definition changes
  33. useLayoutEffect(() => {
  34. echarts.connect(DDM_CHART_GROUP);
  35. }, [widgets]);
  36. const handleChange = useCallback(
  37. (index: number, widget: Partial<MetricWidgetQueryParams>) => {
  38. updateWidget(index, widget);
  39. },
  40. [updateWidget]
  41. );
  42. const handleSampleClick = useCallback(
  43. (sample: Sample) => {
  44. const project = projects.find(p => parseInt(p.id, 10) === sample.projectId);
  45. router.push(
  46. getMetricsCorrelationSpanUrl(
  47. organization,
  48. project?.slug,
  49. sample.spanId,
  50. sample.transactionId,
  51. sample.transactionSpanId
  52. )
  53. );
  54. },
  55. [projects, router, organization]
  56. );
  57. const Wrapper =
  58. widgets.length === 1 ? StyledSingleWidgetWrapper : StyledMetricDashboard;
  59. return (
  60. <Wrapper>
  61. {widgets.map((widget, index) => (
  62. <MetricWidget
  63. key={index}
  64. index={index}
  65. getChartPalette={getChartPalette}
  66. onSelect={setSelectedWidgetIndex}
  67. isSelected={selectedWidgetIndex === index}
  68. hasSiblings={widgets.length > 1}
  69. onChange={handleChange}
  70. widget={widget}
  71. datetime={selection.datetime}
  72. projects={selection.projects}
  73. environments={selection.environments}
  74. focusArea={focusArea}
  75. showQuerySymbols={showQuerySymbols}
  76. onSampleClick={handleSampleClick}
  77. chartHeight={hasDDMExperimentalFeature(organization) ? 200 : 300}
  78. highlightedSampleId={
  79. selectedWidgetIndex === index ? highlightedSampleId : undefined
  80. }
  81. />
  82. ))}
  83. </Wrapper>
  84. );
  85. }
  86. const StyledMetricDashboard = styled('div')`
  87. display: grid;
  88. grid-template-columns: repeat(3, minmax(${MIN_WIDGET_WIDTH}px, 1fr));
  89. gap: ${space(2)};
  90. @media (max-width: ${props => props.theme.breakpoints.xxlarge}) {
  91. grid-template-columns: repeat(2, minmax(${MIN_WIDGET_WIDTH}px, 1fr));
  92. }
  93. @media (max-width: ${props => props.theme.breakpoints.xlarge}) {
  94. grid-template-columns: repeat(1, minmax(${MIN_WIDGET_WIDTH}px, 1fr));
  95. }
  96. grid-auto-rows: auto;
  97. `;
  98. const StyledSingleWidgetWrapper = styled('div')`
  99. display: grid;
  100. grid-template-columns: repeat(1, minmax(${MIN_WIDGET_WIDTH}px, 1fr));
  101. `;