index.tsx 9.7 KB

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