index.tsx 7.5 KB

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