index.tsx 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 useOrganization from 'sentry/utils/useOrganization';
  10. import {useResizableDrawer} from 'sentry/utils/useResizableDrawer';
  11. import useUrlParams from 'sentry/utils/useUrlParams';
  12. import FluidHeight from 'sentry/views/replays/detail/layout/fluidHeight';
  13. import NetworkDetails from 'sentry/views/replays/detail/network/details';
  14. import {ReqRespBodiesAlert} from 'sentry/views/replays/detail/network/details/onboarding';
  15. import NetworkFilters from 'sentry/views/replays/detail/network/networkFilters';
  16. import NetworkHeaderCell, {
  17. COLUMN_COUNT,
  18. } from 'sentry/views/replays/detail/network/networkHeaderCell';
  19. import NetworkTableCell from 'sentry/views/replays/detail/network/networkTableCell';
  20. import useNetworkFilters from 'sentry/views/replays/detail/network/useNetworkFilters';
  21. import useSortNetwork from 'sentry/views/replays/detail/network/useSortNetwork';
  22. import NoRowRenderer from 'sentry/views/replays/detail/noRowRenderer';
  23. import useVirtualizedGrid from 'sentry/views/replays/detail/useVirtualizedGrid';
  24. import type {NetworkSpan} from 'sentry/views/replays/types';
  25. const HEADER_HEIGHT = 25;
  26. const BODY_HEIGHT = 28;
  27. const RESIZEABLE_HANDLE_HEIGHT = 90;
  28. type Props = {
  29. isNetworkDetailsSetup: boolean;
  30. networkSpans: undefined | NetworkSpan[];
  31. projectId: undefined | string;
  32. startTimestampMs: number;
  33. };
  34. const cellMeasurer = {
  35. defaultHeight: BODY_HEIGHT,
  36. defaultWidth: 100,
  37. fixedHeight: true,
  38. };
  39. function NetworkList({
  40. isNetworkDetailsSetup,
  41. networkSpans,
  42. projectId,
  43. startTimestampMs,
  44. }: Props) {
  45. const organization = useOrganization();
  46. const {currentTime, currentHoverTime} = useReplayContext();
  47. const [scrollToRow, setScrollToRow] = useState<undefined | number>(undefined);
  48. const filterProps = useNetworkFilters({networkSpans: networkSpans || []});
  49. const {items: filteredItems, searchTerm, setSearchTerm} = filterProps;
  50. const clearSearchTerm = () => setSearchTerm('');
  51. const {handleSort, items, sortConfig} = useSortNetwork({items: filteredItems});
  52. const {handleMouseEnter, handleMouseLeave, handleClick} =
  53. useCrumbHandlers(startTimestampMs);
  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. networkSpans && 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: item.data.method,
  101. resource_status: item.data.statusCode,
  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. onMouseEnter={handleMouseEnter}
  139. onMouseLeave={handleMouseLeave}
  140. onClickTimestamp={handleClick}
  141. onClickCell={onClickCell}
  142. ref={e => e && registerChild?.(e)}
  143. rowIndex={rowIndex}
  144. sortConfig={sortConfig}
  145. span={network}
  146. startTimestampMs={startTimestampMs}
  147. style={{...style, height: BODY_HEIGHT}}
  148. />
  149. )
  150. }
  151. </CellMeasurer>
  152. );
  153. };
  154. return (
  155. <FluidHeight>
  156. <NetworkFilters networkSpans={networkSpans} {...filterProps} />
  157. <ReqRespBodiesAlert isNetworkDetailsSetup={isNetworkDetailsSetup} />
  158. <NetworkTable ref={containerRef}>
  159. <SplitPanel
  160. style={{
  161. gridTemplateRows: splitSize !== undefined ? `1fr auto ${splitSize}px` : '1fr',
  162. }}
  163. >
  164. {networkSpans ? (
  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={networkSpans}
  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] as NetworkSpan) : 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;