traceDrawer.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. import {useMemo, useRef} from 'react';
  2. import styled from '@emotion/styled';
  3. import type {Location} from 'history';
  4. import {Button} from 'sentry/components/button';
  5. import {IconPanel} from 'sentry/icons';
  6. import {t} from 'sentry/locale';
  7. import {space} from 'sentry/styles/space';
  8. import type {EventTransaction, Organization} from 'sentry/types';
  9. import type EventView from 'sentry/utils/discover/eventView';
  10. import type {
  11. TraceFullDetailed,
  12. TraceSplitResults,
  13. } from 'sentry/utils/performance/quickTrace/types';
  14. import type {UseApiQueryResult} from 'sentry/utils/queryClient';
  15. import type RequestError from 'sentry/utils/requestError/requestError';
  16. import {
  17. useResizableDrawer,
  18. type UseResizableDrawerOptions,
  19. } from 'sentry/utils/useResizableDrawer';
  20. import type {VirtualizedViewManager} from 'sentry/views/performance/newTraceDetails/virtualizedViewManager';
  21. import {
  22. isAutogroupedNode,
  23. isMissingInstrumentationNode,
  24. isSpanNode,
  25. isTraceErrorNode,
  26. isTransactionNode,
  27. } from '../guards';
  28. import type {TraceTree, TraceTreeNode} from '../traceTree';
  29. import NodeDetail from './tabs/details';
  30. import {TraceLevelDetails} from './tabs/trace';
  31. function getTabTitle(node: TraceTreeNode<TraceTree.NodeValue>) {
  32. if (isTransactionNode(node)) {
  33. return (
  34. t('Transaction: ') +
  35. node.value['transaction.op'] +
  36. (node.value.transaction ? ' - ' + node.value.transaction : '')
  37. );
  38. }
  39. if (isSpanNode(node)) {
  40. return (
  41. t('Span: ') +
  42. node.value.op +
  43. (node.value.description ? ' - ' + node.value.description : '')
  44. );
  45. }
  46. if (isAutogroupedNode(node)) {
  47. return t('Autogroup');
  48. }
  49. if (isMissingInstrumentationNode(node)) {
  50. return t('Missing Instrumentation');
  51. }
  52. if (isTraceErrorNode(node)) {
  53. return node.value.title || 'Error';
  54. }
  55. return t('Detail');
  56. }
  57. const MIN_TRACE_DRAWER_DIMENSTIONS: [number, number] = [480, 30];
  58. type TraceDrawerProps = {
  59. activeTab: 'trace' | 'node';
  60. drawerSize: number;
  61. layout: 'drawer bottom' | 'drawer left' | 'drawer right';
  62. location: Location;
  63. manager: VirtualizedViewManager;
  64. nodes: TraceTreeNode<TraceTree.NodeValue>[];
  65. onDrawerResize: (size: number) => void;
  66. onLayoutChange: (layout: 'drawer bottom' | 'drawer left' | 'drawer right') => void;
  67. organization: Organization;
  68. rootEventResults: UseApiQueryResult<EventTransaction, RequestError>;
  69. scrollToNode: (node: TraceTreeNode<TraceTree.NodeValue>) => void;
  70. setActiveTab: (tab: 'trace' | 'node') => void;
  71. trace: TraceTree;
  72. traceEventView: EventView;
  73. traces: TraceSplitResults<TraceFullDetailed> | null;
  74. };
  75. function TraceDrawer(props: TraceDrawerProps) {
  76. const panelRef = useRef<HTMLDivElement>(null);
  77. const onDrawerResize = props.onDrawerResize;
  78. const resizableDrawerOptions: UseResizableDrawerOptions = useMemo(() => {
  79. const isSidebarLayout =
  80. props.layout === 'drawer left' || props.layout === 'drawer right';
  81. const initialSize =
  82. props.drawerSize > 0
  83. ? props.drawerSize
  84. : isSidebarLayout
  85. ? // Half the screen minus the ~sidebar width
  86. Math.max(window.innerWidth * 0.5 - 220, MIN_TRACE_DRAWER_DIMENSTIONS[0])
  87. : // 30% of the screen height
  88. Math.max(window.innerHeight * 0.3);
  89. const min = isSidebarLayout ? window.innerWidth * 0.2 : 30;
  90. function onResize(newSize: number) {
  91. onDrawerResize(newSize);
  92. if (!panelRef.current) {
  93. return;
  94. }
  95. if (isSidebarLayout) {
  96. panelRef.current.style.width = `${newSize}px`;
  97. panelRef.current.style.height = `100%`;
  98. } else {
  99. panelRef.current.style.height = `${newSize}px`;
  100. panelRef.current.style.width = `100%`;
  101. }
  102. // @TODO This can visual delays as the rest of the view uses a resize observer
  103. // to adjust the layout. We should force a sync layout update + draw here to fix that.
  104. }
  105. return {
  106. initialSize,
  107. onResize,
  108. direction:
  109. props.layout === 'drawer left'
  110. ? 'left'
  111. : props.layout === 'drawer right'
  112. ? 'right'
  113. : 'up',
  114. min,
  115. };
  116. }, [props.layout, onDrawerResize, props.drawerSize]);
  117. const {onMouseDown} = useResizableDrawer(resizableDrawerOptions);
  118. return (
  119. <PanelWrapper ref={panelRef} layout={props.layout}>
  120. <ResizeableHandle layout={props.layout} onMouseDown={onMouseDown} />
  121. <TabsContainer
  122. hasIndicators={
  123. // Syncs the height of the tabs with the trace indicators
  124. props.trace.indicators.length > 0 && props.layout !== 'drawer bottom'
  125. }
  126. >
  127. <Tab
  128. active={props.activeTab === 'trace'}
  129. onClick={() => props.setActiveTab('trace')}
  130. >
  131. <TabButton>{t('Trace')}</TabButton>
  132. </Tab>
  133. {props.nodes.map((node, index) => {
  134. const title = getTabTitle(node);
  135. return (
  136. <Tab
  137. key={index}
  138. active={props.activeTab === 'node'}
  139. onClick={() => props.setActiveTab('node')}
  140. >
  141. <TabButton title={title}>{title}</TabButton>
  142. </Tab>
  143. );
  144. })}
  145. <TabLayoutControlsContainer>
  146. <TabLayoutControlItem>
  147. <DrawerButton
  148. active={props.layout === 'drawer left'}
  149. onClick={() => props.onLayoutChange('drawer left')}
  150. size="xs"
  151. title={t('Drawer left')}
  152. >
  153. <IconPanel size="xs" direction="left" />
  154. </DrawerButton>
  155. </TabLayoutControlItem>
  156. <TabLayoutControlItem>
  157. <DrawerButton
  158. active={props.layout === 'drawer bottom'}
  159. onClick={() => props.onLayoutChange('drawer bottom')}
  160. size="xs"
  161. title={t('Drawer bottom')}
  162. >
  163. <IconPanel size="xs" direction="down" />
  164. </DrawerButton>
  165. </TabLayoutControlItem>
  166. <TabLayoutControlItem>
  167. <DrawerButton
  168. active={props.layout === 'drawer right'}
  169. onClick={() => props.onLayoutChange('drawer right')}
  170. size="xs"
  171. title={t('Drawer right')}
  172. >
  173. <IconPanel size="xs" direction="right" />
  174. </DrawerButton>
  175. </TabLayoutControlItem>
  176. </TabLayoutControlsContainer>
  177. </TabsContainer>
  178. <Content layout={props.layout}>
  179. {props.activeTab === 'trace' ? (
  180. <TraceLevelDetails
  181. tree={props.trace}
  182. node={props.trace.root.children[0]}
  183. rootEventResults={props.rootEventResults}
  184. organization={props.organization}
  185. location={props.location}
  186. traces={props.traces}
  187. traceEventView={props.traceEventView}
  188. />
  189. ) : (
  190. props.nodes.map((node, index) => (
  191. <NodeDetail
  192. key={index}
  193. node={node}
  194. organization={props.organization}
  195. location={props.location}
  196. manager={props.manager}
  197. scrollToNode={props.scrollToNode}
  198. />
  199. ))
  200. )}
  201. </Content>
  202. </PanelWrapper>
  203. );
  204. }
  205. const ResizeableHandle = styled('div')<{
  206. layout: 'drawer bottom' | 'drawer left' | 'drawer right';
  207. }>`
  208. width: ${p => (p.layout === 'drawer bottom' ? '100%' : '12px')};
  209. height: ${p => (p.layout === 'drawer bottom' ? '12px' : '100%')};
  210. cursor: ${p => (p.layout === 'drawer bottom' ? 'ns-resize' : 'ew-resize')};
  211. position: absolute;
  212. top: ${p => (p.layout === 'drawer bottom' ? '-6px' : 0)};
  213. left: ${p =>
  214. p.layout === 'drawer bottom' ? 0 : p.layout === 'drawer right' ? '-6px' : 'initial'};
  215. right: ${p => (p.layout === 'drawer left' ? '-6px' : 0)};
  216. z-index: 1;
  217. `;
  218. const PanelWrapper = styled('div')<{
  219. layout: 'drawer bottom' | 'drawer left' | 'drawer right';
  220. }>`
  221. grid-area: drawer;
  222. display: flex;
  223. flex-direction: column;
  224. overflow: hidden;
  225. width: 100%;
  226. border-top: ${p =>
  227. p.layout === 'drawer bottom' ? `1px solid ${p.theme.border}` : 'none'};
  228. border-left: ${p =>
  229. p.layout === 'drawer right' ? `1px solid ${p.theme.border}` : 'none'};
  230. border-right: ${p =>
  231. p.layout === 'drawer left' ? `1px solid ${p.theme.border}` : 'none'};
  232. bottom: 0;
  233. right: 0;
  234. position: relative;
  235. background: ${p => p.theme.background};
  236. color: ${p => p.theme.textColor};
  237. text-align: left;
  238. z-index: 10;
  239. `;
  240. const TabsContainer = styled('ul')<{hasIndicators: boolean}>`
  241. list-style-type: none;
  242. width: 100%;
  243. height: ${p => (p.hasIndicators ? '44px' : '26px')};
  244. border-bottom: 1px solid ${p => p.theme.border};
  245. background-color: ${p => p.theme.backgroundSecondary};
  246. display: flex;
  247. align-items: center;
  248. justify-content: left;
  249. padding-left: ${space(2)};
  250. gap: ${space(1)};
  251. margin-bottom: 0;
  252. `;
  253. const TabLayoutControlsContainer = styled('ul')`
  254. list-style-type: none;
  255. padding-left: 0;
  256. margin-left: auto;
  257. margin-right: ${space(1.5)};
  258. flex: none;
  259. button {
  260. padding: ${space(0.5)};
  261. }
  262. `;
  263. const TabLayoutControlItem = styled('li')`
  264. display: inline-block;
  265. margin: 0;
  266. `;
  267. const Tab = styled('li')<{active: boolean}>`
  268. height: 100%;
  269. button {
  270. border-bottom: 2px solid ${p => (p.active ? p.theme.blue400 : 'transparent')};
  271. font-weight: ${p => (p.active ? 'bold' : 'normal')};
  272. }
  273. `;
  274. const TabButton = styled('button')`
  275. height: 100%;
  276. border: none;
  277. max-width: 260px;
  278. overflow: hidden;
  279. text-overflow: ellipsis;
  280. white-space: nowrap;
  281. border-top: 2px solid transparent;
  282. border-bottom: 2px solid transparent;
  283. border-radius: 0;
  284. margin: 0;
  285. padding: ${space(0.25)};
  286. font-size: ${p => p.theme.fontSizeSmall};
  287. color: ${p => p.theme.textColor};
  288. background: transparent;
  289. `;
  290. const Content = styled('div')<{layout: 'drawer bottom' | 'drawer left' | 'drawer right'}>`
  291. overflow: scroll;
  292. padding: ${space(1)};
  293. flex: 1;
  294. td {
  295. max-width: 100% !important;
  296. }
  297. ${p =>
  298. p.layout !== 'drawer bottom' &&
  299. `
  300. table {
  301. display: flex;
  302. }
  303. tbody {
  304. flex: 1;
  305. }
  306. tr {
  307. display: grid;
  308. }
  309. `}
  310. `;
  311. const DrawerButton = styled(Button)<{active: boolean}>`
  312. border: none;
  313. background-color: transparent;
  314. box-shadow: none;
  315. transition: none !important;
  316. opacity: ${p => (p.active ? 0.7 : 0.5)};
  317. &:not(:last-child) {
  318. margin-right: ${space(1)};
  319. }
  320. &:hover {
  321. border: none;
  322. background-color: transparent;
  323. box-shadow: none;
  324. opacity: ${p => (p.active ? 0.6 : 0.5)};
  325. }
  326. `;
  327. export default TraceDrawer;