traceDrawer.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. import {useCallback, useMemo, useRef} from 'react';
  2. import {type Theme, useTheme} from '@emotion/react';
  3. import styled from '@emotion/styled';
  4. import type {Location} from 'history';
  5. import {Button} from 'sentry/components/button';
  6. import {IconPanel, IconPin} from 'sentry/icons';
  7. import {t} from 'sentry/locale';
  8. import {space} from 'sentry/styles/space';
  9. import type {EventTransaction, Organization} from 'sentry/types';
  10. import type EventView from 'sentry/utils/discover/eventView';
  11. import type {
  12. TraceFullDetailed,
  13. TraceSplitResults,
  14. } from 'sentry/utils/performance/quickTrace/types';
  15. import type {UseApiQueryResult} from 'sentry/utils/queryClient';
  16. import type RequestError from 'sentry/utils/requestError/requestError';
  17. import {
  18. useResizableDrawer,
  19. type UseResizableDrawerOptions,
  20. } from 'sentry/utils/useResizableDrawer';
  21. import {
  22. getTraceTabTitle,
  23. type TraceTabsReducerAction,
  24. type TraceTabsReducerState,
  25. } from 'sentry/views/performance/newTraceDetails/traceTabs';
  26. import type {VirtualizedViewManager} from 'sentry/views/performance/newTraceDetails/virtualizedViewManager';
  27. import {makeTraceNodeBarColor, type TraceTree, type TraceTreeNode} from '../traceTree';
  28. import NodeDetail from './tabs/details';
  29. import {TraceLevelDetails} from './tabs/trace';
  30. const MIN_TRACE_DRAWER_DIMENSTIONS: [number, number] = [480, 30];
  31. type TraceDrawerProps = {
  32. drawerSize: number;
  33. layout: 'drawer bottom' | 'drawer left' | 'drawer right';
  34. location: Location;
  35. manager: VirtualizedViewManager;
  36. onDrawerResize: (size: number) => void;
  37. onLayoutChange: (layout: 'drawer bottom' | 'drawer left' | 'drawer right') => void;
  38. organization: Organization;
  39. rootEventResults: UseApiQueryResult<EventTransaction, RequestError>;
  40. scrollToNode: (node: TraceTreeNode<TraceTree.NodeValue>) => void;
  41. tabs: TraceTabsReducerState;
  42. tabsDispatch: React.Dispatch<TraceTabsReducerAction>;
  43. trace: TraceTree;
  44. traceEventView: EventView;
  45. traces: TraceSplitResults<TraceFullDetailed> | null;
  46. };
  47. function TraceDrawer(props: TraceDrawerProps) {
  48. const theme = useTheme();
  49. const panelRef = useRef<HTMLDivElement>(null);
  50. const onDrawerResize = props.onDrawerResize;
  51. const resizableDrawerOptions: UseResizableDrawerOptions = useMemo(() => {
  52. const isSidebarLayout =
  53. props.layout === 'drawer left' || props.layout === 'drawer right';
  54. const initialSize =
  55. props.drawerSize > 0
  56. ? props.drawerSize
  57. : isSidebarLayout
  58. ? // Half the screen minus the ~sidebar width
  59. Math.max(window.innerWidth * 0.5 - 220, MIN_TRACE_DRAWER_DIMENSTIONS[0])
  60. : // 30% of the screen height
  61. Math.max(window.innerHeight * 0.3);
  62. const min = isSidebarLayout ? window.innerWidth * 0.2 : 30;
  63. function onResize(newSize: number) {
  64. onDrawerResize(newSize);
  65. if (!panelRef.current) {
  66. return;
  67. }
  68. if (isSidebarLayout) {
  69. panelRef.current.style.width = `${newSize}px`;
  70. panelRef.current.style.height = `100%`;
  71. } else {
  72. panelRef.current.style.height = `${newSize}px`;
  73. panelRef.current.style.width = `100%`;
  74. }
  75. // @TODO This can visual delays as the rest of the view uses a resize observer
  76. // to adjust the layout. We should force a sync layout update + draw here to fix that.
  77. }
  78. return {
  79. initialSize,
  80. onResize,
  81. direction:
  82. props.layout === 'drawer left'
  83. ? 'left'
  84. : props.layout === 'drawer right'
  85. ? 'right'
  86. : 'up',
  87. min,
  88. };
  89. }, [props.layout, onDrawerResize, props.drawerSize]);
  90. const {onMouseDown} = useResizableDrawer(resizableDrawerOptions);
  91. const onParentClick = useCallback(
  92. (node: TraceTreeNode<TraceTree.NodeValue>) => {
  93. props.scrollToNode(node);
  94. props.tabsDispatch({
  95. type: 'activate tab',
  96. payload: node,
  97. pin_previous: true,
  98. });
  99. },
  100. [props]
  101. );
  102. return (
  103. <PanelWrapper layout={props.layout} ref={panelRef}>
  104. <ResizeableHandle layout={props.layout} onMouseDown={onMouseDown} />
  105. <TabsLayout
  106. hasIndicators={
  107. // Syncs the height of the tabs with the trace indicators
  108. props.trace.indicators.length > 0 && props.layout !== 'drawer bottom'
  109. }
  110. >
  111. <TabsContainer
  112. style={{
  113. gridTemplateColumns: `repeat(${props.tabs.tabs.length + (props.tabs.last_clicked ? 1 : 0)}, minmax(0, min-content))`,
  114. }}
  115. >
  116. {props.tabs.tabs.map((n, i) => {
  117. return (
  118. <TraceDrawerTab
  119. key={i}
  120. tab={n}
  121. index={i}
  122. theme={theme}
  123. tabs={props.tabs}
  124. tabsDispatch={props.tabsDispatch}
  125. scrollToNode={props.scrollToNode}
  126. trace={props.trace}
  127. pinned
  128. />
  129. );
  130. })}
  131. {props.tabs.last_clicked ? (
  132. <TraceDrawerTab
  133. pinned={false}
  134. key="last-clicked"
  135. tab={props.tabs.last_clicked}
  136. index={props.tabs.tabs.length}
  137. theme={theme}
  138. tabs={props.tabs}
  139. tabsDispatch={props.tabsDispatch}
  140. scrollToNode={props.scrollToNode}
  141. trace={props.trace}
  142. />
  143. ) : null}
  144. </TabsContainer>
  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. </TabsLayout>
  178. <Content layout={props.layout}>
  179. <ContentWrapper>
  180. {props.tabs.current ? (
  181. props.tabs.current.node === 'Trace' ? (
  182. <TraceLevelDetails
  183. node={props.trace.root.children[0]}
  184. tree={props.trace}
  185. rootEventResults={props.rootEventResults}
  186. organization={props.organization}
  187. location={props.location}
  188. traces={props.traces}
  189. traceEventView={props.traceEventView}
  190. />
  191. ) : (
  192. <NodeDetail
  193. node={props.tabs.current.node}
  194. organization={props.organization}
  195. location={props.location}
  196. manager={props.manager}
  197. scrollToNode={props.scrollToNode}
  198. onParentClick={onParentClick}
  199. />
  200. )
  201. ) : null}
  202. </ContentWrapper>
  203. </Content>
  204. </PanelWrapper>
  205. );
  206. }
  207. interface TraceDrawerTabProps {
  208. index: number;
  209. pinned: boolean;
  210. scrollToNode: (node: TraceTreeNode<TraceTree.NodeValue>) => void;
  211. tab: TraceTabsReducerState['tabs'][number];
  212. tabs: TraceTabsReducerState;
  213. tabsDispatch: React.Dispatch<TraceTabsReducerAction>;
  214. theme: Theme;
  215. trace: TraceTree;
  216. }
  217. function TraceDrawerTab(props: TraceDrawerTabProps) {
  218. const node = props.tab.node;
  219. if (typeof node === 'string') {
  220. const root = props.trace.root.children[0];
  221. return (
  222. <Tab
  223. active={props.tab === props.tabs.current}
  224. onClick={() => {
  225. props.scrollToNode(root);
  226. props.tabsDispatch({type: 'activate tab', payload: props.index});
  227. }}
  228. >
  229. {/* A trace is technically an entry in the list, so it has a color */}
  230. {props.tab.node === 'Trace' ? null : (
  231. <TabButtonIndicator
  232. backgroundColor={makeTraceNodeBarColor(props.theme, root)}
  233. />
  234. )}
  235. <TabButton>{node}</TabButton>
  236. </Tab>
  237. );
  238. }
  239. return (
  240. <Tab
  241. active={props.tab === props.tabs.current}
  242. onClick={() => {
  243. props.scrollToNode(node);
  244. props.tabsDispatch({type: 'activate tab', payload: props.index});
  245. }}
  246. >
  247. <TabButtonIndicator backgroundColor={makeTraceNodeBarColor(props.theme, node)} />
  248. <TabButton>{getTraceTabTitle(node)}</TabButton>
  249. <TabPinButton
  250. pinned={props.pinned}
  251. onClick={e => {
  252. e.stopPropagation();
  253. props.pinned
  254. ? props.tabsDispatch({type: 'unpin tab', payload: props.index})
  255. : props.tabsDispatch({type: 'pin tab'});
  256. }}
  257. />
  258. </Tab>
  259. );
  260. }
  261. const ResizeableHandle = styled('div')<{
  262. layout: 'drawer bottom' | 'drawer left' | 'drawer right';
  263. }>`
  264. width: ${p => (p.layout === 'drawer bottom' ? '100%' : '12px')};
  265. height: ${p => (p.layout === 'drawer bottom' ? '12px' : '100%')};
  266. cursor: ${p => (p.layout === 'drawer bottom' ? 'ns-resize' : 'ew-resize')};
  267. position: absolute;
  268. top: ${p => (p.layout === 'drawer bottom' ? '-6px' : 0)};
  269. left: ${p =>
  270. p.layout === 'drawer bottom' ? 0 : p.layout === 'drawer right' ? '-6px' : 'initial'};
  271. right: ${p => (p.layout === 'drawer left' ? '-6px' : 0)};
  272. z-index: 1;
  273. `;
  274. const PanelWrapper = styled('div')<{
  275. layout: 'drawer bottom' | 'drawer left' | 'drawer right';
  276. }>`
  277. grid-area: drawer;
  278. display: flex;
  279. flex-direction: column;
  280. overflow: hidden;
  281. width: 100%;
  282. border-top: ${p =>
  283. p.layout === 'drawer bottom' ? `1px solid ${p.theme.border}` : 'none'};
  284. border-left: ${p =>
  285. p.layout === 'drawer right' ? `1px solid ${p.theme.border}` : 'none'};
  286. border-right: ${p =>
  287. p.layout === 'drawer left' ? `1px solid ${p.theme.border}` : 'none'};
  288. bottom: 0;
  289. right: 0;
  290. position: relative;
  291. background: ${p => p.theme.background};
  292. color: ${p => p.theme.textColor};
  293. text-align: left;
  294. z-index: 10;
  295. `;
  296. const TabsLayout = styled('div')<{hasIndicators: boolean}>`
  297. display: grid;
  298. grid-template-columns: 1fr auto;
  299. border-bottom: 1px solid ${p => p.theme.border};
  300. background-color: ${p => p.theme.backgroundSecondary};
  301. height: ${p => (p.hasIndicators ? '44px' : '26px')};
  302. `;
  303. const TabsContainer = styled('ul')`
  304. display: grid;
  305. list-style-type: none;
  306. width: 100%;
  307. align-items: center;
  308. justify-content: left;
  309. padding-left: ${space(1)};
  310. gap: ${space(1)};
  311. margin-bottom: 0;
  312. `;
  313. const TabLayoutControlsContainer = styled('ul')`
  314. list-style-type: none;
  315. padding-left: 0;
  316. margin-bottom: 0;
  317. flex: none;
  318. button {
  319. padding: 0 ${space(0.5)};
  320. }
  321. `;
  322. const TabLayoutControlItem = styled('li')`
  323. display: inline-block;
  324. margin: 0;
  325. `;
  326. const Tab = styled('li')<{active: boolean}>`
  327. height: 100%;
  328. border-top: 2px solid transparent;
  329. display: flex;
  330. align-items: center;
  331. border-bottom: 2px solid ${p => (p.active ? p.theme.blue400 : 'transparent')};
  332. padding: 0 ${space(0.25)};
  333. &:hover {
  334. border-bottom: 2px solid ${p => (p.active ? p.theme.blue400 : p.theme.blue200)};
  335. button:last-child {
  336. transition: all 0.3s ease-in-out 500ms;
  337. transform: scale(1);
  338. opacity: 1;
  339. }
  340. }
  341. `;
  342. const TabButtonIndicator = styled('div')<{backgroundColor: string}>`
  343. width: 12px;
  344. height: 12px;
  345. min-width: 12px;
  346. border-radius: 2px;
  347. background-color: ${p => p.backgroundColor};
  348. `;
  349. const TabButton = styled('button')`
  350. height: 100%;
  351. border: none;
  352. max-width: 66ch;
  353. overflow: hidden;
  354. text-overflow: ellipsis;
  355. white-space: nowrap;
  356. border-radius: 0;
  357. margin: 0;
  358. font-size: ${p => p.theme.fontSizeSmall};
  359. color: ${p => p.theme.textColor};
  360. background: transparent;
  361. `;
  362. const Content = styled('div')<{layout: 'drawer bottom' | 'drawer left' | 'drawer right'}>`
  363. position: relative;
  364. overflow: auto;
  365. padding: ${space(1)};
  366. flex: 1;
  367. td {
  368. max-width: 100% !important;
  369. }
  370. ${p =>
  371. p.layout !== 'drawer bottom' &&
  372. `
  373. table {
  374. display: flex;
  375. }
  376. tbody {
  377. flex: 1;
  378. }
  379. tr {
  380. display: grid;
  381. }
  382. `}
  383. `;
  384. const DrawerButton = styled(Button)<{active: boolean}>`
  385. border: none;
  386. background-color: transparent;
  387. box-shadow: none;
  388. transition: none !important;
  389. opacity: ${p => (p.active ? 0.7 : 0.5)};
  390. &:not(:last-child) {
  391. margin-right: ${space(1)};
  392. }
  393. &:hover {
  394. border: none;
  395. background-color: transparent;
  396. box-shadow: none;
  397. opacity: ${p => (p.active ? 0.6 : 0.5)};
  398. }
  399. `;
  400. function TabPinButton(props: {
  401. pinned: boolean;
  402. onClick?: (e: React.MouseEvent<HTMLElement>) => void;
  403. }) {
  404. return (
  405. <PinButton size="zero" onClick={props.onClick}>
  406. <StyledIconPin size="xs" isSolid={props.pinned} />
  407. </PinButton>
  408. );
  409. }
  410. const PinButton = styled(Button)`
  411. padding: ${space(0.5)};
  412. margin: 0;
  413. background-color: transparent;
  414. border: none;
  415. &:hover {
  416. background-color: transparent;
  417. }
  418. `;
  419. const StyledIconPin = styled(IconPin)`
  420. background-color: transparent;
  421. border: none;
  422. `;
  423. const ContentWrapper = styled('div')`
  424. inset: ${space(1)};
  425. position: absolute;
  426. `;
  427. export default TraceDrawer;