domMutationRow.tsx 4.7 KB

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