traceDrawer.tsx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. import {useCallback, useMemo, useRef, useState} from 'react';
  2. import {type Theme, useTheme} from '@emotion/react';
  3. import styled from '@emotion/styled';
  4. import {Button} from 'sentry/components/button';
  5. import {IconChevron, IconPanel, IconPin} 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 {TraceVitals} from 'sentry/views/performance/newTraceDetails/traceDrawer/tabs/traceVitals';
  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 {TraceDetails} from './tabs/trace';
  29. import {TraceTreeNodeDetails} from './tabs/traceTreeNodeDetails';
  30. const MIN_TRACE_DRAWER_DIMENSTIONS: [number, number] = [480, 27];
  31. type TraceDrawerProps = {
  32. drawerSize: number;
  33. layout: 'drawer bottom' | 'drawer left' | 'drawer right';
  34. manager: VirtualizedViewManager;
  35. onDrawerResize: (size: number) => void;
  36. onLayoutChange: (layout: 'drawer bottom' | 'drawer left' | 'drawer right') => void;
  37. organization: Organization;
  38. rootEventResults: UseApiQueryResult<EventTransaction, RequestError>;
  39. scrollToNode: (node: TraceTreeNode<TraceTree.NodeValue>) => void;
  40. tabs: TraceTabsReducerState;
  41. tabsDispatch: React.Dispatch<TraceTabsReducerAction>;
  42. trace: TraceTree;
  43. traceEventView: EventView;
  44. traces: TraceSplitResults<TraceFullDetailed> | null;
  45. };
  46. function getUninitializedDrawerSize(layout: TraceDrawerProps['layout']): number {
  47. const uninitialized =
  48. layout === 'drawer bottom'
  49. ? // 36 of the screen height
  50. Math.max(window.innerHeight * 0.36)
  51. : // Half the screen minus the ~sidebar width
  52. Math.max(window.innerWidth * 0.5 - 220, MIN_TRACE_DRAWER_DIMENSTIONS[0]);
  53. return uninitialized;
  54. }
  55. function getDrawerInitialSize(
  56. layout: TraceDrawerProps['layout'],
  57. drawerSize: number
  58. ): number {
  59. return drawerSize > 0 ? drawerSize : getUninitializedDrawerSize(layout);
  60. }
  61. function getDrawerMinSize(layout: TraceDrawerProps['layout']): number {
  62. return layout === 'drawer left' || layout === 'drawer right'
  63. ? MIN_TRACE_DRAWER_DIMENSTIONS[0]
  64. : MIN_TRACE_DRAWER_DIMENSTIONS[1];
  65. }
  66. const LAYOUT_STORAGE: Partial<Record<TraceDrawerProps['layout'], number>> = {};
  67. export function TraceDrawer(props: TraceDrawerProps) {
  68. const theme = useTheme();
  69. const panelRef = useRef<HTMLDivElement | null>(null);
  70. const [minimized, setMinimized] = useState(
  71. Math.round(props.drawerSize) <= getDrawerMinSize(props.layout)
  72. );
  73. const minimizedRef = useRef(minimized);
  74. minimizedRef.current = minimized;
  75. const lastNonMinimizedSizeRef =
  76. useRef<Partial<Record<TraceDrawerProps['layout'], number>>>(LAYOUT_STORAGE);
  77. const lastLayoutRef = useRef<TraceDrawerProps['layout']>(props.layout);
  78. const onDrawerResize = props.onDrawerResize;
  79. const onResize = useCallback(
  80. (newSize: number, _oldSize: number | undefined, userEvent: boolean) => {
  81. const min = getDrawerMinSize(props.layout);
  82. // Round to nearest pixel value
  83. newSize = Math.round(newSize);
  84. if (userEvent) {
  85. lastNonMinimizedSizeRef.current[props.layout] = newSize;
  86. // Track the value to see if the user manually minimized or expanded the drawer
  87. if (!minimizedRef.current && newSize <= min) {
  88. // Only auto collapse when drawer is pinned to bottom
  89. if (props.layout === 'drawer bottom') {
  90. setMinimized(true);
  91. }
  92. } else if (minimizedRef.current && newSize > min) {
  93. setMinimized(false);
  94. }
  95. }
  96. if (minimizedRef.current) {
  97. newSize = min;
  98. }
  99. onDrawerResize(newSize);
  100. lastLayoutRef.current = props.layout;
  101. if (!panelRef.current) {
  102. return;
  103. }
  104. if (props.layout === 'drawer left' || props.layout === 'drawer right') {
  105. panelRef.current.style.width = `${newSize}px`;
  106. panelRef.current.style.height = `100%`;
  107. } else {
  108. panelRef.current.style.height = `${newSize}px`;
  109. panelRef.current.style.width = `100%`;
  110. }
  111. // @TODO This can visual delays as the rest of the view uses a resize observer
  112. // to adjust the layout. We should force a sync layout update + draw here to fix that.
  113. },
  114. [onDrawerResize, props.layout]
  115. );
  116. const resizableDrawerOptions: UseResizableDrawerOptions = useMemo(() => {
  117. return {
  118. initialSize:
  119. lastNonMinimizedSizeRef[props.layout] ??
  120. getDrawerInitialSize(props.layout, props.drawerSize),
  121. min: getDrawerMinSize(props.layout),
  122. onResize,
  123. direction:
  124. props.layout === 'drawer left'
  125. ? 'left'
  126. : props.layout === 'drawer right'
  127. ? 'right'
  128. : 'up',
  129. };
  130. }, [props.layout, onResize, props.drawerSize]);
  131. const {onMouseDown, size, setSize} = useResizableDrawer(resizableDrawerOptions);
  132. const onMinimize = useCallback(
  133. (value: boolean) => {
  134. minimizedRef.current = value;
  135. setMinimized(value);
  136. if (!value) {
  137. const lastUserSize = lastNonMinimizedSizeRef.current[props.layout];
  138. const min = getDrawerMinSize(props.layout);
  139. // If the user has minimized the drawer to the minimum size, we should
  140. // restore the drawer to the initial size instead of the last user size.
  141. if (lastUserSize === undefined || lastUserSize <= min) {
  142. setSize(getUninitializedDrawerSize(props.layout), true);
  143. return;
  144. }
  145. setSize(lastUserSize, false);
  146. return;
  147. }
  148. setSize(
  149. props.layout === 'drawer bottom'
  150. ? MIN_TRACE_DRAWER_DIMENSTIONS[1]
  151. : MIN_TRACE_DRAWER_DIMENSTIONS[0],
  152. false
  153. );
  154. },
  155. [props.layout, setSize]
  156. );
  157. const onParentClick = useCallback(
  158. (node: TraceTreeNode<TraceTree.NodeValue>) => {
  159. props.scrollToNode(node);
  160. props.tabsDispatch({
  161. type: 'activate tab',
  162. payload: node,
  163. pin_previous: true,
  164. });
  165. },
  166. [props]
  167. );
  168. if (minimized && (props.layout === 'drawer left' || props.layout === 'drawer right')) {
  169. return (
  170. <TabsHeightContainer
  171. absolute
  172. hasIndicators={
  173. // Syncs the height of the tabs with the trace indicators
  174. props.trace.indicators.length > 0
  175. }
  176. >
  177. <TabLayoutControlItem>
  178. <TabIconButton
  179. size="xs"
  180. active={minimized}
  181. onClick={() => onMinimize(!minimized)}
  182. aria-label={t('Minimize')}
  183. icon={
  184. <SmallerChevronIcon
  185. size="sm"
  186. isCircled
  187. direction={
  188. props.layout === 'drawer left'
  189. ? minimized
  190. ? 'right'
  191. : 'left'
  192. : minimized
  193. ? 'left'
  194. : 'right'
  195. }
  196. />
  197. }
  198. />
  199. </TabLayoutControlItem>
  200. </TabsHeightContainer>
  201. );
  202. }
  203. return (
  204. <PanelWrapper
  205. layout={props.layout}
  206. ref={r => {
  207. panelRef.current = r;
  208. if (r) {
  209. if (props.layout === 'drawer left' || props.layout === 'drawer right') {
  210. r.style.width = `${size}px`;
  211. r.style.height = `100%`;
  212. } else {
  213. r.style.height = `${size}px`;
  214. r.style.width = `100%`;
  215. }
  216. }
  217. }}
  218. >
  219. <ResizeableHandle layout={props.layout} onMouseDown={onMouseDown} />
  220. <TabsHeightContainer
  221. hasIndicators={
  222. // Syncs the height of the tabs with the trace indicators
  223. props.trace.indicators.length > 0 && props.layout !== 'drawer bottom'
  224. }
  225. >
  226. <TabsLayout>
  227. <TabActions>
  228. <TabLayoutControlItem>
  229. <TabIconButton
  230. size="xs"
  231. active={minimized}
  232. onClick={() => onMinimize(!minimized)}
  233. aria-label={t('Minimize')}
  234. icon={
  235. <SmallerChevronIcon
  236. size="sm"
  237. isCircled
  238. direction={
  239. props.layout === 'drawer bottom'
  240. ? minimized
  241. ? 'up'
  242. : 'down'
  243. : props.layout === 'drawer left'
  244. ? minimized
  245. ? 'right'
  246. : 'left'
  247. : minimized
  248. ? 'left'
  249. : 'right'
  250. }
  251. />
  252. }
  253. />
  254. </TabLayoutControlItem>
  255. </TabActions>
  256. <TabsContainer
  257. style={{
  258. gridTemplateColumns: `repeat(${props.tabs.tabs.length + (props.tabs.last_clicked ? 1 : 0)}, minmax(0, min-content))`,
  259. }}
  260. >
  261. {props.tabs.tabs.map((n, i) => {
  262. return (
  263. <TraceDrawerTab
  264. key={i}
  265. tab={n}
  266. index={i}
  267. theme={theme}
  268. tabs={props.tabs}
  269. tabsDispatch={props.tabsDispatch}
  270. scrollToNode={props.scrollToNode}
  271. trace={props.trace}
  272. pinned
  273. />
  274. );
  275. })}
  276. {props.tabs.last_clicked ? (
  277. <TraceDrawerTab
  278. pinned={false}
  279. key="last-clicked"
  280. tab={props.tabs.last_clicked}
  281. index={props.tabs.tabs.length}
  282. theme={theme}
  283. tabs={props.tabs}
  284. tabsDispatch={props.tabsDispatch}
  285. scrollToNode={props.scrollToNode}
  286. trace={props.trace}
  287. />
  288. ) : null}
  289. </TabsContainer>
  290. <TabActions>
  291. <TabLayoutControlItem>
  292. <TabIconButton
  293. active={props.layout === 'drawer left'}
  294. onClick={() => props.onLayoutChange('drawer left')}
  295. size="xs"
  296. aria-label={t('Drawer left')}
  297. icon={<IconPanel size="xs" direction="left" />}
  298. />
  299. </TabLayoutControlItem>
  300. <TabLayoutControlItem>
  301. <TabIconButton
  302. active={props.layout === 'drawer bottom'}
  303. onClick={() => props.onLayoutChange('drawer bottom')}
  304. size="xs"
  305. aria-label={t('Drawer bottom')}
  306. icon={<IconPanel size="xs" direction="down" />}
  307. />
  308. </TabLayoutControlItem>
  309. <TabLayoutControlItem>
  310. <TabIconButton
  311. active={props.layout === 'drawer right'}
  312. onClick={() => props.onLayoutChange('drawer right')}
  313. size="xs"
  314. aria-label={t('Drawer right')}
  315. icon={<IconPanel size="xs" direction="right" />}
  316. />
  317. </TabLayoutControlItem>
  318. </TabActions>
  319. </TabsLayout>
  320. </TabsHeightContainer>
  321. <Content layout={props.layout}>
  322. <ContentWrapper>
  323. {props.tabs.current ? (
  324. props.tabs.current.node === 'trace' ? (
  325. <TraceDetails
  326. tree={props.trace}
  327. node={props.trace.root.children[0]}
  328. rootEventResults={props.rootEventResults}
  329. organization={props.organization}
  330. traces={props.traces}
  331. traceEventView={props.traceEventView}
  332. />
  333. ) : props.tabs.current.node === 'vitals' ? (
  334. <TraceVitals trace={props.trace} />
  335. ) : (
  336. <TraceTreeNodeDetails
  337. node={props.tabs.current.node}
  338. organization={props.organization}
  339. manager={props.manager}
  340. scrollToNode={props.scrollToNode}
  341. onParentClick={onParentClick}
  342. />
  343. )
  344. ) : null}
  345. </ContentWrapper>
  346. </Content>
  347. </PanelWrapper>
  348. );
  349. }
  350. interface TraceDrawerTabProps {
  351. index: number;
  352. pinned: boolean;
  353. scrollToNode: (node: TraceTreeNode<TraceTree.NodeValue>) => void;
  354. tab: TraceTabsReducerState['tabs'][number];
  355. tabs: TraceTabsReducerState;
  356. tabsDispatch: React.Dispatch<TraceTabsReducerAction>;
  357. theme: Theme;
  358. trace: TraceTree;
  359. }
  360. function TraceDrawerTab(props: TraceDrawerTabProps) {
  361. const node = props.tab.node;
  362. if (typeof node === 'string') {
  363. const root = props.trace.root.children[0];
  364. return (
  365. <Tab
  366. className={typeof props.tab.node === 'string' ? 'Static' : ''}
  367. active={props.tab === props.tabs.current}
  368. onClick={() => {
  369. if (props.tab.node !== 'vitals') {
  370. props.scrollToNode(root);
  371. }
  372. props.tabsDispatch({type: 'activate tab', payload: props.index});
  373. }}
  374. >
  375. {/* A trace is technically an entry in the list, so it has a color */}
  376. {props.tab.node === 'trace' ? null : (
  377. <TabButtonIndicator
  378. backgroundColor={makeTraceNodeBarColor(props.theme, root)}
  379. />
  380. )}
  381. <TabButton>{props.tab.label ?? props.tab.node}</TabButton>
  382. </Tab>
  383. );
  384. }
  385. return (
  386. <Tab
  387. active={props.tab === props.tabs.current}
  388. onClick={() => {
  389. props.scrollToNode(node);
  390. props.tabsDispatch({type: 'activate tab', payload: props.index});
  391. }}
  392. >
  393. <TabButtonIndicator backgroundColor={makeTraceNodeBarColor(props.theme, node)} />
  394. <TabButton>{getTraceTabTitle(node)}</TabButton>
  395. <TabPinButton
  396. pinned={props.pinned}
  397. onClick={e => {
  398. e.stopPropagation();
  399. props.pinned
  400. ? props.tabsDispatch({type: 'unpin tab', payload: props.index})
  401. : props.tabsDispatch({type: 'pin tab'});
  402. }}
  403. />
  404. </Tab>
  405. );
  406. }
  407. const ResizeableHandle = styled('div')<{
  408. layout: 'drawer bottom' | 'drawer left' | 'drawer right';
  409. }>`
  410. width: ${p => (p.layout === 'drawer bottom' ? '100%' : '12px')};
  411. height: ${p => (p.layout === 'drawer bottom' ? '12px' : '100%')};
  412. cursor: ${p => (p.layout === 'drawer bottom' ? 'ns-resize' : 'ew-resize')};
  413. position: absolute;
  414. top: ${p => (p.layout === 'drawer bottom' ? '-6px' : 0)};
  415. left: ${p =>
  416. p.layout === 'drawer bottom' ? 0 : p.layout === 'drawer right' ? '-6px' : 'initial'};
  417. right: ${p => (p.layout === 'drawer left' ? '-6px' : 0)};
  418. z-index: 1;
  419. `;
  420. const PanelWrapper = styled('div')<{
  421. layout: 'drawer bottom' | 'drawer left' | 'drawer right';
  422. }>`
  423. grid-area: drawer;
  424. display: flex;
  425. flex-direction: column;
  426. overflow: hidden;
  427. width: 100%;
  428. border-top: ${p =>
  429. p.layout === 'drawer bottom' ? `1px solid ${p.theme.border}` : 'none'};
  430. border-left: ${p =>
  431. p.layout === 'drawer right' ? `1px solid ${p.theme.border}` : 'none'};
  432. border-right: ${p =>
  433. p.layout === 'drawer left' ? `1px solid ${p.theme.border}` : 'none'};
  434. bottom: 0;
  435. right: 0;
  436. position: relative;
  437. background: ${p => p.theme.background};
  438. color: ${p => p.theme.textColor};
  439. text-align: left;
  440. z-index: 10;
  441. `;
  442. const SmallerChevronIcon = styled(IconChevron)`
  443. width: 13px;
  444. height: 13px;
  445. transition: none;
  446. `;
  447. const TabsHeightContainer = styled('div')<{hasIndicators: boolean; absolute?: boolean}>`
  448. position: ${p => (p.absolute ? 'absolute' : 'relative')};
  449. height: ${p => (p.hasIndicators ? '44px' : '26px')};
  450. border-bottom: 1px solid ${p => p.theme.border};
  451. background-color: ${p => p.theme.backgroundSecondary};
  452. display: flex;
  453. flex-direction: column;
  454. justify-content: end;
  455. `;
  456. const TabsLayout = styled('div')`
  457. display: grid;
  458. grid-template-columns: auto 1fr auto;
  459. padding-left: ${space(0.25)};
  460. padding-right: ${space(0.5)};
  461. `;
  462. const TabsContainer = styled('ul')`
  463. display: grid;
  464. list-style-type: none;
  465. width: 100%;
  466. align-items: center;
  467. justify-content: left;
  468. gap: ${space(0.5)};
  469. padding-left: 0;
  470. margin-bottom: 0;
  471. `;
  472. const TabActions = styled('ul')`
  473. list-style-type: none;
  474. padding-left: 0;
  475. margin-bottom: 0;
  476. flex: none;
  477. button {
  478. padding: 0 ${space(0.5)};
  479. }
  480. `;
  481. const TabLayoutControlItem = styled('li')`
  482. display: inline-block;
  483. margin: 0;
  484. `;
  485. const Tab = styled('li')<{active: boolean}>`
  486. height: 100%;
  487. border-top: 2px solid transparent;
  488. display: flex;
  489. align-items: center;
  490. border-bottom: 2px solid ${p => (p.active ? p.theme.blue400 : 'transparent')};
  491. padding: 0 ${space(0.25)};
  492. position: relative;
  493. &.Static + li:not(.Static) {
  494. margin-left: ${space(2)};
  495. &:after {
  496. display: block;
  497. content: '';
  498. position: absolute;
  499. left: -14px;
  500. top: 50%;
  501. transform: translateY(-50%);
  502. height: 72%;
  503. width: 1px;
  504. background-color: ${p => p.theme.border};
  505. }
  506. }
  507. &:hover {
  508. border-bottom: 2px solid ${p => (p.active ? p.theme.blue400 : p.theme.blue200)};
  509. button:last-child {
  510. transition: all 0.3s ease-in-out 500ms;
  511. transform: scale(1);
  512. opacity: 1;
  513. }
  514. }
  515. `;
  516. const TabButtonIndicator = styled('div')<{backgroundColor: string}>`
  517. width: 12px;
  518. height: 12px;
  519. min-width: 12px;
  520. border-radius: 2px;
  521. margin-right: ${space(0.25)};
  522. background-color: ${p => p.backgroundColor};
  523. `;
  524. const TabButton = styled('button')`
  525. height: 100%;
  526. border: none;
  527. max-width: 66ch;
  528. overflow: hidden;
  529. text-overflow: ellipsis;
  530. white-space: nowrap;
  531. border-radius: 0;
  532. margin: 0;
  533. padding: 0 ${space(0.25)};
  534. font-size: ${p => p.theme.fontSizeSmall};
  535. color: ${p => p.theme.textColor};
  536. background: transparent;
  537. `;
  538. const Content = styled('div')<{layout: 'drawer bottom' | 'drawer left' | 'drawer right'}>`
  539. position: relative;
  540. overflow: auto;
  541. padding: ${space(1)};
  542. flex: 1;
  543. td {
  544. max-width: 100% !important;
  545. }
  546. ${p =>
  547. p.layout !== 'drawer bottom' &&
  548. `
  549. table {
  550. display: flex;
  551. }
  552. tbody {
  553. flex: 1;
  554. }
  555. tr {
  556. display: grid;
  557. }
  558. `}
  559. `;
  560. const TabIconButton = styled(Button)<{active: boolean}>`
  561. border: none;
  562. background-color: transparent;
  563. box-shadow: none;
  564. transition: none !important;
  565. opacity: ${p => (p.active ? 0.7 : 0.5)};
  566. &:not(:last-child) {
  567. margin-right: ${space(1)};
  568. }
  569. &:hover {
  570. border: none;
  571. background-color: transparent;
  572. box-shadow: none;
  573. opacity: ${p => (p.active ? 0.6 : 0.5)};
  574. }
  575. `;
  576. function TabPinButton(props: {
  577. pinned: boolean;
  578. onClick?: (e: React.MouseEvent<HTMLElement>) => void;
  579. }) {
  580. return (
  581. <PinButton size="zero" onClick={props.onClick}>
  582. <StyledIconPin size="xs" isSolid={props.pinned} />
  583. </PinButton>
  584. );
  585. }
  586. const PinButton = styled(Button)`
  587. padding: ${space(0.5)};
  588. margin: 0;
  589. background-color: transparent;
  590. border: none;
  591. &:hover {
  592. background-color: transparent;
  593. }
  594. `;
  595. const StyledIconPin = styled(IconPin)`
  596. background-color: transparent;
  597. border: none;
  598. `;
  599. const ContentWrapper = styled('div')`
  600. inset: ${space(1)};
  601. position: absolute;
  602. `;