domMutationRow.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import {CSSProperties, useCallback} from 'react';
  2. import styled from '@emotion/styled';
  3. import classNames from 'classnames';
  4. import beautify from 'js-beautify';
  5. import {CodeSnippet} from 'sentry/components/codeSnippet';
  6. import BreadcrumbIcon from 'sentry/components/events/interfaces/breadcrumbs/breadcrumb/type/icon';
  7. import {space} from 'sentry/styles/space';
  8. import type {Extraction} from 'sentry/utils/replays/extractDomNodes';
  9. import {getBreadcrumbType, getColor, getTitle} from 'sentry/utils/replays/frame';
  10. import useCrumbHandlers from 'sentry/utils/replays/hooks/useCrumbHandlers';
  11. import IconWrapper from 'sentry/views/replays/detail/iconWrapper';
  12. import TimestampButton from 'sentry/views/replays/detail/timestampButton';
  13. type Props = {
  14. currentHoverTime: number | undefined;
  15. currentTime: number;
  16. mutation: Extraction;
  17. startTimestampMs: number;
  18. style: CSSProperties;
  19. };
  20. function DomMutationRow({
  21. currentHoverTime,
  22. currentTime,
  23. mutation,
  24. startTimestampMs,
  25. style,
  26. }: Props) {
  27. const {html, frame: breadcrumb} = mutation;
  28. const {handleMouseEnter, handleMouseLeave, handleClick} =
  29. useCrumbHandlers(startTimestampMs);
  30. const onClickTimestamp = useCallback(
  31. () => handleClick(breadcrumb),
  32. [handleClick, breadcrumb]
  33. );
  34. const onMouseEnter = useCallback(
  35. () => handleMouseEnter(breadcrumb),
  36. [handleMouseEnter, breadcrumb]
  37. );
  38. const onMouseLeave = useCallback(
  39. () => handleMouseLeave(breadcrumb),
  40. [handleMouseLeave, breadcrumb]
  41. );
  42. const hasOccurred = currentTime >= breadcrumb.offsetMs;
  43. const isBeforeHover =
  44. currentHoverTime === undefined || currentHoverTime >= breadcrumb.offsetMs;
  45. const color = getColor(breadcrumb);
  46. const title = getTitle(breadcrumb);
  47. const type = getBreadcrumbType(breadcrumb);
  48. return (
  49. <MutationListItem
  50. className={classNames({
  51. beforeCurrentTime: hasOccurred,
  52. afterCurrentTime: !hasOccurred,
  53. beforeHoverTime: currentHoverTime !== undefined && isBeforeHover,
  54. afterHoverTime: currentHoverTime !== undefined && !isBeforeHover,
  55. })}
  56. onMouseEnter={onMouseEnter}
  57. onMouseLeave={onMouseLeave}
  58. style={style}
  59. >
  60. <IconWrapper color={color} hasOccurred={hasOccurred}>
  61. <BreadcrumbIcon type={type} />
  62. </IconWrapper>
  63. <List>
  64. <Row>
  65. <Title hasOccurred={hasOccurred}>{title}</Title>
  66. <TimestampButton
  67. onClick={onClickTimestamp}
  68. startTimestampMs={startTimestampMs}
  69. timestampMs={breadcrumb.timestampMs}
  70. />
  71. </Row>
  72. {/* @ts-expect-error */}
  73. <Selector>{breadcrumb.message ?? ''}</Selector>
  74. <CodeContainer>
  75. <CodeSnippet language="html" hideCopyButton>
  76. {beautify.html(html, {indent_size: 2})}
  77. </CodeSnippet>
  78. </CodeContainer>
  79. </List>
  80. </MutationListItem>
  81. );
  82. }
  83. const MutationListItem = styled('div')`
  84. display: flex;
  85. gap: ${space(1)};
  86. padding: ${space(1)} ${space(1.5)};
  87. /* Overridden in TabItemContainer, depending on *CurrentTime and *HoverTime classes */
  88. border-top: 1px solid transparent;
  89. border-bottom: 1px solid transparent;
  90. &:hover {
  91. background-color: ${p => p.theme.hover};
  92. }
  93. /*
  94. Draw a vertical line behind the breadcrumb icon.
  95. The line connects each row together, but is truncated for the first and last items.
  96. */
  97. position: relative;
  98. &::after {
  99. content: '';
  100. position: absolute;
  101. top: 0;
  102. /* $padding + $half_icon_width - $space_for_the_line */
  103. left: calc(${space(1.5)} + (24px / 2) - 1px);
  104. width: 1px;
  105. height: 100%;
  106. background: ${p => p.theme.gray200};
  107. }
  108. &:first-of-type::after {
  109. top: ${space(1)};
  110. bottom: 0;
  111. }
  112. &:last-of-type::after {
  113. top: 0;
  114. height: ${space(1)};
  115. }
  116. &:only-of-type::after {
  117. height: 0;
  118. }
  119. `;
  120. const List = styled('div')`
  121. display: flex;
  122. flex-direction: column;
  123. overflow: hidden;
  124. width: 100%;
  125. `;
  126. const Row = styled('div')`
  127. display: flex;
  128. flex-direction: row;
  129. font-size: ${p => p.theme.fontSizeSmall};
  130. `;
  131. const Title = styled('span')<{hasOccurred?: boolean}>`
  132. color: ${p => (p.hasOccurred ? p.theme.gray400 : p.theme.gray300)};
  133. font-size: ${p => p.theme.fontSizeMedium};
  134. font-weight: bold;
  135. line-height: ${p => p.theme.text.lineHeightBody};
  136. text-transform: capitalize;
  137. ${p => p.theme.overflowEllipsis};
  138. `;
  139. const Selector = styled('p')`
  140. color: ${p => p.theme.gray300};
  141. font-size: ${p => p.theme.fontSizeSmall};
  142. margin-bottom: 0;
  143. `;
  144. const CodeContainer = styled('div')`
  145. margin-top: ${space(1)};
  146. max-height: 400px;
  147. max-width: 100%;
  148. overflow: auto;
  149. `;
  150. export default DomMutationRow;