scratchpad.tsx 3.2 KB

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