traceDrawer.tsx 17 KB

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