index.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 useJumpButtons from 'sentry/components/replays/useJumpButtons';
  8. import {t} from 'sentry/locale';
  9. import {trackAnalytics} from 'sentry/utils/analytics';
  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 = 25;
  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 filterProps = useNetworkFilters({networkFrames: networkFrames || []});
  45. const {items: filteredItems, searchTerm, setSearchTerm} = filterProps;
  46. const clearSearchTerm = () => setSearchTerm('');
  47. const {handleSort, items, sortConfig} = useSortNetwork({items: filteredItems});
  48. const containerRef = useRef<HTMLDivElement>(null);
  49. const gridRef = useRef<MultiGrid>(null);
  50. const deps = useMemo(() => [items, searchTerm], [items, searchTerm]);
  51. const {cache, getColumnWidth, onScrollbarPresenceChange, onWrapperResize} =
  52. useVirtualizedGrid({
  53. cellMeasurer,
  54. gridRef,
  55. columnCount: COLUMN_COUNT,
  56. dynamicColumnIndex: 2,
  57. deps,
  58. });
  59. // `initialSize` cannot depend on containerRef because the ref starts as
  60. // `undefined` which then gets set into the hook and doesn't update.
  61. const initialSize = Math.max(150, window.innerHeight * 0.4);
  62. const {size: containerSize, ...resizableDrawerProps} = useResizableDrawer({
  63. direction: 'up',
  64. initialSize,
  65. min: 0,
  66. onResize: () => {},
  67. });
  68. const {getParamValue: getDetailRow, setParamValue: setDetailRow} = useUrlParams(
  69. 'n_detail_row',
  70. ''
  71. );
  72. const detailDataIndex = getDetailRow();
  73. const maxContainerHeight =
  74. (containerRef.current?.clientHeight || window.innerHeight) - RESIZEABLE_HANDLE_HEIGHT;
  75. const splitSize =
  76. networkFrames && detailDataIndex
  77. ? Math.min(maxContainerHeight, containerSize)
  78. : undefined;
  79. const {
  80. handleClick: onClickToJump,
  81. onSectionRendered,
  82. showJumpDownButton,
  83. showJumpUpButton,
  84. } = useJumpButtons({
  85. currentTime,
  86. frames: filteredItems,
  87. isTable: true,
  88. setScrollToRow,
  89. });
  90. const onClickCell = useCallback(
  91. ({dataIndex, rowIndex}: {dataIndex: number; rowIndex: number}) => {
  92. if (getDetailRow() === String(dataIndex)) {
  93. setDetailRow('');
  94. trackAnalytics('replay.details-network-panel-closed', {
  95. is_sdk_setup: isNetworkDetailsSetup,
  96. organization,
  97. });
  98. } else {
  99. setDetailRow(String(dataIndex));
  100. setScrollToRow(rowIndex);
  101. const item = items[dataIndex];
  102. trackAnalytics('replay.details-network-panel-opened', {
  103. is_sdk_setup: isNetworkDetailsSetup,
  104. organization,
  105. resource_method: getFrameMethod(item),
  106. resource_status: String(getFrameStatus(item)),
  107. resource_type: item.op,
  108. });
  109. }
  110. },
  111. [getDetailRow, isNetworkDetailsSetup, items, organization, setDetailRow]
  112. );
  113. const cellRenderer = ({columnIndex, rowIndex, key, style, parent}: GridCellProps) => {
  114. const network = items[rowIndex - 1];
  115. return (
  116. <CellMeasurer
  117. cache={cache}
  118. columnIndex={columnIndex}
  119. key={key}
  120. parent={parent}
  121. rowIndex={rowIndex}
  122. >
  123. {({
  124. measure: _,
  125. registerChild,
  126. }: {
  127. measure: () => void;
  128. registerChild?: (element?: Element) => void;
  129. }) =>
  130. rowIndex === 0 ? (
  131. <NetworkHeaderCell
  132. ref={e => e && registerChild?.(e)}
  133. handleSort={handleSort}
  134. index={columnIndex}
  135. sortConfig={sortConfig}
  136. style={{...style, height: HEADER_HEIGHT}}
  137. />
  138. ) : (
  139. <NetworkTableCell
  140. columnIndex={columnIndex}
  141. currentHoverTime={currentHoverTime}
  142. currentTime={currentTime}
  143. frame={network}
  144. onMouseEnter={onMouseEnter}
  145. onMouseLeave={onMouseLeave}
  146. onClickCell={onClickCell}
  147. onClickTimestamp={onClickTimestamp}
  148. ref={e => e && registerChild?.(e)}
  149. rowIndex={rowIndex}
  150. sortConfig={sortConfig}
  151. startTimestampMs={startTimestampMs}
  152. style={{...style, height: BODY_HEIGHT}}
  153. />
  154. )
  155. }
  156. </CellMeasurer>
  157. );
  158. };
  159. return (
  160. <FluidHeight>
  161. <NetworkFilters networkFrames={networkFrames} {...filterProps} />
  162. <ReqRespBodiesAlert isNetworkDetailsSetup={isNetworkDetailsSetup} />
  163. <NetworkTable ref={containerRef} data-test-id="replay-details-network-tab">
  164. <SplitPanel
  165. style={{
  166. gridTemplateRows: splitSize !== undefined ? `1fr auto ${splitSize}px` : '1fr',
  167. }}
  168. >
  169. {networkFrames ? (
  170. <OverflowHidden>
  171. <AutoSizer onResize={onWrapperResize}>
  172. {({height, width}) => (
  173. <MultiGrid
  174. ref={gridRef}
  175. cellRenderer={cellRenderer}
  176. columnCount={COLUMN_COUNT}
  177. columnWidth={getColumnWidth(width)}
  178. deferredMeasurementCache={cache}
  179. estimatedColumnSize={100}
  180. estimatedRowSize={BODY_HEIGHT}
  181. fixedRowCount={1}
  182. height={height}
  183. noContentRenderer={() => (
  184. <NoRowRenderer
  185. unfilteredItems={networkFrames}
  186. clearSearchTerm={clearSearchTerm}
  187. >
  188. {t('No network requests recorded')}
  189. </NoRowRenderer>
  190. )}
  191. onScrollbarPresenceChange={onScrollbarPresenceChange}
  192. onScroll={() => {
  193. if (scrollToRow !== undefined) {
  194. setScrollToRow(undefined);
  195. }
  196. }}
  197. onSectionRendered={onSectionRendered}
  198. overscanColumnCount={COLUMN_COUNT}
  199. overscanRowCount={5}
  200. rowCount={items.length + 1}
  201. rowHeight={({index}) => (index === 0 ? HEADER_HEIGHT : BODY_HEIGHT)}
  202. scrollToRow={scrollToRow}
  203. width={width}
  204. />
  205. )}
  206. </AutoSizer>
  207. {sortConfig.by === 'startTimestamp' && items.length ? (
  208. <JumpButtons
  209. jump={showJumpUpButton ? 'up' : showJumpDownButton ? 'down' : undefined}
  210. onClick={onClickToJump}
  211. tableHeaderHeight={HEADER_HEIGHT}
  212. />
  213. ) : null}
  214. </OverflowHidden>
  215. ) : (
  216. <Placeholder height="100%" />
  217. )}
  218. <NetworkDetails
  219. {...resizableDrawerProps}
  220. isSetup={isNetworkDetailsSetup}
  221. item={detailDataIndex ? items[detailDataIndex] : null}
  222. onClose={() => {
  223. setDetailRow('');
  224. trackAnalytics('replay.details-network-panel-closed', {
  225. is_sdk_setup: isNetworkDetailsSetup,
  226. organization,
  227. });
  228. }}
  229. projectId={projectId}
  230. startTimestampMs={startTimestampMs}
  231. />
  232. </SplitPanel>
  233. </NetworkTable>
  234. </FluidHeight>
  235. );
  236. }
  237. const SplitPanel = styled('div')`
  238. width: 100%;
  239. height: 100%;
  240. position: relative;
  241. display: grid;
  242. overflow: auto;
  243. `;
  244. const OverflowHidden = styled('div')`
  245. position: relative;
  246. height: 100%;
  247. overflow: hidden;
  248. display: grid;
  249. `;
  250. const NetworkTable = styled(FluidHeight)`
  251. border: 1px solid ${p => p.theme.border};
  252. border-radius: ${p => p.theme.borderRadius};
  253. .beforeHoverTime + .afterHoverTime:before {
  254. border-top: 1px solid ${p => p.theme.purple200};
  255. content: '';
  256. left: 0;
  257. position: absolute;
  258. top: 0;
  259. width: 999999999%;
  260. }
  261. .beforeHoverTime:last-child:before {
  262. border-bottom: 1px solid ${p => p.theme.purple200};
  263. content: '';
  264. right: 0;
  265. position: absolute;
  266. bottom: 0;
  267. width: 999999999%;
  268. }
  269. .beforeCurrentTime + .afterCurrentTime:before {
  270. border-top: 1px solid ${p => p.theme.purple300};
  271. content: '';
  272. left: 0;
  273. position: absolute;
  274. top: 0;
  275. width: 999999999%;
  276. }
  277. .beforeCurrentTime:last-child:before {
  278. border-bottom: 1px solid ${p => p.theme.purple300};
  279. content: '';
  280. right: 0;
  281. position: absolute;
  282. bottom: 0;
  283. width: 999999999%;
  284. }
  285. `;
  286. export default NetworkList;