scratchpad.tsx 3.3 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 {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. 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. const eventSlug = generateEventSlug({
  43. id: sample.transactionId,
  44. project: project?.slug,
  45. });
  46. router.push(
  47. getTransactionDetailsUrl(
  48. organization.slug,
  49. eventSlug,
  50. undefined,
  51. {referrer: 'metrics'},
  52. sample.spanId
  53. )
  54. );
  55. },
  56. [router, organization.slug, projects]
  57. );
  58. const Wrapper =
  59. widgets.length === 1 ? StyledSingleWidgetWrapper : StyledMetricDashboard;
  60. return (
  61. <Wrapper>
  62. {widgets.map((widget, index) => (
  63. <MetricWidget
  64. key={index}
  65. index={index}
  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. highlightedSampleId={highlightedSampleId}
  78. />
  79. ))}
  80. </Wrapper>
  81. );
  82. }
  83. const StyledMetricDashboard = styled('div')`
  84. display: grid;
  85. grid-template-columns: repeat(3, minmax(${MIN_WIDGET_WIDTH}px, 1fr));
  86. gap: ${space(2)};
  87. @media (max-width: ${props => props.theme.breakpoints.xxlarge}) {
  88. grid-template-columns: repeat(2, minmax(${MIN_WIDGET_WIDTH}px, 1fr));
  89. }
  90. @media (max-width: ${props => props.theme.breakpoints.xlarge}) {
  91. grid-template-columns: repeat(1, minmax(${MIN_WIDGET_WIDTH}px, 1fr));
  92. }
  93. grid-auto-rows: auto;
  94. `;
  95. const StyledSingleWidgetWrapper = styled('div')`
  96. display: grid;
  97. grid-template-columns: repeat(1, minmax(${MIN_WIDGET_WIDTH}px, 1fr));
  98. `;