index.tsx 7.3 KB

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