index.tsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import {useEffect, useMemo, useRef, useState} from 'react';
  2. import type {ListRowProps} from 'react-virtualized';
  3. import {AutoSizer, CellMeasurer, List as ReactVirtualizedList} from 'react-virtualized';
  4. import styled from '@emotion/styled';
  5. import Alert from 'sentry/components/alert';
  6. import {Button} from 'sentry/components/button';
  7. import ExternalLink from 'sentry/components/links/externalLink';
  8. import Placeholder from 'sentry/components/placeholder';
  9. import JumpButtons from 'sentry/components/replays/jumpButtons';
  10. import {useReplayContext} from 'sentry/components/replays/replayContext';
  11. import useJumpButtons from 'sentry/components/replays/useJumpButtons';
  12. import {IconClose} from 'sentry/icons';
  13. import {t, tct} from 'sentry/locale';
  14. import {space} from 'sentry/styles/space';
  15. import {trackAnalytics} from 'sentry/utils/analytics';
  16. import useCrumbHandlers from 'sentry/utils/replays/hooks/useCrumbHandlers';
  17. import useExtractedDomNodes from 'sentry/utils/replays/hooks/useExtractedDomNodes';
  18. import useDismissAlert from 'sentry/utils/useDismissAlert';
  19. import useOrganization from 'sentry/utils/useOrganization';
  20. import useVirtualizedInspector from 'sentry/views/replays/detail//useVirtualizedInspector';
  21. import BreadcrumbFilters from 'sentry/views/replays/detail/breadcrumbs/breadcrumbFilters';
  22. import BreadcrumbRow from 'sentry/views/replays/detail/breadcrumbs/breadcrumbRow';
  23. import useBreadcrumbFilters from 'sentry/views/replays/detail/breadcrumbs/useBreadcrumbFilters';
  24. import useScrollToCurrentItem from 'sentry/views/replays/detail/breadcrumbs/useScrollToCurrentItem';
  25. import FilterLoadingIndicator from 'sentry/views/replays/detail/filterLoadingIndicator';
  26. import FluidHeight from 'sentry/views/replays/detail/layout/fluidHeight';
  27. import NoRowRenderer from 'sentry/views/replays/detail/noRowRenderer';
  28. import useReplayPerfData from 'sentry/views/replays/detail/perfTable/useReplayPerfData';
  29. import TabItemContainer from 'sentry/views/replays/detail/tabItemContainer';
  30. import useVirtualizedList from 'sentry/views/replays/detail/useVirtualizedList';
  31. import useVirtualListDimentionChange from 'sentry/views/replays/detail/useVirtualListDimentionChange';
  32. const LOCAL_STORAGE_KEY = 'replay-details-mask-config-instructions-dismissed';
  33. // Ensure this object is created once as it is an input to
  34. // `useVirtualizedList`'s memoization
  35. const cellMeasurer = {
  36. fixedWidth: true,
  37. minHeight: 53,
  38. };
  39. function Breadcrumbs() {
  40. const {dismiss, isDismissed} = useDismissAlert({key: LOCAL_STORAGE_KEY});
  41. const {currentTime, replay, startTimeOffsetMs, durationMs} = useReplayContext();
  42. const organization = useOrganization();
  43. const hasPerfTab = organization.features.includes('session-replay-trace-table');
  44. const {onClickTimestamp} = useCrumbHandlers();
  45. const {data: frameToExtraction, isFetching: isFetchingExtractions} =
  46. useExtractedDomNodes({replay});
  47. const {data: frameToTrace, isFetching: isFetchingTraces} = useReplayPerfData({replay});
  48. const startTimestampMs =
  49. replay?.getReplay()?.started_at?.getTime() ?? 0 + startTimeOffsetMs;
  50. const allFrames = replay?.getChapterFrames();
  51. const frames = useMemo(
  52. () =>
  53. allFrames?.filter(
  54. frame =>
  55. frame.offsetMs >= startTimeOffsetMs &&
  56. frame.offsetMs <= startTimeOffsetMs + durationMs
  57. ),
  58. [allFrames, durationMs, startTimeOffsetMs]
  59. );
  60. const [scrollToRow, setScrollToRow] = useState<undefined | number>(undefined);
  61. const filterProps = useBreadcrumbFilters({frames: frames || []});
  62. const {expandPathsRef, items, searchTerm, setSearchTerm} = filterProps;
  63. const clearSearchTerm = () => setSearchTerm('');
  64. const listRef = useRef<ReactVirtualizedList>(null);
  65. const deps = useMemo(() => [items, searchTerm], [items, searchTerm]);
  66. const {cache, updateList} = useVirtualizedList({
  67. cellMeasurer,
  68. ref: listRef,
  69. deps,
  70. });
  71. const {handleDimensionChange} = useVirtualListDimentionChange({cache, listRef});
  72. const {handleDimensionChange: handleInspectorExpanded} = useVirtualizedInspector({
  73. cache,
  74. listRef,
  75. expandPathsRef,
  76. });
  77. const {
  78. handleClick: onClickToJump,
  79. onRowsRendered,
  80. showJumpDownButton,
  81. showJumpUpButton,
  82. } = useJumpButtons({
  83. currentTime,
  84. frames: items,
  85. isTable: false,
  86. setScrollToRow,
  87. });
  88. useScrollToCurrentItem({
  89. frames,
  90. ref: listRef,
  91. });
  92. // Need to refresh the item dimensions as DOM & Trace data gets loaded
  93. useEffect(() => {
  94. if (!isFetchingExtractions || !isFetchingTraces) {
  95. updateList();
  96. }
  97. }, [isFetchingExtractions, isFetchingTraces, updateList]);
  98. const renderRow = ({index, key, style, parent}: ListRowProps) => {
  99. const item = (items || [])[index];
  100. return (
  101. <CellMeasurer
  102. cache={cache}
  103. columnIndex={0}
  104. key={key}
  105. parent={parent}
  106. rowIndex={index}
  107. >
  108. <BreadcrumbRow
  109. index={index}
  110. frame={item}
  111. extraction={frameToExtraction?.get(item)}
  112. traces={hasPerfTab ? frameToTrace?.get(item) : undefined}
  113. startTimestampMs={startTimestampMs}
  114. style={style}
  115. expandPaths={Array.from(expandPathsRef.current?.get(index) || [])}
  116. onClick={() => {
  117. onClickTimestamp(item);
  118. }}
  119. onDimensionChange={handleDimensionChange}
  120. onInspectorExpanded={handleInspectorExpanded}
  121. />
  122. </CellMeasurer>
  123. );
  124. };
  125. return (
  126. <FluidHeight>
  127. <FilterLoadingIndicator isLoading={isFetchingExtractions || isFetchingTraces}>
  128. <BreadcrumbFilters frames={frames} {...filterProps} />
  129. </FilterLoadingIndicator>
  130. {isDismissed ? null : (
  131. <StyledAlert
  132. type="info"
  133. showIcon
  134. trailingItems={
  135. <Button
  136. aria-label={t('Dismiss banner')}
  137. icon={<IconClose />}
  138. onClick={dismiss}
  139. size="zero"
  140. borderless
  141. />
  142. }
  143. >
  144. {tct('Learn how to unmask text (****) and unblock media [link:here].', {
  145. link: (
  146. <ExternalLink
  147. href="https://docs.sentry.io/platforms/javascript/session-replay/privacy/"
  148. onClick={() => {
  149. trackAnalytics('replay.details-mask-banner-link-clicked', {
  150. organization,
  151. });
  152. }}
  153. />
  154. ),
  155. })}
  156. </StyledAlert>
  157. )}
  158. <TabItemContainer data-test-id="replay-details-breadcrumbs-tab">
  159. {frames ? (
  160. <AutoSizer onResize={updateList}>
  161. {({height, width}) => (
  162. <ReactVirtualizedList
  163. deferredMeasurementCache={cache}
  164. height={height}
  165. noRowsRenderer={() => (
  166. <NoRowRenderer
  167. unfilteredItems={frames}
  168. clearSearchTerm={clearSearchTerm}
  169. >
  170. {t('No breadcrumbs recorded')}
  171. </NoRowRenderer>
  172. )}
  173. onRowsRendered={onRowsRendered}
  174. onScroll={() => {
  175. if (scrollToRow !== undefined) {
  176. setScrollToRow(undefined);
  177. }
  178. }}
  179. overscanRowCount={5}
  180. ref={listRef}
  181. rowCount={items.length}
  182. rowHeight={cache.rowHeight}
  183. rowRenderer={renderRow}
  184. scrollToIndex={scrollToRow}
  185. width={width}
  186. />
  187. )}
  188. </AutoSizer>
  189. ) : (
  190. <Placeholder height="100%" />
  191. )}
  192. {items?.length ? (
  193. <JumpButtons
  194. jump={showJumpUpButton ? 'up' : showJumpDownButton ? 'down' : undefined}
  195. onClick={onClickToJump}
  196. tableHeaderHeight={0}
  197. />
  198. ) : null}
  199. </TabItemContainer>
  200. </FluidHeight>
  201. );
  202. }
  203. const StyledAlert = styled(Alert)`
  204. margin-bottom: ${space(1)};
  205. `;
  206. export default Breadcrumbs;