traceDrawer.tsx 18 KB

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