widgetDetails.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import {useCallback, useMemo, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import {MetricSamplesTable} from 'sentry/components/ddm/metricSamplesTable';
  4. import {TabList, TabPanels, Tabs} from 'sentry/components/tabs';
  5. import {Tooltip} from 'sentry/components/tooltip';
  6. import {t} from 'sentry/locale';
  7. import {space} from 'sentry/styles/space';
  8. import {trackAnalytics} from 'sentry/utils/analytics';
  9. import {isCustomMetric} from 'sentry/utils/metrics';
  10. import type {MetricWidgetQueryParams} from 'sentry/utils/metrics/types';
  11. import useOrganization from 'sentry/utils/useOrganization';
  12. import {CodeLocations} from 'sentry/views/ddm/codeLocations';
  13. import type {FocusAreaProps} from 'sentry/views/ddm/context';
  14. import {useDDMContext} from 'sentry/views/ddm/context';
  15. import type {SamplesTableProps} from 'sentry/views/ddm/sampleTable';
  16. import {SampleTable} from 'sentry/views/ddm/sampleTable';
  17. import {getQueryWithFocusedSeries} from 'sentry/views/ddm/utils';
  18. enum Tab {
  19. SAMPLES = 'samples',
  20. CODE_LOCATIONS = 'codeLocations',
  21. }
  22. export function WidgetDetails() {
  23. const {selectedWidgetIndex, widgets, focusArea, setHighlightedSampleId} =
  24. useDDMContext();
  25. const selectedWidget = widgets[selectedWidgetIndex] as
  26. | MetricWidgetQueryParams
  27. | undefined;
  28. const handleSampleRowHover = useCallback(
  29. (sampleId?: string) => {
  30. setHighlightedSampleId(sampleId);
  31. },
  32. [setHighlightedSampleId]
  33. );
  34. return (
  35. <MetricDetails
  36. widget={selectedWidget}
  37. onRowHover={handleSampleRowHover}
  38. focusArea={focusArea}
  39. />
  40. );
  41. }
  42. interface MetricDetailsProps {
  43. focusArea?: FocusAreaProps;
  44. onRowHover?: SamplesTableProps['onRowHover'];
  45. widget?: MetricWidgetQueryParams;
  46. }
  47. export function MetricDetails({widget, onRowHover, focusArea}: MetricDetailsProps) {
  48. const organization = useOrganization();
  49. const [selectedTab, setSelectedTab] = useState(Tab.SAMPLES);
  50. const isCodeLocationsDisabled = widget?.mri && !isCustomMetric({mri: widget.mri});
  51. if (isCodeLocationsDisabled && selectedTab === Tab.CODE_LOCATIONS) {
  52. setSelectedTab(Tab.SAMPLES);
  53. }
  54. const queryWithFocusedSeries = useMemo(
  55. () => widget && getQueryWithFocusedSeries(widget.query || '', widget.focusedSeries),
  56. [widget]
  57. );
  58. const handleTabChange = useCallback(
  59. (tab: Tab) => {
  60. if (tab === Tab.CODE_LOCATIONS) {
  61. trackAnalytics('ddm.code-locations', {
  62. organization,
  63. });
  64. }
  65. setSelectedTab(tab);
  66. },
  67. [organization]
  68. );
  69. return (
  70. <TrayWrapper>
  71. <Tabs value={selectedTab} onChange={handleTabChange}>
  72. <TabList>
  73. <TabList.Item key={Tab.SAMPLES}>{t('Sampled Events')}</TabList.Item>
  74. <TabList.Item
  75. textValue={t('Code Location')}
  76. key={Tab.CODE_LOCATIONS}
  77. disabled={isCodeLocationsDisabled}
  78. >
  79. <Tooltip
  80. title={t(
  81. 'This metric is automatically collected by Sentry. It is not bound to a specific line of your code.'
  82. )}
  83. disabled={!isCodeLocationsDisabled}
  84. >
  85. <span style={{pointerEvents: 'all'}}>{t('Code Location')}</span>
  86. </Tooltip>
  87. </TabList.Item>
  88. </TabList>
  89. <ContentWrapper>
  90. <TabPanels>
  91. <TabPanels.Item key={Tab.SAMPLES}>
  92. {organization.features.includes('metrics-samples-list') ? (
  93. <MetricSamplesTable mri={widget?.mri} query={queryWithFocusedSeries} />
  94. ) : (
  95. <SampleTable
  96. mri={widget?.mri}
  97. {...focusArea?.selection?.range}
  98. query={queryWithFocusedSeries}
  99. onRowHover={onRowHover}
  100. />
  101. )}
  102. </TabPanels.Item>
  103. <TabPanels.Item key={Tab.CODE_LOCATIONS}>
  104. <CodeLocations mri={widget?.mri} {...focusArea?.selection?.range} />
  105. </TabPanels.Item>
  106. </TabPanels>
  107. </ContentWrapper>
  108. </Tabs>
  109. </TrayWrapper>
  110. );
  111. }
  112. const TrayWrapper = styled('div')`
  113. padding-top: ${space(4)};
  114. display: grid;
  115. grid-template-rows: auto auto 1fr;
  116. `;
  117. const ContentWrapper = styled('div')`
  118. position: relative;
  119. padding-top: ${space(2)};
  120. `;