consoleLogRow.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import type {CSSProperties} from 'react';
  2. import {memo, 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. function UnmemoizedConsoleLogRow({
  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. /* Overridden in TabItemContainer, depending on *CurrentTime and *HoverTime classes */
  95. border-top: 1px solid transparent;
  96. border-bottom: 1px solid transparent;
  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. `;
  139. const ConsoleLogRow = memo(UnmemoizedConsoleLogRow);
  140. export default ConsoleLogRow;