index.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import {memo, useMemo, useRef, useState} from 'react';
  2. import type {ListRowProps} from 'react-virtualized';
  3. import {AutoSizer, CellMeasurer, List as ReactVirtualizedList} from 'react-virtualized';
  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 useCrumbHandlers from 'sentry/utils/replays/hooks/useCrumbHandlers';
  10. import ConsoleFilters from 'sentry/views/replays/detail/console/consoleFilters';
  11. import ConsoleLogRow from 'sentry/views/replays/detail/console/consoleLogRow';
  12. import useConsoleFilters from 'sentry/views/replays/detail/console/useConsoleFilters';
  13. import FluidHeight from 'sentry/views/replays/detail/layout/fluidHeight';
  14. import NoRowRenderer from 'sentry/views/replays/detail/noRowRenderer';
  15. import TabItemContainer from 'sentry/views/replays/detail/tabItemContainer';
  16. import useVirtualizedList from 'sentry/views/replays/detail/useVirtualizedList';
  17. import useVirtualizedInspector from '../useVirtualizedInspector';
  18. // Ensure this object is created once as it is an input to
  19. // `useVirtualizedList`'s memoization
  20. const cellMeasurer = {
  21. fixedWidth: true,
  22. minHeight: 24,
  23. };
  24. function Console() {
  25. const {currentTime, currentHoverTime, replay} = useReplayContext();
  26. const {onMouseEnter, onMouseLeave, onClickTimestamp} = useCrumbHandlers();
  27. const startTimestampMs = replay?.getReplay()?.started_at?.getTime() ?? 0;
  28. const frames = replay?.getConsoleFrames();
  29. const [scrollToRow, setScrollToRow] = useState<undefined | number>(undefined);
  30. const filterProps = useConsoleFilters({frames: frames || []});
  31. const {expandPathsRef, searchTerm, logLevel, items, setSearchTerm} = filterProps;
  32. const clearSearchTerm = () => setSearchTerm('');
  33. const listRef = useRef<ReactVirtualizedList>(null);
  34. const deps = useMemo(() => [items], [items]);
  35. const {cache, updateList} = useVirtualizedList({
  36. cellMeasurer,
  37. ref: listRef,
  38. deps,
  39. });
  40. const {handleDimensionChange} = useVirtualizedInspector({
  41. cache,
  42. listRef,
  43. expandPathsRef,
  44. });
  45. const {
  46. handleClick: onClickToJump,
  47. onRowsRendered,
  48. showJumpDownButton,
  49. showJumpUpButton,
  50. } = useJumpButtons({
  51. currentTime,
  52. frames: items,
  53. isTable: false,
  54. setScrollToRow,
  55. });
  56. const renderRow = ({index, key, style, parent}: ListRowProps) => {
  57. const item = items[index];
  58. return (
  59. <CellMeasurer
  60. cache={cache}
  61. columnIndex={0}
  62. // Set key based on filters, otherwise we can have odd expand/collapse state
  63. // with <ObjectInspector> when filtering
  64. key={`${searchTerm}-${logLevel.join(',')}-${key}`}
  65. parent={parent}
  66. rowIndex={index}
  67. >
  68. <ConsoleLogRow
  69. currentHoverTime={currentHoverTime}
  70. currentTime={currentTime}
  71. expandPaths={Array.from(expandPathsRef.current?.get(index) || [])}
  72. frame={item}
  73. onMouseEnter={onMouseEnter}
  74. onMouseLeave={onMouseLeave}
  75. index={index}
  76. onClickTimestamp={onClickTimestamp}
  77. onDimensionChange={handleDimensionChange}
  78. startTimestampMs={startTimestampMs}
  79. style={style}
  80. />
  81. </CellMeasurer>
  82. );
  83. };
  84. return (
  85. <FluidHeight>
  86. <ConsoleFilters frames={frames} {...filterProps} />
  87. <TabItemContainer data-test-id="replay-details-console-tab">
  88. {frames ? (
  89. <AutoSizer onResize={updateList}>
  90. {({width, height}) => (
  91. <ReactVirtualizedList
  92. deferredMeasurementCache={cache}
  93. height={height}
  94. noRowsRenderer={() => (
  95. <NoRowRenderer
  96. unfilteredItems={frames}
  97. clearSearchTerm={clearSearchTerm}
  98. >
  99. {t('No console logs recorded')}
  100. </NoRowRenderer>
  101. )}
  102. onRowsRendered={onRowsRendered}
  103. onScroll={() => {
  104. if (scrollToRow !== undefined) {
  105. setScrollToRow(undefined);
  106. }
  107. }}
  108. overscanRowCount={5}
  109. ref={listRef}
  110. rowCount={items.length}
  111. rowHeight={cache.rowHeight}
  112. rowRenderer={renderRow}
  113. scrollToIndex={scrollToRow}
  114. width={width}
  115. />
  116. )}
  117. </AutoSizer>
  118. ) : (
  119. <Placeholder height="100%" />
  120. )}
  121. {items?.length ? (
  122. <JumpButtons
  123. jump={showJumpUpButton ? 'up' : showJumpDownButton ? 'down' : undefined}
  124. onClick={onClickToJump}
  125. tableHeaderHeight={0}
  126. />
  127. ) : null}
  128. </TabItemContainer>
  129. </FluidHeight>
  130. );
  131. }
  132. export default memo(Console);