scratchpad.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 {generateEventSlug} from 'sentry/utils/discover/urls';
  6. import type {MetricWidgetQueryParams} from 'sentry/utils/metrics/types';
  7. import {getTransactionDetailsUrl} from 'sentry/utils/performance/urls';
  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 type {Sample} from './widget';
  15. import {MetricWidget} from './widget';
  16. export function MetricScratchpad() {
  17. const {
  18. setSelectedWidgetIndex,
  19. selectedWidgetIndex,
  20. widgets,
  21. updateWidget,
  22. focusArea,
  23. addFocusArea,
  24. removeFocusArea,
  25. showQuerySymbols,
  26. highlightedSampleId,
  27. } = useDDMContext();
  28. const {selection} = usePageFilters();
  29. const router = useRouter();
  30. const organization = useOrganization();
  31. const {projects} = useProjects();
  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. const eventSlug = generateEventSlug({
  46. id: sample.transactionId,
  47. project: project?.slug,
  48. });
  49. router.push(
  50. getTransactionDetailsUrl(
  51. organization.slug,
  52. eventSlug,
  53. undefined,
  54. {referrer: 'metrics'},
  55. sample.spanId
  56. )
  57. );
  58. },
  59. [router, organization.slug, projects]
  60. );
  61. const Wrapper =
  62. widgets.length === 1 ? StyledSingleWidgetWrapper : StyledMetricDashboard;
  63. return (
  64. <Wrapper>
  65. {widgets.map((widget, index) => (
  66. <MetricWidget
  67. key={index}
  68. index={index}
  69. onSelect={setSelectedWidgetIndex}
  70. isSelected={selectedWidgetIndex === index}
  71. hasSiblings={widgets.length > 1}
  72. onChange={handleChange}
  73. widget={widget}
  74. datetime={selection.datetime}
  75. projects={selection.projects}
  76. environments={selection.environments}
  77. addFocusArea={addFocusArea}
  78. removeFocusArea={removeFocusArea}
  79. showQuerySymbols={showQuerySymbols}
  80. focusArea={focusArea}
  81. onSampleClick={handleSampleClick}
  82. highlightedSampleId={highlightedSampleId}
  83. />
  84. ))}
  85. </Wrapper>
  86. );
  87. }
  88. const StyledMetricDashboard = styled('div')`
  89. display: grid;
  90. grid-template-columns: repeat(3, minmax(${MIN_WIDGET_WIDTH}px, 1fr));
  91. gap: ${space(2)};
  92. @media (max-width: ${props => props.theme.breakpoints.xxlarge}) {
  93. grid-template-columns: repeat(2, minmax(${MIN_WIDGET_WIDTH}px, 1fr));
  94. }
  95. @media (max-width: ${props => props.theme.breakpoints.xlarge}) {
  96. grid-template-columns: repeat(1, minmax(${MIN_WIDGET_WIDTH}px, 1fr));
  97. }
  98. grid-auto-rows: auto;
  99. `;
  100. const StyledSingleWidgetWrapper = styled('div')`
  101. display: grid;
  102. grid-template-columns: repeat(1, minmax(${MIN_WIDGET_WIDTH}px, 1fr));
  103. `;