index.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 styled from '@emotion/styled';
  9. import Placeholder from 'sentry/components/placeholder';
  10. import {t} from 'sentry/locale';
  11. import type {Crumb} from 'sentry/types/breadcrumbs';
  12. import BreadcrumbRow from 'sentry/views/replays/detail/breadcrumbs/breadcrumbRow';
  13. import useScrollToCurrentItem from 'sentry/views/replays/detail/breadcrumbs/useScrollToCurrentItem';
  14. import FluidHeight from 'sentry/views/replays/detail/layout/fluidHeight';
  15. import NoRowRenderer from 'sentry/views/replays/detail/noRowRenderer';
  16. import useVirtualizedList from 'sentry/views/replays/detail/useVirtualizedList';
  17. import useVirtualizedInspector from '../useVirtualizedInspector';
  18. type Props = {
  19. breadcrumbs: undefined | Crumb[];
  20. startTimestampMs: number;
  21. };
  22. // Ensure this object is created once as it is an input to
  23. // `useVirtualizedList`'s memoization
  24. const cellMeasurer = {
  25. fixedWidth: true,
  26. minHeight: 53,
  27. };
  28. function Breadcrumbs({breadcrumbs, startTimestampMs}: Props) {
  29. const listRef = useRef<ReactVirtualizedList>(null);
  30. // Keep a reference of object paths that are expanded (via <ObjectInspector>)
  31. // by log row, so they they can be restored as the Console pane is scrolling.
  32. // Due to virtualization, components can be unmounted as the user scrolls, so
  33. // state needs to be remembered.
  34. //
  35. // Note that this is intentionally not in state because we do not want to
  36. // re-render when items are expanded/collapsed, though it may work in state as well.
  37. const expandPathsRef = useRef(new Map<number, Set<string>>());
  38. const deps = useMemo(() => [breadcrumbs], [breadcrumbs]);
  39. const {cache, updateList} = useVirtualizedList({
  40. cellMeasurer,
  41. ref: listRef,
  42. deps,
  43. });
  44. const {handleDimensionChange} = useVirtualizedInspector({
  45. cache,
  46. listRef,
  47. expandPathsRef,
  48. });
  49. useScrollToCurrentItem({
  50. breadcrumbs,
  51. ref: listRef,
  52. startTimestampMs,
  53. });
  54. const renderRow = ({index, key, style, parent}: ListRowProps) => {
  55. const item = (breadcrumbs || [])[index];
  56. return (
  57. <CellMeasurer
  58. cache={cache}
  59. columnIndex={0}
  60. key={key}
  61. parent={parent}
  62. rowIndex={index}
  63. >
  64. <BreadcrumbRow
  65. index={index}
  66. breadcrumb={item}
  67. startTimestampMs={startTimestampMs}
  68. style={style}
  69. expandPaths={Array.from(expandPathsRef.current?.get(index) || [])}
  70. onDimensionChange={handleDimensionChange}
  71. />
  72. </CellMeasurer>
  73. );
  74. };
  75. return (
  76. <FluidHeight>
  77. <BreadcrumbContainer>
  78. {breadcrumbs ? (
  79. <AutoSizer onResize={updateList}>
  80. {({height, width}) => (
  81. <ReactVirtualizedList
  82. deferredMeasurementCache={cache}
  83. height={height}
  84. noRowsRenderer={() => (
  85. <NoRowRenderer unfilteredItems={breadcrumbs} clearSearchTerm={() => {}}>
  86. {t('No breadcrumbs recorded')}
  87. </NoRowRenderer>
  88. )}
  89. overscanRowCount={5}
  90. ref={listRef}
  91. rowCount={breadcrumbs.length}
  92. rowHeight={cache.rowHeight}
  93. rowRenderer={renderRow}
  94. width={width}
  95. />
  96. )}
  97. </AutoSizer>
  98. ) : (
  99. <Placeholder height="100%" />
  100. )}
  101. </BreadcrumbContainer>
  102. </FluidHeight>
  103. );
  104. }
  105. const BreadcrumbContainer = styled('div')`
  106. position: relative;
  107. height: 100%;
  108. overflow: hidden;
  109. border: 1px solid ${p => p.theme.border};
  110. border-radius: ${p => p.theme.borderRadius};
  111. .beforeHoverTime + .afterHoverTime:before {
  112. background-color: ${p => p.theme.surface200};
  113. border-top: 1px solid ${p => p.theme.purple200};
  114. content: '';
  115. left: 0;
  116. position: absolute;
  117. top: 0;
  118. width: 999999999%;
  119. }
  120. .beforeHoverTime:last-child:before {
  121. background-color: ${p => p.theme.surface200};
  122. border-bottom: 1px solid ${p => p.theme.purple200};
  123. content: '';
  124. right: 0;
  125. position: absolute;
  126. bottom: 0;
  127. width: 999999999%;
  128. }
  129. .beforeCurrentTime + .afterCurrentTime:before {
  130. background-color: ${p => p.theme.purple100};
  131. border-top: 1px solid ${p => p.theme.purple300};
  132. content: '';
  133. left: 0;
  134. position: absolute;
  135. top: 0;
  136. width: 999999999%;
  137. }
  138. .beforeCurrentTime:last-child:before {
  139. background-color: ${p => p.theme.purple100};
  140. border-bottom: 1px solid ${p => p.theme.purple300};
  141. content: '';
  142. right: 0;
  143. position: absolute;
  144. bottom: 0;
  145. width: 999999999%;
  146. }
  147. `;
  148. export default memo(Breadcrumbs);