index.tsx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. import {useEffect} from 'react';
  2. import {
  3. AutoSizer,
  4. CellMeasurer,
  5. CellMeasurerCache,
  6. List as ReactVirtualizedList,
  7. ListRowProps,
  8. } from 'react-virtualized';
  9. import styled from '@emotion/styled';
  10. import CompactSelect from 'sentry/components/compactSelect';
  11. import EmptyStateWarning from 'sentry/components/emptyStateWarning';
  12. import BreadcrumbIcon from 'sentry/components/events/interfaces/breadcrumbs/breadcrumb/type/icon';
  13. import HTMLCode from 'sentry/components/htmlCode';
  14. import Placeholder from 'sentry/components/placeholder';
  15. import {getDetails} from 'sentry/components/replays/breadcrumbs/utils';
  16. import PlayerRelativeTime from 'sentry/components/replays/playerRelativeTime';
  17. import {useReplayContext} from 'sentry/components/replays/replayContext';
  18. import {relativeTimeInMs} from 'sentry/components/replays/utils';
  19. import SearchBar from 'sentry/components/searchBar';
  20. import {SVGIconProps} from 'sentry/icons/svgIcon';
  21. import {t} from 'sentry/locale';
  22. import space from 'sentry/styles/space';
  23. import {getPrevReplayEvent} from 'sentry/utils/replays/getReplayEvent';
  24. import useCrumbHandlers from 'sentry/utils/replays/hooks/useCrumbHandlers';
  25. import useExtractedCrumbHtml from 'sentry/utils/replays/hooks/useExtractedCrumbHtml';
  26. import type ReplayReader from 'sentry/utils/replays/replayReader';
  27. import useDomFilters from 'sentry/views/replays/detail/domMutations/useDomFilters';
  28. import {getDomMutationsTypes} from 'sentry/views/replays/detail/domMutations/utils';
  29. import FluidHeight from 'sentry/views/replays/detail/layout/fluidHeight';
  30. type Props = {
  31. replay: ReplayReader;
  32. };
  33. // The cache is used to measure the height of each row
  34. const cache = new CellMeasurerCache({
  35. fixedWidth: true,
  36. minHeight: 82,
  37. });
  38. function DomMutations({replay}: Props) {
  39. const startTimestampMs = replay.getReplay().startedAt.getTime();
  40. const {currentTime} = useReplayContext();
  41. const {isLoading, actions} = useExtractedCrumbHtml({replay});
  42. let listRef: ReactVirtualizedList | null = null;
  43. const {
  44. items,
  45. type: filteredTypes,
  46. searchTerm,
  47. setType,
  48. setSearchTerm,
  49. } = useDomFilters({actions});
  50. const currentDomMutation = getPrevReplayEvent({
  51. items: items.map(mutation => mutation.crumb),
  52. targetTimestampMs: startTimestampMs + currentTime,
  53. allowEqual: true,
  54. allowExact: true,
  55. });
  56. const {handleMouseEnter, handleMouseLeave, handleClick} =
  57. useCrumbHandlers(startTimestampMs);
  58. useEffect(() => {
  59. // Restart cache when items changes
  60. if (listRef) {
  61. cache.clearAll();
  62. listRef?.forceUpdateGrid();
  63. }
  64. }, [items, listRef]);
  65. const renderRow = ({index, key, style, parent}: ListRowProps) => {
  66. const mutation = items[index];
  67. const {html, crumb} = mutation;
  68. const {title} = getDetails(crumb);
  69. const hasOccurred =
  70. currentTime >= relativeTimeInMs(crumb.timestamp || '', startTimestampMs);
  71. return (
  72. <CellMeasurer
  73. cache={cache}
  74. columnIndex={0}
  75. key={key}
  76. parent={parent}
  77. rowIndex={index}
  78. >
  79. <MutationListItem
  80. onMouseEnter={() => handleMouseEnter(crumb)}
  81. onMouseLeave={() => handleMouseLeave(crumb)}
  82. style={style}
  83. isCurrent={crumb.id === currentDomMutation?.id}
  84. >
  85. <IconWrapper color={crumb.color} hasOccurred={hasOccurred}>
  86. <BreadcrumbIcon type={crumb.type} />
  87. </IconWrapper>
  88. <MutationContent>
  89. <MutationDetailsContainer>
  90. <div>
  91. <TitleContainer>
  92. <Title hasOccurred={hasOccurred}>{title}</Title>
  93. </TitleContainer>
  94. <MutationMessage>{crumb.message}</MutationMessage>
  95. </div>
  96. <UnstyledButton onClick={() => handleClick(crumb)}>
  97. <PlayerRelativeTime
  98. relativeTimeMs={startTimestampMs}
  99. timestamp={crumb.timestamp}
  100. />
  101. </UnstyledButton>
  102. </MutationDetailsContainer>
  103. <CodeContainer>
  104. <HTMLCode code={html} />
  105. </CodeContainer>
  106. </MutationContent>
  107. </MutationListItem>
  108. </CellMeasurer>
  109. );
  110. };
  111. return (
  112. <MutationContainer>
  113. <MutationFilters>
  114. <CompactSelect
  115. triggerProps={{prefix: t('Event Type')}}
  116. triggerLabel={filteredTypes.length === 0 ? t('Any') : null}
  117. multiple
  118. options={getDomMutationsTypes(actions).map(value => ({value, label: value}))}
  119. size="sm"
  120. onChange={selected => setType(selected.map(_ => _.value))}
  121. value={filteredTypes}
  122. />
  123. <SearchBar
  124. size="sm"
  125. onChange={setSearchTerm}
  126. placeholder={t('Search DOM')}
  127. query={searchTerm}
  128. />
  129. </MutationFilters>
  130. {isLoading ? (
  131. <Placeholder height="200px" />
  132. ) : (
  133. <MutationList>
  134. <AutoSizer>
  135. {({width, height}) => (
  136. <ReactVirtualizedList
  137. ref={(el: ReactVirtualizedList | null) => {
  138. listRef = el;
  139. }}
  140. deferredMeasurementCache={cache}
  141. height={height}
  142. overscanRowCount={5}
  143. rowCount={items.length}
  144. noRowsRenderer={() =>
  145. actions.length === 0 ? (
  146. <EmptyStateWarning withIcon={false} small>
  147. {t('No related DOM events recorded')}
  148. </EmptyStateWarning>
  149. ) : (
  150. <EmptyStateWarning withIcon small>
  151. {t('No results found')}
  152. </EmptyStateWarning>
  153. )
  154. }
  155. rowHeight={cache.rowHeight}
  156. rowRenderer={renderRow}
  157. width={width}
  158. />
  159. )}
  160. </AutoSizer>
  161. </MutationList>
  162. )}
  163. </MutationContainer>
  164. );
  165. }
  166. const MutationFilters = styled('div')`
  167. display: grid;
  168. gap: ${space(1)};
  169. grid-template-columns: max-content 1fr;
  170. margin-bottom: ${space(1)};
  171. @media (max-width: ${p => p.theme.breakpoints.small}) {
  172. margin-top: ${space(1)};
  173. }
  174. `;
  175. const MutationContainer = styled(FluidHeight)`
  176. height: 100%;
  177. `;
  178. const MutationList = styled('ul')`
  179. list-style: none;
  180. position: relative;
  181. height: 100%;
  182. overflow: hidden;
  183. border: 1px solid ${p => p.theme.border};
  184. border-radius: ${p => p.theme.borderRadius};
  185. padding-left: 0;
  186. margin-bottom: 0;
  187. `;
  188. const MutationContent = styled('div')`
  189. overflow: hidden;
  190. width: 100%;
  191. display: flex;
  192. flex-direction: column;
  193. gap: ${space(1)};
  194. `;
  195. const MutationDetailsContainer = styled('div')`
  196. display: flex;
  197. justify-content: space-between;
  198. align-items: flex-start;
  199. flex-grow: 1;
  200. `;
  201. /**
  202. * Taken `from events/interfaces/.../breadcrumbs/types`
  203. */
  204. const IconWrapper = styled('div')<
  205. {hasOccurred?: boolean} & Required<Pick<SVGIconProps, 'color'>>
  206. >`
  207. display: flex;
  208. align-items: center;
  209. justify-content: center;
  210. width: 24px;
  211. min-width: 24px;
  212. height: 24px;
  213. border-radius: 50%;
  214. color: ${p => p.theme.white};
  215. background: ${p => (p.hasOccurred ? p.theme[p.color] ?? p.color : p.theme.purple200)};
  216. box-shadow: ${p => p.theme.dropShadowLightest};
  217. z-index: 2;
  218. `;
  219. const MutationListItem = styled('li')<{isCurrent?: boolean}>`
  220. display: flex;
  221. gap: ${space(1)};
  222. flex-grow: 1;
  223. padding: ${space(1)} ${space(1.5)};
  224. position: relative;
  225. border-bottom: 1px solid ${p => (p.isCurrent ? p.theme.purple300 : 'transparent')};
  226. &:hover {
  227. background-color: ${p => p.theme.backgroundSecondary};
  228. }
  229. /* Draw a vertical line behind the breadcrumb icon. The line connects each row together, but is truncated for the first and last items */
  230. &::after {
  231. content: '';
  232. position: absolute;
  233. left: 23.5px;
  234. top: 0;
  235. width: 1px;
  236. background: ${p => p.theme.gray200};
  237. height: 100%;
  238. }
  239. &:first-of-type::after {
  240. top: ${space(1)};
  241. bottom: 0;
  242. }
  243. &:last-of-type::after {
  244. top: 0;
  245. height: ${space(1)};
  246. }
  247. &:only-of-type::after {
  248. height: 0;
  249. }
  250. `;
  251. const TitleContainer = styled('div')`
  252. display: flex;
  253. justify-content: space-between;
  254. `;
  255. const Title = styled('span')<{hasOccurred?: boolean}>`
  256. ${p => p.theme.overflowEllipsis};
  257. text-transform: capitalize;
  258. color: ${p => (p.hasOccurred ? p.theme.gray400 : p.theme.gray300)};
  259. font-weight: bold;
  260. line-height: ${p => p.theme.text.lineHeightBody};
  261. `;
  262. const UnstyledButton = styled('button')`
  263. background: none;
  264. border: none;
  265. padding: 0;
  266. line-height: 0.75;
  267. `;
  268. const MutationMessage = styled('p')`
  269. color: ${p => p.theme.gray300};
  270. font-size: ${p => p.theme.fontSizeSmall};
  271. margin-bottom: 0;
  272. `;
  273. const CodeContainer = styled('div')`
  274. max-height: 400px;
  275. max-width: 100%;
  276. overflow: auto;
  277. `;
  278. export default DomMutations;