traceDrawer.tsx 17 KB

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