index.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import {memo, useMemo, useRef} from 'react';
  2. import {
  3. AutoSizer,
  4. CellMeasurer,
  5. List as ReactVirtualizedList,
  6. ListRowProps,
  7. } from 'react-virtualized';
  8. import Placeholder from 'sentry/components/placeholder';
  9. import {useReplayContext} from 'sentry/components/replays/replayContext';
  10. import {t} from 'sentry/locale';
  11. import type {BreadcrumbFrame} from 'sentry/utils/replays/types';
  12. import ConsoleFilters from 'sentry/views/replays/detail/console/consoleFilters';
  13. import ConsoleLogRow from 'sentry/views/replays/detail/console/consoleLogRow';
  14. import useConsoleFilters from 'sentry/views/replays/detail/console/useConsoleFilters';
  15. import FluidHeight from 'sentry/views/replays/detail/layout/fluidHeight';
  16. import NoRowRenderer from 'sentry/views/replays/detail/noRowRenderer';
  17. import TabItemContainer from 'sentry/views/replays/detail/tabItemContainer';
  18. import useVirtualizedList from 'sentry/views/replays/detail/useVirtualizedList';
  19. import useVirtualizedInspector from '../useVirtualizedInspector';
  20. interface Props {
  21. frames: undefined | BreadcrumbFrame[];
  22. startTimestampMs: number;
  23. }
  24. // Ensure this object is created once as it is an input to
  25. // `useVirtualizedList`'s memoization
  26. const cellMeasurer = {
  27. fixedWidth: true,
  28. minHeight: 24,
  29. };
  30. function Console({frames, startTimestampMs}: Props) {
  31. const filterProps = useConsoleFilters({frames: frames || []});
  32. const {expandPathsRef, searchTerm, logLevel, items, setSearchTerm} = filterProps;
  33. const clearSearchTerm = () => setSearchTerm('');
  34. const {currentTime, currentHoverTime} = useReplayContext();
  35. const listRef = useRef<ReactVirtualizedList>(null);
  36. const deps = useMemo(() => [items], [items]);
  37. const {cache, updateList} = useVirtualizedList({
  38. cellMeasurer,
  39. ref: listRef,
  40. deps,
  41. });
  42. const {handleDimensionChange} = useVirtualizedInspector({
  43. cache,
  44. listRef,
  45. expandPathsRef,
  46. });
  47. const renderRow = ({index, key, style, parent}: ListRowProps) => {
  48. const item = items[index];
  49. return (
  50. <CellMeasurer
  51. cache={cache}
  52. columnIndex={0}
  53. // Set key based on filters, otherwise we can have odd expand/collapse state
  54. // with <ObjectInspector> when filtering
  55. key={`${searchTerm}-${logLevel.join(',')}-${key}`}
  56. parent={parent}
  57. rowIndex={index}
  58. >
  59. <ConsoleLogRow
  60. frame={item}
  61. currentTime={currentTime}
  62. currentHoverTime={currentHoverTime}
  63. expandPaths={Array.from(expandPathsRef.current?.get(index) || [])}
  64. index={index}
  65. onDimensionChange={handleDimensionChange}
  66. startTimestampMs={startTimestampMs}
  67. style={style}
  68. />
  69. </CellMeasurer>
  70. );
  71. };
  72. return (
  73. <FluidHeight>
  74. <ConsoleFilters frames={frames} {...filterProps} />
  75. <TabItemContainer data-test-id="replay-details-console-tab">
  76. {frames ? (
  77. <AutoSizer onResize={updateList}>
  78. {({width, height}) => (
  79. <ReactVirtualizedList
  80. deferredMeasurementCache={cache}
  81. height={height}
  82. noRowsRenderer={() => (
  83. <NoRowRenderer
  84. unfilteredItems={frames}
  85. clearSearchTerm={clearSearchTerm}
  86. >
  87. {t('No console logs recorded')}
  88. </NoRowRenderer>
  89. )}
  90. overscanRowCount={5}
  91. ref={listRef}
  92. rowCount={items.length}
  93. rowHeight={cache.rowHeight}
  94. rowRenderer={renderRow}
  95. width={width}
  96. />
  97. )}
  98. </AutoSizer>
  99. ) : (
  100. <Placeholder height="100%" />
  101. )}
  102. </TabItemContainer>
  103. </FluidHeight>
  104. );
  105. }
  106. export default memo(Console);