consoleLogRow.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import type {CSSProperties} from 'react';
  2. import {useCallback} from 'react';
  3. import styled from '@emotion/styled';
  4. import classNames from 'classnames';
  5. import ErrorBoundary from 'sentry/components/errorBoundary';
  6. import {Tooltip} from 'sentry/components/tooltip';
  7. import {IconClose, IconInfo, IconWarning} from 'sentry/icons';
  8. import {space} from 'sentry/styles/space';
  9. import {BreadcrumbLevelType} from 'sentry/types/breadcrumbs';
  10. import type useCrumbHandlers from 'sentry/utils/replays/hooks/useCrumbHandlers';
  11. import type {BreadcrumbFrame, ConsoleFrame} from 'sentry/utils/replays/types';
  12. import MessageFormatter from 'sentry/views/replays/detail/console/messageFormatter';
  13. import TimestampButton from 'sentry/views/replays/detail/timestampButton';
  14. interface Props extends ReturnType<typeof useCrumbHandlers> {
  15. currentHoverTime: number | undefined;
  16. currentTime: number;
  17. frame: BreadcrumbFrame;
  18. index: number;
  19. onDimensionChange: (
  20. index: number,
  21. path: string,
  22. expandedState: Record<string, boolean>
  23. ) => void;
  24. startTimestampMs: number;
  25. style: CSSProperties;
  26. expandPaths?: string[];
  27. }
  28. export default function ConsoleLogRow({
  29. currentHoverTime,
  30. currentTime,
  31. expandPaths,
  32. frame,
  33. onMouseEnter,
  34. onMouseLeave,
  35. index,
  36. onClickTimestamp,
  37. onDimensionChange,
  38. startTimestampMs,
  39. style,
  40. }: Props) {
  41. const handleDimensionChange = useCallback(
  42. (path, expandedState) => onDimensionChange?.(index, path, expandedState),
  43. [onDimensionChange, index]
  44. );
  45. const hasOccurred = currentTime >= frame.offsetMs;
  46. const isBeforeHover =
  47. currentHoverTime === undefined || currentHoverTime >= frame.offsetMs;
  48. return (
  49. <ConsoleLog
  50. className={classNames({
  51. beforeCurrentTime: hasOccurred,
  52. afterCurrentTime: !hasOccurred,
  53. beforeHoverTime: currentHoverTime !== undefined && isBeforeHover,
  54. afterHoverTime: currentHoverTime !== undefined && !isBeforeHover,
  55. })}
  56. hasOccurred={hasOccurred}
  57. level={(frame as ConsoleFrame).level}
  58. onMouseEnter={() => onMouseEnter(frame)}
  59. onMouseLeave={() => onMouseLeave(frame)}
  60. style={style}
  61. >
  62. <ConsoleLevelIcon level={(frame as ConsoleFrame).level} />
  63. <Message>
  64. <ErrorBoundary mini>
  65. <MessageFormatter
  66. expandPaths={expandPaths}
  67. frame={frame}
  68. onExpand={handleDimensionChange}
  69. />
  70. </ErrorBoundary>
  71. </Message>
  72. <TimestampButton
  73. onClick={event => {
  74. event.stopPropagation();
  75. onClickTimestamp(frame);
  76. }}
  77. startTimestampMs={startTimestampMs}
  78. timestampMs={frame.timestampMs}
  79. />
  80. </ConsoleLog>
  81. );
  82. }
  83. const ConsoleLog = styled('div')<{
  84. hasOccurred: boolean;
  85. level: undefined | string;
  86. }>`
  87. display: grid;
  88. grid-template-columns: 12px 1fr max-content;
  89. gap: ${space(0.75)};
  90. align-items: baseline;
  91. padding: ${space(0.5)} ${space(1)};
  92. font-size: ${p => p.theme.fontSizeSmall};
  93. background-color: ${p =>
  94. ['warning', 'error'].includes(String(p.level))
  95. ? p.theme.alert[String(p.level)].backgroundLight
  96. : 'inherit'};
  97. color: ${p => p.theme.gray400};
  98. /*
  99. Show the timestamp button "Play" icon when we hover the row.
  100. This is a really generic selector that could find many things, but for now it
  101. only targets the one thing that we expect.
  102. */
  103. &:hover button > svg {
  104. visibility: visible;
  105. }
  106. `;
  107. const ICONS = {
  108. [BreadcrumbLevelType.ERROR]: (
  109. <Tooltip title={BreadcrumbLevelType.ERROR}>
  110. <IconClose size="xs" color="red400" isCircled />
  111. </Tooltip>
  112. ),
  113. [BreadcrumbLevelType.WARNING]: (
  114. <Tooltip title={BreadcrumbLevelType.WARNING}>
  115. <IconWarning color="yellow400" size="xs" />
  116. </Tooltip>
  117. ),
  118. [BreadcrumbLevelType.INFO]: (
  119. <Tooltip title={BreadcrumbLevelType.INFO}>
  120. <IconInfo color="gray400" size="xs" />
  121. </Tooltip>
  122. ),
  123. };
  124. const MediumFontSize = styled('span')`
  125. font-size: ${p => p.theme.fontSizeMedium};
  126. `;
  127. function ConsoleLevelIcon({level}: {level: string | undefined}) {
  128. return level && level in ICONS ? (
  129. <MediumFontSize>{ICONS[level]}</MediumFontSize>
  130. ) : (
  131. <i />
  132. );
  133. }
  134. const Message = styled('div')`
  135. font-family: ${p => p.theme.text.familyMono};
  136. white-space: pre-wrap;
  137. word-break: break-word;
  138. `;