index.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import {useRef} from 'react';
  2. import styled from '@emotion/styled';
  3. import EmptyMessage from 'sentry/components/emptyMessage';
  4. import CompactSelect from 'sentry/components/forms/compactSelect';
  5. import {Panel} from 'sentry/components/panels';
  6. import {useReplayContext} from 'sentry/components/replays/replayContext';
  7. import {relativeTimeInMs} from 'sentry/components/replays/utils';
  8. import SearchBar from 'sentry/components/searchBar';
  9. import {t} from 'sentry/locale';
  10. import space from 'sentry/styles/space';
  11. import type {BreadcrumbTypeDefault, Crumb} from 'sentry/types/breadcrumbs';
  12. import {defined} from 'sentry/utils';
  13. import {getPrevReplayEvent} from 'sentry/utils/replays/getReplayEvent';
  14. import {useCurrentItemScroller} from 'sentry/utils/replays/hooks/useCurrentItemScroller';
  15. import ConsoleMessage from 'sentry/views/replays/detail/console/consoleMessage';
  16. import useConsoleFilters from 'sentry/views/replays/detail/console/useConsoleFilters';
  17. import {getLogLevels} from 'sentry/views/replays/detail/console/utils';
  18. import FluidHeight from 'sentry/views/replays/detail/layout/fluidHeight';
  19. interface Props {
  20. breadcrumbs: Extract<Crumb, BreadcrumbTypeDefault>[];
  21. startTimestampMs: number;
  22. }
  23. function Console({breadcrumbs, startTimestampMs = 0}: Props) {
  24. const {currentHoverTime, currentTime} = useReplayContext();
  25. const containerRef = useRef<HTMLDivElement>(null);
  26. useCurrentItemScroller(containerRef);
  27. const {items, logLevel, searchTerm, setLogLevel, setSearchTerm} = useConsoleFilters({
  28. breadcrumbs,
  29. });
  30. const currentUserAction = getPrevReplayEvent({
  31. items: breadcrumbs,
  32. targetTimestampMs: startTimestampMs + currentTime,
  33. allowExact: true,
  34. allowEqual: true,
  35. });
  36. const closestUserAction =
  37. currentHoverTime !== undefined
  38. ? getPrevReplayEvent({
  39. items: breadcrumbs,
  40. targetTimestampMs: startTimestampMs + (currentHoverTime ?? 0),
  41. allowExact: true,
  42. allowEqual: true,
  43. })
  44. : undefined;
  45. const isOcurring = (breadcrumb: Crumb, closestBreadcrumb?: Crumb): boolean => {
  46. if (!defined(currentHoverTime) || !defined(closestBreadcrumb)) {
  47. return false;
  48. }
  49. const isCurrentBreadcrumb = closestBreadcrumb.id === breadcrumb.id;
  50. // We don't want to hightlight the breadcrumb if it's more than 1 second away from the current hover time
  51. const isMoreThanASecondOfDiff =
  52. Math.trunc(currentHoverTime / 1000) >
  53. Math.trunc(
  54. relativeTimeInMs(closestBreadcrumb.timestamp || '', startTimestampMs) / 1000
  55. );
  56. return isCurrentBreadcrumb && !isMoreThanASecondOfDiff;
  57. };
  58. return (
  59. <ConsoleContainer>
  60. <ConsoleFilters>
  61. <CompactSelect
  62. triggerProps={{prefix: t('Log Level')}}
  63. triggerLabel={logLevel.length === 0 ? t('Any') : null}
  64. multiple
  65. options={getLogLevels(breadcrumbs, logLevel).map(value => ({
  66. value,
  67. label: value,
  68. }))}
  69. onChange={selected => setLogLevel(selected.map(_ => _.value))}
  70. size="sm"
  71. value={logLevel}
  72. />
  73. <SearchBar
  74. onChange={setSearchTerm}
  75. placeholder={t('Search console logs...')}
  76. size="sm"
  77. query={searchTerm}
  78. />
  79. </ConsoleFilters>
  80. <ConsoleMessageContainer ref={containerRef}>
  81. {items.length > 0 ? (
  82. <ConsoleTable>
  83. {items.map((breadcrumb, i) => {
  84. return (
  85. <ConsoleMessage
  86. isActive={closestUserAction?.id === breadcrumb.id}
  87. isCurrent={currentUserAction?.id === breadcrumb.id}
  88. isOcurring={isOcurring(breadcrumb, closestUserAction)}
  89. startTimestampMs={startTimestampMs}
  90. key={breadcrumb.id}
  91. isLast={i === breadcrumbs.length - 1}
  92. breadcrumb={breadcrumb}
  93. hasOccurred={
  94. currentTime >=
  95. relativeTimeInMs(breadcrumb?.timestamp || '', startTimestampMs)
  96. }
  97. />
  98. );
  99. })}
  100. </ConsoleTable>
  101. ) : (
  102. <StyledEmptyMessage title={t('No results found.')} />
  103. )}
  104. </ConsoleMessageContainer>
  105. </ConsoleContainer>
  106. );
  107. }
  108. const ConsoleContainer = styled(FluidHeight)`
  109. height: 100%;
  110. `;
  111. const ConsoleFilters = styled('div')`
  112. display: grid;
  113. gap: ${space(1)};
  114. grid-template-columns: max-content 1fr;
  115. margin-bottom: ${space(1)};
  116. @media (max-width: ${p => p.theme.breakpoints.small}) {
  117. margin-top: ${space(1)};
  118. }
  119. `;
  120. const ConsoleMessageContainer = styled(FluidHeight)`
  121. overflow: auto;
  122. border-radius: ${p => p.theme.borderRadius};
  123. border: 1px solid ${p => p.theme.border};
  124. box-shadow: ${p => p.theme.dropShadowLight};
  125. `;
  126. const StyledEmptyMessage = styled(EmptyMessage)`
  127. align-items: center;
  128. `;
  129. const ConsoleTable = styled(Panel)`
  130. display: grid;
  131. grid-template-columns: max-content auto max-content;
  132. width: 100%;
  133. font-family: ${p => p.theme.text.familyMono};
  134. font-size: 0.8em;
  135. border: none;
  136. box-shadow: none;
  137. margin-bottom: 0;
  138. `;
  139. export default Console;