index.tsx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 EmptyStateWarning from 'sentry/components/emptyStateWarning';
  11. import BreadcrumbIcon from 'sentry/components/events/interfaces/breadcrumbs/breadcrumb/type/icon';
  12. import CompactSelect from 'sentry/components/forms/compactSelect';
  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. <EmptyStateWarning withIcon={false} small>
  146. {t('No related DOM Events recorded')}
  147. </EmptyStateWarning>
  148. )}
  149. rowHeight={cache.rowHeight}
  150. rowRenderer={renderRow}
  151. width={width}
  152. />
  153. )}
  154. </AutoSizer>
  155. </MutationList>
  156. )}
  157. </MutationContainer>
  158. );
  159. }
  160. const MutationFilters = styled('div')`
  161. display: grid;
  162. gap: ${space(1)};
  163. grid-template-columns: max-content 1fr;
  164. margin-bottom: ${space(1)};
  165. @media (max-width: ${p => p.theme.breakpoints.small}) {
  166. margin-top: ${space(1)};
  167. }
  168. `;
  169. const MutationContainer = styled(FluidHeight)`
  170. height: 100%;
  171. `;
  172. const MutationList = styled('ul')`
  173. list-style: none;
  174. position: relative;
  175. height: 100%;
  176. overflow: hidden;
  177. border: 1px solid ${p => p.theme.border};
  178. border-radius: ${p => p.theme.borderRadius};
  179. padding-left: 0;
  180. margin-bottom: 0;
  181. `;
  182. const MutationContent = styled('div')`
  183. overflow: hidden;
  184. width: 100%;
  185. display: flex;
  186. flex-direction: column;
  187. gap: ${space(1)};
  188. `;
  189. const MutationDetailsContainer = styled('div')`
  190. display: flex;
  191. justify-content: space-between;
  192. align-items: flex-start;
  193. flex-grow: 1;
  194. `;
  195. /**
  196. * Taken `from events/interfaces/.../breadcrumbs/types`
  197. */
  198. const IconWrapper = styled('div')<
  199. {hasOccurred?: boolean} & Required<Pick<SVGIconProps, 'color'>>
  200. >`
  201. display: flex;
  202. align-items: center;
  203. justify-content: center;
  204. width: 24px;
  205. min-width: 24px;
  206. height: 24px;
  207. border-radius: 50%;
  208. color: ${p => p.theme.white};
  209. background: ${p => (p.hasOccurred ? p.theme[p.color] ?? p.color : p.theme.purple200)};
  210. box-shadow: ${p => p.theme.dropShadowLightest};
  211. z-index: 2;
  212. `;
  213. const MutationListItem = styled('li')<{isCurrent?: boolean}>`
  214. display: flex;
  215. gap: ${space(1)};
  216. flex-grow: 1;
  217. padding: ${space(1)} ${space(1.5)};
  218. position: relative;
  219. border-bottom: 1px solid ${p => (p.isCurrent ? p.theme.purple300 : 'transparent')};
  220. &:hover {
  221. background-color: ${p => p.theme.backgroundSecondary};
  222. }
  223. /* Draw a vertical line behind the breadcrumb icon. The line connects each row together, but is truncated for the first and last items */
  224. &::after {
  225. content: '';
  226. position: absolute;
  227. left: 23.5px;
  228. top: 0;
  229. width: 1px;
  230. background: ${p => p.theme.gray200};
  231. height: 100%;
  232. }
  233. &:first-of-type::after {
  234. top: ${space(1)};
  235. bottom: 0;
  236. }
  237. &:last-of-type::after {
  238. top: 0;
  239. height: ${space(1)};
  240. }
  241. &:only-of-type::after {
  242. height: 0;
  243. }
  244. `;
  245. const TitleContainer = styled('div')`
  246. display: flex;
  247. justify-content: space-between;
  248. `;
  249. const Title = styled('span')<{hasOccurred?: boolean}>`
  250. ${p => p.theme.overflowEllipsis};
  251. text-transform: capitalize;
  252. color: ${p => (p.hasOccurred ? p.theme.gray400 : p.theme.gray300)};
  253. font-weight: bold;
  254. line-height: ${p => p.theme.text.lineHeightBody};
  255. `;
  256. const UnstyledButton = styled('button')`
  257. background: none;
  258. border: none;
  259. padding: 0;
  260. line-height: 0.75;
  261. `;
  262. const MutationMessage = styled('p')`
  263. color: ${p => p.theme.gray300};
  264. font-size: ${p => p.theme.fontSizeSmall};
  265. margin-bottom: 0;
  266. `;
  267. const CodeContainer = styled('div')`
  268. max-height: 400px;
  269. max-width: 100%;
  270. `;
  271. export default DomMutations;