domMutationRow.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import {CSSProperties, useCallback} from 'react';
  2. import styled from '@emotion/styled';
  3. import beautify from 'js-beautify';
  4. import {CodeSnippet} from 'sentry/components/codeSnippet';
  5. import BreadcrumbIcon from 'sentry/components/events/interfaces/breadcrumbs/breadcrumb/type/icon';
  6. import {getDetails} from 'sentry/components/replays/breadcrumbs/utils';
  7. import {useReplayContext} from 'sentry/components/replays/replayContext';
  8. import {relativeTimeInMs} from 'sentry/components/replays/utils';
  9. import {SVGIconProps} from 'sentry/icons/svgIcon';
  10. import {space} from 'sentry/styles/space';
  11. import {getPrevReplayEvent} from 'sentry/utils/replays/getReplayEvent';
  12. import useCrumbHandlers from 'sentry/utils/replays/hooks/useCrumbHandlers';
  13. import type {Extraction} from 'sentry/utils/replays/hooks/useExtractedCrumbHtml';
  14. import TimestampButton from 'sentry/views/replays/detail/timestampButton';
  15. type Props = {
  16. mutation: Extraction;
  17. mutations: Extraction[];
  18. startTimestampMs: number;
  19. style: CSSProperties;
  20. };
  21. function DomMutationRow({mutation, mutations, startTimestampMs, style}: Props) {
  22. const {html, crumb: breadcrumb} = mutation;
  23. const {currentTime, currentHoverTime} = useReplayContext();
  24. const {handleMouseEnter, handleMouseLeave, handleClick} =
  25. useCrumbHandlers(startTimestampMs);
  26. const onClickTimestamp = useCallback(
  27. () => handleClick(breadcrumb),
  28. [handleClick, breadcrumb]
  29. );
  30. const onMouseEnter = useCallback(
  31. () => handleMouseEnter(breadcrumb),
  32. [handleMouseEnter, breadcrumb]
  33. );
  34. const onMouseLeave = useCallback(
  35. () => handleMouseLeave(breadcrumb),
  36. [handleMouseLeave, breadcrumb]
  37. );
  38. const breadcrumbs = mutations.map(({crumb}) => crumb);
  39. const current = getPrevReplayEvent({
  40. items: breadcrumbs,
  41. targetTimestampMs: startTimestampMs + currentTime,
  42. allowEqual: true,
  43. allowExact: true,
  44. });
  45. const hovered = currentHoverTime
  46. ? getPrevReplayEvent({
  47. items: breadcrumbs,
  48. targetTimestampMs: startTimestampMs + currentHoverTime,
  49. allowEqual: true,
  50. allowExact: true,
  51. })
  52. : undefined;
  53. const hasOccurred =
  54. currentTime >= relativeTimeInMs(breadcrumb.timestamp || 0, startTimestampMs);
  55. const isCurrent = breadcrumb.id === current?.id;
  56. const isHovered = breadcrumb.id === hovered?.id;
  57. const {title} = getDetails(breadcrumb);
  58. return (
  59. <MutationListItem
  60. onMouseEnter={onMouseEnter}
  61. onMouseLeave={onMouseLeave}
  62. style={style}
  63. isCurrent={isCurrent}
  64. isHovered={isHovered}
  65. >
  66. <IconWrapper color={breadcrumb.color} hasOccurred={hasOccurred}>
  67. <BreadcrumbIcon type={breadcrumb.type} />
  68. </IconWrapper>
  69. <List>
  70. <Row>
  71. <Title hasOccurred={hasOccurred}>{title}</Title>
  72. <TimestampButton
  73. onClick={onClickTimestamp}
  74. startTimestampMs={startTimestampMs}
  75. timestampMs={breadcrumb.timestamp || ''}
  76. />
  77. </Row>
  78. <Selector>{breadcrumb.message}</Selector>
  79. <CodeContainer>
  80. <CodeSnippet language="html" hideCopyButton>
  81. {beautify.html(html, {indent_size: 2})}
  82. </CodeSnippet>
  83. </CodeContainer>
  84. </List>
  85. </MutationListItem>
  86. );
  87. }
  88. const MutationListItem = styled('div')<{
  89. isCurrent: boolean;
  90. isHovered: boolean;
  91. }>`
  92. display: flex;
  93. gap: ${space(1)};
  94. padding: ${space(1)} ${space(1.5)};
  95. border-bottom: 1px solid
  96. ${p =>
  97. p.isCurrent ? p.theme.purple300 : p.isHovered ? p.theme.purple200 : 'transparent'};
  98. &:hover {
  99. background-color: ${p => p.theme.hover};
  100. }
  101. /*
  102. Draw a vertical line behind the breadcrumb icon.
  103. The line connects each row together, but is truncated for the first and last items.
  104. */
  105. position: relative;
  106. &::after {
  107. content: '';
  108. position: absolute;
  109. top: 0;
  110. /* $padding + $half_icon_width - $space_for_the_line */
  111. left: calc(${space(1.5)} + (24px / 2) - 1px);
  112. width: 1px;
  113. height: 100%;
  114. background: ${p => p.theme.gray200};
  115. }
  116. &:first-of-type::after {
  117. top: ${space(1)};
  118. bottom: 0;
  119. }
  120. &:last-of-type::after {
  121. top: 0;
  122. height: ${space(1)};
  123. }
  124. &:only-of-type::after {
  125. height: 0;
  126. }
  127. `;
  128. const List = styled('div')`
  129. display: flex;
  130. flex-direction: column;
  131. overflow: hidden;
  132. width: 100%;
  133. `;
  134. const Row = styled('div')`
  135. display: flex;
  136. flex-direction: row;
  137. `;
  138. /**
  139. * Taken `from events/interfaces/.../breadcrumbs/types`
  140. */
  141. const IconWrapper = styled('div')<
  142. {hasOccurred: boolean} & Required<Pick<SVGIconProps, 'color'>>
  143. >`
  144. display: flex;
  145. align-items: center;
  146. justify-content: center;
  147. width: 24px;
  148. min-width: 24px;
  149. height: 24px;
  150. border-radius: 50%;
  151. color: ${p => p.theme.white};
  152. background: ${p => (p.hasOccurred ? p.theme[p.color] ?? p.color : p.theme.purple200)};
  153. /* Make sure the icon is above the line through the back */
  154. z-index: ${p => p.theme.zIndex.initial};
  155. `;
  156. const Title = styled('span')<{hasOccurred?: boolean}>`
  157. color: ${p => (p.hasOccurred ? p.theme.gray400 : p.theme.gray300)};
  158. font-weight: bold;
  159. line-height: ${p => p.theme.text.lineHeightBody};
  160. text-transform: capitalize;
  161. ${p => p.theme.overflowEllipsis};
  162. `;
  163. const Selector = styled('p')`
  164. color: ${p => p.theme.gray300};
  165. font-size: ${p => p.theme.fontSizeSmall};
  166. margin-bottom: 0;
  167. `;
  168. const CodeContainer = styled('div')`
  169. margin-top: ${space(1)};
  170. max-height: 400px;
  171. max-width: 100%;
  172. overflow: auto;
  173. `;
  174. export default DomMutationRow;