consoleLogRow.tsx 4.2 KB

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