index.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 Placeholder from 'sentry/components/placeholder';
  5. import JumpButtons from 'sentry/components/replays/jumpButtons';
  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 =
  174. sortConfig.by === 'startTimestamp' &&
  175. currentIndex > visibleRange[1] &&
  176. networkFrames?.length;
  177. const showJumpUpButton =
  178. sortConfig.by === 'startTimestamp' &&
  179. currentIndex < visibleRange[0] &&
  180. networkFrames?.length;
  181. return (
  182. <FluidHeight>
  183. <NetworkFilters networkFrames={networkFrames} {...filterProps} />
  184. <ReqRespBodiesAlert isNetworkDetailsSetup={isNetworkDetailsSetup} />
  185. <NetworkTable ref={containerRef} data-test-id="replay-details-network-tab">
  186. <SplitPanel
  187. style={{
  188. gridTemplateRows: splitSize !== undefined ? `1fr auto ${splitSize}px` : '1fr',
  189. }}
  190. >
  191. {networkFrames ? (
  192. <OverflowHidden>
  193. <AutoSizer onResize={onWrapperResize}>
  194. {({height, width}) => (
  195. <MultiGrid
  196. ref={gridRef}
  197. cellRenderer={cellRenderer}
  198. columnCount={COLUMN_COUNT}
  199. columnWidth={getColumnWidth(width)}
  200. deferredMeasurementCache={cache}
  201. estimatedColumnSize={100}
  202. estimatedRowSize={BODY_HEIGHT}
  203. fixedRowCount={1}
  204. height={height}
  205. noContentRenderer={() => (
  206. <NoRowRenderer
  207. unfilteredItems={networkFrames}
  208. clearSearchTerm={clearSearchTerm}
  209. >
  210. {t('No network requests recorded')}
  211. </NoRowRenderer>
  212. )}
  213. onScrollbarPresenceChange={onScrollbarPresenceChange}
  214. onScroll={({clientHeight, scrollTop}) => {
  215. if (scrollToRow !== undefined) {
  216. setScrollToRow(undefined);
  217. }
  218. setVisibleRange([
  219. pixelsToRow(scrollTop),
  220. pixelsToRow(scrollTop + clientHeight),
  221. ]);
  222. }}
  223. scrollToRow={scrollToRow}
  224. overscanColumnCount={COLUMN_COUNT}
  225. overscanRowCount={5}
  226. rowCount={items.length + 1}
  227. rowHeight={({index}) => (index === 0 ? HEADER_HEIGHT : BODY_HEIGHT)}
  228. width={width}
  229. />
  230. )}
  231. </AutoSizer>
  232. <JumpButtons
  233. jump={showJumpUpButton ? 'up' : showJumpDownButton ? 'down' : undefined}
  234. onClick={handleClick}
  235. tableHeaderHeight={HEADER_HEIGHT}
  236. />
  237. </OverflowHidden>
  238. ) : (
  239. <Placeholder height="100%" />
  240. )}
  241. <NetworkDetails
  242. {...resizableDrawerProps}
  243. isSetup={isNetworkDetailsSetup}
  244. item={detailDataIndex ? items[detailDataIndex] : null}
  245. onClose={() => {
  246. setDetailRow('');
  247. trackAnalytics('replay.details-network-panel-closed', {
  248. is_sdk_setup: isNetworkDetailsSetup,
  249. organization,
  250. });
  251. }}
  252. projectId={projectId}
  253. startTimestampMs={startTimestampMs}
  254. />
  255. </SplitPanel>
  256. </NetworkTable>
  257. </FluidHeight>
  258. );
  259. }
  260. const SplitPanel = styled('div')`
  261. width: 100%;
  262. height: 100%;
  263. position: relative;
  264. display: grid;
  265. overflow: auto;
  266. `;
  267. const OverflowHidden = styled('div')`
  268. position: relative;
  269. height: 100%;
  270. overflow: hidden;
  271. display: grid;
  272. `;
  273. const NetworkTable = styled(FluidHeight)`
  274. border: 1px solid ${p => p.theme.border};
  275. border-radius: ${p => p.theme.borderRadius};
  276. .beforeHoverTime + .afterHoverTime:before {
  277. border-top: 1px solid ${p => p.theme.purple200};
  278. content: '';
  279. left: 0;
  280. position: absolute;
  281. top: 0;
  282. width: 999999999%;
  283. }
  284. .beforeHoverTime:last-child:before {
  285. border-bottom: 1px solid ${p => p.theme.purple200};
  286. content: '';
  287. right: 0;
  288. position: absolute;
  289. bottom: 0;
  290. width: 999999999%;
  291. }
  292. .beforeCurrentTime + .afterCurrentTime:before {
  293. border-top: 1px solid ${p => p.theme.purple300};
  294. content: '';
  295. left: 0;
  296. position: absolute;
  297. top: 0;
  298. width: 999999999%;
  299. }
  300. .beforeCurrentTime:last-child:before {
  301. border-bottom: 1px solid ${p => p.theme.purple300};
  302. content: '';
  303. right: 0;
  304. position: absolute;
  305. bottom: 0;
  306. width: 999999999%;
  307. }
  308. `;
  309. export default NetworkList;