scratchpad.tsx 3.5 KB

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