domMutationRow.tsx 5.2 KB

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