scratchpad.tsx 3.4 KB

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