index.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. import {useCallback, useMemo, useRef, useState} from 'react';
  2. import {AutoSizer, CellMeasurer, GridCellProps, MultiGrid} from 'react-virtualized';
  3. import styled from '@emotion/styled';
  4. import {Button} from 'sentry/components/button';
  5. import Placeholder from 'sentry/components/placeholder';
  6. import {useReplayContext} from 'sentry/components/replays/replayContext';
  7. import {t} from 'sentry/locale';
  8. import {trackAnalytics} from 'sentry/utils/analytics';
  9. import {getNextReplayFrame} from 'sentry/utils/replays/getReplayEvent';
  10. import useCrumbHandlers from 'sentry/utils/replays/hooks/useCrumbHandlers';
  11. import {getFrameMethod, getFrameStatus} from 'sentry/utils/replays/resourceFrame';
  12. import useOrganization from 'sentry/utils/useOrganization';
  13. import {useResizableDrawer} from 'sentry/utils/useResizableDrawer';
  14. import useUrlParams from 'sentry/utils/useUrlParams';
  15. import FluidHeight from 'sentry/views/replays/detail/layout/fluidHeight';
  16. import NetworkDetails from 'sentry/views/replays/detail/network/details';
  17. import {ReqRespBodiesAlert} from 'sentry/views/replays/detail/network/details/onboarding';
  18. import NetworkFilters from 'sentry/views/replays/detail/network/networkFilters';
  19. import NetworkHeaderCell, {
  20. COLUMN_COUNT,
  21. } from 'sentry/views/replays/detail/network/networkHeaderCell';
  22. import NetworkTableCell from 'sentry/views/replays/detail/network/networkTableCell';
  23. import useNetworkFilters from 'sentry/views/replays/detail/network/useNetworkFilters';
  24. import useSortNetwork from 'sentry/views/replays/detail/network/useSortNetwork';
  25. import NoRowRenderer from 'sentry/views/replays/detail/noRowRenderer';
  26. import useVirtualizedGrid from 'sentry/views/replays/detail/useVirtualizedGrid';
  27. const HEADER_HEIGHT = 25;
  28. const BODY_HEIGHT = 28;
  29. const RESIZEABLE_HANDLE_HEIGHT = 90;
  30. const cellMeasurer = {
  31. defaultHeight: BODY_HEIGHT,
  32. defaultWidth: 100,
  33. fixedHeight: true,
  34. };
  35. function NetworkList() {
  36. const organization = useOrganization();
  37. const {currentTime, currentHoverTime, replay} = useReplayContext();
  38. const {onMouseEnter, onMouseLeave, onClickTimestamp} = useCrumbHandlers();
  39. const isNetworkDetailsSetup = Boolean(replay?.isNetworkDetailsSetup());
  40. const networkFrames = replay?.getNetworkFrames();
  41. const projectId = replay?.getReplay()?.project_id;
  42. const startTimestampMs = replay?.getReplay()?.started_at?.getTime() || 0;
  43. const [scrollToRow, setScrollToRow] = useState<undefined | number>(undefined);
  44. const [visibleRange, setVisibleRange] = useState([0, 0]);
  45. const filterProps = useNetworkFilters({networkFrames: networkFrames || []});
  46. const {items: filteredItems, searchTerm, setSearchTerm} = filterProps;
  47. const clearSearchTerm = () => setSearchTerm('');
  48. const {handleSort, items, sortConfig} = useSortNetwork({items: filteredItems});
  49. const containerRef = useRef<HTMLDivElement>(null);
  50. const gridRef = useRef<MultiGrid>(null);
  51. const deps = useMemo(() => [items, searchTerm], [items, searchTerm]);
  52. const {cache, getColumnWidth, onScrollbarPresenceChange, onWrapperResize} =
  53. useVirtualizedGrid({
  54. cellMeasurer,
  55. gridRef,
  56. columnCount: COLUMN_COUNT,
  57. dynamicColumnIndex: 2,
  58. deps,
  59. });
  60. // `initialSize` cannot depend on containerRef because the ref starts as
  61. // `undefined` which then gets set into the hook and doesn't update.
  62. const initialSize = Math.max(150, window.innerHeight * 0.4);
  63. const {size: containerSize, ...resizableDrawerProps} = useResizableDrawer({
  64. direction: 'up',
  65. initialSize,
  66. min: 0,
  67. onResize: () => {},
  68. });
  69. const {getParamValue: getDetailRow, setParamValue: setDetailRow} = useUrlParams(
  70. 'n_detail_row',
  71. ''
  72. );
  73. const detailDataIndex = getDetailRow();
  74. const maxContainerHeight =
  75. (containerRef.current?.clientHeight || window.innerHeight) - RESIZEABLE_HANDLE_HEIGHT;
  76. const splitSize =
  77. networkFrames && detailDataIndex
  78. ? Math.min(maxContainerHeight, containerSize)
  79. : undefined;
  80. const onClickCell = useCallback(
  81. ({dataIndex, rowIndex}: {dataIndex: number; rowIndex: number}) => {
  82. if (getDetailRow() === String(dataIndex)) {
  83. setDetailRow('');
  84. trackAnalytics('replay.details-network-panel-closed', {
  85. is_sdk_setup: isNetworkDetailsSetup,
  86. organization,
  87. });
  88. } else {
  89. setDetailRow(String(dataIndex));
  90. setScrollToRow(rowIndex);
  91. const item = items[dataIndex];
  92. trackAnalytics('replay.details-network-panel-opened', {
  93. is_sdk_setup: isNetworkDetailsSetup,
  94. organization,
  95. resource_method: getFrameMethod(item),
  96. resource_status: String(getFrameStatus(item)),
  97. resource_type: item.op,
  98. });
  99. }
  100. },
  101. [getDetailRow, isNetworkDetailsSetup, items, organization, setDetailRow]
  102. );
  103. const cellRenderer = ({columnIndex, rowIndex, key, style, parent}: GridCellProps) => {
  104. const network = items[rowIndex - 1];
  105. return (
  106. <CellMeasurer
  107. cache={cache}
  108. columnIndex={columnIndex}
  109. key={key}
  110. parent={parent}
  111. rowIndex={rowIndex}
  112. >
  113. {({
  114. measure: _,
  115. registerChild,
  116. }: {
  117. measure: () => void;
  118. registerChild?: (element?: Element) => void;
  119. }) =>
  120. rowIndex === 0 ? (
  121. <NetworkHeaderCell
  122. ref={e => e && registerChild?.(e)}
  123. handleSort={handleSort}
  124. index={columnIndex}
  125. sortConfig={sortConfig}
  126. style={{...style, height: HEADER_HEIGHT}}
  127. />
  128. ) : (
  129. <NetworkTableCell
  130. columnIndex={columnIndex}
  131. currentHoverTime={currentHoverTime}
  132. currentTime={currentTime}
  133. frame={network}
  134. onMouseEnter={onMouseEnter}
  135. onMouseLeave={onMouseLeave}
  136. onClickCell={onClickCell}
  137. onClickTimestamp={onClickTimestamp}
  138. ref={e => e && registerChild?.(e)}
  139. rowIndex={rowIndex}
  140. sortConfig={sortConfig}
  141. startTimestampMs={startTimestampMs}
  142. style={{...style, height: BODY_HEIGHT}}
  143. />
  144. )
  145. }
  146. </CellMeasurer>
  147. );
  148. };
  149. const handleClick = () => {
  150. const index = indexAtCurrentTime();
  151. // When Jump Down, ensures purple line is visible and index needs to be 1 to jump to top of network list
  152. if (index > visibleRange[1] || index === 0) {
  153. setScrollToRow(index + 1);
  154. } else {
  155. setScrollToRow(index);
  156. }
  157. };
  158. function indexAtCurrentTime() {
  159. const frame = getNextReplayFrame({
  160. frames: items,
  161. targetOffsetMs: currentTime,
  162. allowExact: true,
  163. });
  164. const frameIndex = items.findIndex(spanFrame => frame === spanFrame);
  165. // frameIndex is -1 at end of replay, so use last index
  166. const index = frameIndex === -1 ? items.length - 1 : frameIndex;
  167. return index;
  168. }
  169. function pixelsToRow(pixels) {
  170. return Math.floor(pixels / BODY_HEIGHT);
  171. }
  172. const currentIndex = indexAtCurrentTime();
  173. const showJumpDownButton = currentIndex > visibleRange[1];
  174. const showJumpUpButton = currentIndex < visibleRange[0];
  175. return (
  176. <FluidHeight>
  177. <NetworkFilters networkFrames={networkFrames} {...filterProps} />
  178. <ReqRespBodiesAlert isNetworkDetailsSetup={isNetworkDetailsSetup} />
  179. <NetworkTable ref={containerRef} data-test-id="replay-details-network-tab">
  180. <SplitPanel
  181. style={{
  182. gridTemplateRows: splitSize !== undefined ? `1fr auto ${splitSize}px` : '1fr',
  183. }}
  184. >
  185. {networkFrames ? (
  186. <OverflowHidden>
  187. <AutoSizer onResize={onWrapperResize}>
  188. {({height, width}) => (
  189. <MultiGrid
  190. ref={gridRef}
  191. cellRenderer={cellRenderer}
  192. columnCount={COLUMN_COUNT}
  193. columnWidth={getColumnWidth(width)}
  194. deferredMeasurementCache={cache}
  195. estimatedColumnSize={100}
  196. estimatedRowSize={BODY_HEIGHT}
  197. fixedRowCount={1}
  198. height={height}
  199. noContentRenderer={() => (
  200. <NoRowRenderer
  201. unfilteredItems={networkFrames}
  202. clearSearchTerm={clearSearchTerm}
  203. >
  204. {t('No network requests recorded')}
  205. </NoRowRenderer>
  206. )}
  207. onScrollbarPresenceChange={onScrollbarPresenceChange}
  208. onScroll={({clientHeight, scrollTop}) => {
  209. if (scrollToRow !== undefined) {
  210. setScrollToRow(undefined);
  211. }
  212. setVisibleRange([
  213. pixelsToRow(scrollTop),
  214. pixelsToRow(scrollTop + clientHeight),
  215. ]);
  216. }}
  217. scrollToRow={scrollToRow}
  218. overscanColumnCount={COLUMN_COUNT}
  219. overscanRowCount={5}
  220. rowCount={items.length + 1}
  221. rowHeight={({index}) => (index === 0 ? HEADER_HEIGHT : BODY_HEIGHT)}
  222. width={width}
  223. />
  224. )}
  225. </AutoSizer>
  226. {sortConfig.by === 'startTimestamp' && showJumpUpButton ? (
  227. <JumpButton
  228. onClick={handleClick}
  229. aria-label={t('Jump Up')}
  230. priority="primary"
  231. size="xs"
  232. style={{top: '30px'}}
  233. >
  234. {t('↑ Jump to current timestamp')}
  235. </JumpButton>
  236. ) : null}
  237. {sortConfig.by === 'startTimestamp' && showJumpDownButton ? (
  238. <JumpButton
  239. onClick={handleClick}
  240. aria-label={t('Jump Down')}
  241. priority="primary"
  242. size="xs"
  243. style={{bottom: '5px'}}
  244. >
  245. {t('↓ Jump to current timestamp')}
  246. </JumpButton>
  247. ) : null}
  248. </OverflowHidden>
  249. ) : (
  250. <Placeholder height="100%" />
  251. )}
  252. <NetworkDetails
  253. {...resizableDrawerProps}
  254. isSetup={isNetworkDetailsSetup}
  255. item={detailDataIndex ? items[detailDataIndex] : null}
  256. onClose={() => {
  257. setDetailRow('');
  258. trackAnalytics('replay.details-network-panel-closed', {
  259. is_sdk_setup: isNetworkDetailsSetup,
  260. organization,
  261. });
  262. }}
  263. projectId={projectId}
  264. startTimestampMs={startTimestampMs}
  265. />
  266. </SplitPanel>
  267. </NetworkTable>
  268. </FluidHeight>
  269. );
  270. }
  271. const SplitPanel = styled('div')`
  272. width: 100%;
  273. height: 100%;
  274. position: relative;
  275. display: grid;
  276. overflow: auto;
  277. `;
  278. const OverflowHidden = styled('div')`
  279. position: relative;
  280. height: 100%;
  281. overflow: hidden;
  282. display: grid;
  283. `;
  284. const JumpButton = styled(Button)`
  285. position: absolute;
  286. justify-self: center;
  287. `;
  288. const NetworkTable = styled(FluidHeight)`
  289. border: 1px solid ${p => p.theme.border};
  290. border-radius: ${p => p.theme.borderRadius};
  291. .beforeHoverTime + .afterHoverTime:before {
  292. border-top: 1px solid ${p => p.theme.purple200};
  293. content: '';
  294. left: 0;
  295. position: absolute;
  296. top: 0;
  297. width: 999999999%;
  298. }
  299. .beforeHoverTime:last-child:before {
  300. border-bottom: 1px solid ${p => p.theme.purple200};
  301. content: '';
  302. right: 0;
  303. position: absolute;
  304. bottom: 0;
  305. width: 999999999%;
  306. }
  307. .beforeCurrentTime + .afterCurrentTime:before {
  308. border-top: 1px solid ${p => p.theme.purple300};
  309. content: '';
  310. left: 0;
  311. position: absolute;
  312. top: 0;
  313. width: 999999999%;
  314. }
  315. .beforeCurrentTime:last-child:before {
  316. border-bottom: 1px solid ${p => p.theme.purple300};
  317. content: '';
  318. right: 0;
  319. position: absolute;
  320. bottom: 0;
  321. width: 999999999%;
  322. }
  323. `;
  324. export default NetworkList;