widgetDetails.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import {useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import {TabList, TabPanels, Tabs} from 'sentry/components/tabs';
  4. import {Tooltip} from 'sentry/components/tooltip';
  5. import {t} from 'sentry/locale';
  6. import {space} from 'sentry/styles/space';
  7. import {isCustomMetric, MetricWidgetQueryParams} from 'sentry/utils/metrics';
  8. import {CodeLocations} from 'sentry/views/ddm/codeLocations';
  9. import {useDDMContext} from 'sentry/views/ddm/context';
  10. import {SampleTable} from 'sentry/views/ddm/sampleTable';
  11. enum Tab {
  12. SAMPLES = 'samples',
  13. CODE_LOCATIONS = 'codeLocations',
  14. }
  15. const constructQueryString = (queryObject: Record<string, string>) => {
  16. return Object.entries(queryObject)
  17. .map(([key, value]) => `${key}:"${value}"`)
  18. .join(' ');
  19. };
  20. export function WidgetDetails() {
  21. const {
  22. selectedWidgetIndex,
  23. widgets,
  24. focusArea,
  25. highlightedSampleId,
  26. setHighlightedSampleId,
  27. } = useDDMContext();
  28. const [selectedTab, setSelectedTab] = useState(Tab.SAMPLES);
  29. // the tray is minimized when the main content is maximized
  30. const selectedWidget = widgets[selectedWidgetIndex] as
  31. | MetricWidgetQueryParams
  32. | undefined;
  33. const isCodeLocationsDisabled =
  34. selectedWidget?.mri && !isCustomMetric({mri: selectedWidget.mri});
  35. if (isCodeLocationsDisabled && selectedTab === Tab.CODE_LOCATIONS) {
  36. setSelectedTab(Tab.SAMPLES);
  37. }
  38. const handleSampleRowHover = (sampleId?: string) => {
  39. setHighlightedSampleId(sampleId);
  40. };
  41. return (
  42. <TrayWrapper>
  43. <Tabs value={selectedTab} onChange={setSelectedTab}>
  44. <TabList>
  45. <TabList.Item key={Tab.SAMPLES}>{t('Samples')}</TabList.Item>
  46. <TabList.Item
  47. textValue={t('Code Location')}
  48. key={Tab.CODE_LOCATIONS}
  49. disabled={isCodeLocationsDisabled}
  50. >
  51. <Tooltip
  52. title={t(
  53. 'This metric is automatically collected by Sentry. It is not bound to a specific line of your code.'
  54. )}
  55. disabled={!isCodeLocationsDisabled}
  56. >
  57. <span style={{pointerEvents: 'all'}}>{t('Code Location')}</span>
  58. </Tooltip>
  59. </TabList.Item>
  60. </TabList>
  61. <ContentWrapper>
  62. <TabPanels>
  63. <TabPanels.Item key={Tab.SAMPLES}>
  64. <SampleTable
  65. mri={selectedWidget?.mri}
  66. query={
  67. selectedWidget?.focusedSeries?.groupBy
  68. ? `${selectedWidget.query} ${constructQueryString(
  69. selectedWidget.focusedSeries.groupBy
  70. )}`.trim()
  71. : selectedWidget?.query
  72. }
  73. {...focusArea?.range}
  74. highlightedRow={highlightedSampleId}
  75. onRowHover={handleSampleRowHover}
  76. />
  77. </TabPanels.Item>
  78. <TabPanels.Item key={Tab.CODE_LOCATIONS}>
  79. <CodeLocations mri={selectedWidget?.mri} {...focusArea?.range} />
  80. </TabPanels.Item>
  81. </TabPanels>
  82. </ContentWrapper>
  83. </Tabs>
  84. </TrayWrapper>
  85. );
  86. }
  87. const TrayWrapper = styled('div')`
  88. padding-top: ${space(4)};
  89. display: grid;
  90. grid-template-rows: auto auto 1fr;
  91. `;
  92. const ContentWrapper = styled('div')`
  93. position: relative;
  94. padding: ${space(2)} 0;
  95. `;