index.tsx 9.7 KB

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