trayContent.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import {useContext, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import {Button} from 'sentry/components/button';
  4. import EmptyMessage from 'sentry/components/emptyMessage';
  5. import {SplitPanelContext} from 'sentry/components/splitPanel';
  6. import {TabList, Tabs} from 'sentry/components/tabs';
  7. import {Tooltip} from 'sentry/components/tooltip';
  8. import {IconChevron, IconSearch} from 'sentry/icons';
  9. import {t} from 'sentry/locale';
  10. import {space} from 'sentry/styles/space';
  11. import {isCustomMetric} from 'sentry/utils/metrics';
  12. import {formatMRI} from 'sentry/utils/metrics/mri';
  13. import {CodeLocations} from 'sentry/views/ddm/codeLocations';
  14. import {useDDMContext} from 'sentry/views/ddm/context';
  15. import {TraceTable} from 'sentry/views/ddm/traceTable';
  16. enum Tab {
  17. SAMPLES = 'samples',
  18. CODE_LOCATIONS = 'codeLocations',
  19. }
  20. export function TrayContent() {
  21. const {selectedWidgetIndex, widgets} = useDDMContext();
  22. const [selectedTab, setSelectedTab] = useState(Tab.CODE_LOCATIONS);
  23. const {isMaximized, maximiseSize, resetSize} = useContext(SplitPanelContext);
  24. // the tray is minimized when the main content is maximized
  25. const trayIsMinimized = isMaximized;
  26. const selectedWidget = widgets[selectedWidgetIndex];
  27. const isCodeLocationsDisabled =
  28. selectedWidget.mri && !isCustomMetric({mri: selectedWidget.mri});
  29. if (isCodeLocationsDisabled && selectedTab === Tab.CODE_LOCATIONS) {
  30. setSelectedTab(Tab.SAMPLES);
  31. }
  32. return (
  33. <TrayWrapper>
  34. <Header>
  35. <Title>
  36. {selectedWidget?.mri
  37. ? formatMRI(selectedWidget.mri)
  38. : t('Choose a metric to display data')}
  39. </Title>
  40. <ToggleButton
  41. size="xs"
  42. isMinimized={trayIsMinimized}
  43. icon={<IconChevron size="xs" />}
  44. onClick={trayIsMinimized ? resetSize : maximiseSize}
  45. aria-label={trayIsMinimized ? t('show') : t('hide')}
  46. />
  47. </Header>
  48. <Tabs value={selectedTab} onChange={setSelectedTab}>
  49. <StyledTabList>
  50. <TabList.Item
  51. textValue={t('Code Location')}
  52. key={Tab.CODE_LOCATIONS}
  53. disabled={isCodeLocationsDisabled}
  54. >
  55. <Tooltip
  56. title={t(
  57. 'This metric is automatically collected by Sentry. It is not bound to a specific line of your code.'
  58. )}
  59. disabled={!isCodeLocationsDisabled}
  60. >
  61. <span style={{pointerEvents: 'all'}}>{t('Code Location')}</span>
  62. </Tooltip>
  63. </TabList.Item>
  64. <TabList.Item key={Tab.SAMPLES}>{t('Samples')}</TabList.Item>
  65. </StyledTabList>
  66. </Tabs>
  67. <ContentWrapper>
  68. {!selectedWidget?.mri ? (
  69. <CenterContent>
  70. <EmptyMessage
  71. style={{margin: 'auto'}}
  72. icon={<IconSearch size="xxl" />}
  73. title={t('Nothing to show!')}
  74. description={t('Choose a metric to display data.')}
  75. />
  76. </CenterContent>
  77. ) : selectedTab === Tab.SAMPLES ? (
  78. <TraceTable
  79. // Force re-render when selectedWidget changes so the mocked data updates
  80. // TODO: remove this when we have real data
  81. key={selectedWidget.mri}
  82. />
  83. ) : (
  84. <CodeLocations mri={selectedWidget.mri} />
  85. )}
  86. </ContentWrapper>
  87. </TrayWrapper>
  88. );
  89. }
  90. const TrayWrapper = styled('div')`
  91. height: 100%;
  92. background-color: ${p => p.theme.background};
  93. z-index: ${p => p.theme.zIndex.sidebar};
  94. display: grid;
  95. grid-template-rows: auto auto 1fr;
  96. `;
  97. const Header = styled('div')`
  98. display: flex;
  99. flex-direction: row;
  100. justify-content: space-between;
  101. align-items: center;
  102. padding: 0 ${space(4)};
  103. height: 32px;
  104. background-color: ${p => p.theme.backgroundSecondary};
  105. `;
  106. const Title = styled('div')`
  107. font-size: ${p => p.theme.fontSizeLarge};
  108. font-weight: bold;
  109. `;
  110. const ToggleButton = styled(Button)<{isMinimized}>`
  111. & svg {
  112. transform: rotate(${p => (p.isMinimized ? '0deg' : '180deg')});
  113. }
  114. `;
  115. const StyledTabList = styled(TabList)`
  116. padding: 0 ${space(4)};
  117. background-color: ${p => p.theme.backgroundSecondary};
  118. `;
  119. const ContentWrapper = styled('div')`
  120. position: relative;
  121. padding: ${space(2)} ${space(4)};
  122. overflow: auto;
  123. `;
  124. const CenterContent = styled('div')`
  125. display: flex;
  126. justify-content: center;
  127. align-items: center;
  128. height: 100%;
  129. `;