traceDrawer.tsx 18 KB

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