index.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import {Fragment, useCallback} from 'react';
  2. import styled from '@emotion/styled';
  3. import {
  4. Panel as BasePanel,
  5. PanelBody as BasePanelBody,
  6. PanelHeader as BasePanelHeader,
  7. } from 'sentry/components/panels';
  8. import Placeholder from 'sentry/components/placeholder';
  9. import {useReplayContext} from 'sentry/components/replays/replayContext';
  10. import {relativeTimeInMs} from 'sentry/components/replays/utils';
  11. import {t} from 'sentry/locale';
  12. import space from 'sentry/styles/space';
  13. import {Crumb} from 'sentry/types/breadcrumbs';
  14. import {EventTransaction} from 'sentry/types/event';
  15. import {getPrevBreadcrumb} from 'sentry/utils/replays/getBreadcrumb';
  16. import BreadcrumbItem from './breadcrumbItem';
  17. function CrumbPlaceholder({number}: {number: number}) {
  18. return (
  19. <Fragment>
  20. {[...Array(number)].map((_, i) => (
  21. <PlaceholderMargin key={i} height="40px" />
  22. ))}
  23. </Fragment>
  24. );
  25. }
  26. type Props = {
  27. /**
  28. * Raw breadcrumbs, `undefined` means it is still loading
  29. */
  30. crumbs: Crumb[] | undefined;
  31. /**
  32. * Root replay event, `undefined` means it is still loading
  33. */
  34. event: EventTransaction | undefined;
  35. };
  36. function Breadcrumbs({event, crumbs: allCrumbs}: Props) {
  37. const {
  38. setCurrentTime,
  39. setCurrentHoverTime,
  40. currentHoverTime,
  41. currentTime,
  42. highlight,
  43. removeHighlight,
  44. clearAllHighlights,
  45. } = useReplayContext();
  46. const startTimestamp = event?.startTimestamp || 0;
  47. const isLoaded = Boolean(event);
  48. const crumbs =
  49. allCrumbs?.filter(crumb => !['console'].includes(crumb.category || '')) || [];
  50. const currentUserAction = getPrevBreadcrumb({
  51. crumbs,
  52. targetTimestampMs: startTimestamp * 1000 + currentTime,
  53. allowExact: true,
  54. });
  55. const closestUserAction =
  56. currentHoverTime !== undefined
  57. ? getPrevBreadcrumb({
  58. crumbs,
  59. targetTimestampMs: startTimestamp * 1000 + (currentHoverTime ?? 0),
  60. allowExact: true,
  61. })
  62. : undefined;
  63. const handleMouseEnter = useCallback(
  64. (item: Crumb) => {
  65. if (startTimestamp) {
  66. setCurrentHoverTime(relativeTimeInMs(item.timestamp ?? '', startTimestamp));
  67. }
  68. if (item.data && 'nodeId' in item.data) {
  69. // XXX: Kind of hacky, but mouseLeave does not fire if you move from a
  70. // crumb to a tooltip
  71. clearAllHighlights();
  72. highlight({nodeId: item.data.nodeId});
  73. }
  74. },
  75. [setCurrentHoverTime, startTimestamp, highlight, clearAllHighlights]
  76. );
  77. const handleMouseLeave = useCallback(
  78. (item: Crumb) => {
  79. setCurrentHoverTime(undefined);
  80. if (item.data && 'nodeId' in item.data) {
  81. removeHighlight({nodeId: item.data.nodeId});
  82. }
  83. },
  84. [setCurrentHoverTime, removeHighlight]
  85. );
  86. const handleClick = useCallback(
  87. (crumb: Crumb) => {
  88. crumb.timestamp !== undefined
  89. ? setCurrentTime(relativeTimeInMs(crumb.timestamp, startTimestamp))
  90. : null;
  91. },
  92. [setCurrentTime, startTimestamp]
  93. );
  94. return (
  95. <Panel>
  96. <PanelHeader>{t('Breadcrumbs')}</PanelHeader>
  97. <PanelBody>
  98. {!isLoaded && <CrumbPlaceholder number={4} />}
  99. {isLoaded &&
  100. crumbs.map(crumb => (
  101. <BreadcrumbItem
  102. key={crumb.id}
  103. crumb={crumb}
  104. startTimestamp={startTimestamp}
  105. isHovered={closestUserAction?.id === crumb.id}
  106. isSelected={currentUserAction?.id === crumb.id}
  107. onMouseEnter={handleMouseEnter}
  108. onMouseLeave={handleMouseLeave}
  109. onClick={handleClick}
  110. />
  111. ))}
  112. </PanelBody>
  113. </Panel>
  114. );
  115. }
  116. // FYI: Since the Replay Player has dynamic height based
  117. // on the width of the window,
  118. // height: 0; will helps us to reset the height
  119. // min-height: 100%; will helps us to grow at the same height of Player
  120. const Panel = styled(BasePanel)`
  121. width: 100%;
  122. display: grid;
  123. grid-template-rows: auto 1fr;
  124. height: 0;
  125. min-height: 100%;
  126. @media only screen and (max-width: ${p => p.theme.breakpoints.large}) {
  127. height: fit-content;
  128. max-height: 400px;
  129. margin-top: ${space(2)};
  130. }
  131. `;
  132. const PanelHeader = styled(BasePanelHeader)`
  133. background-color: ${p => p.theme.background};
  134. border-bottom: none;
  135. font-size: ${p => p.theme.fontSizeSmall};
  136. color: ${p => p.theme.gray300};
  137. text-transform: capitalize;
  138. padding: ${space(1.5)} ${space(2)} ${space(0.5)};
  139. `;
  140. const PanelBody = styled(BasePanelBody)`
  141. overflow-y: auto;
  142. `;
  143. const PlaceholderMargin = styled(Placeholder)`
  144. margin: ${space(1)} ${space(1.5)};
  145. width: auto;
  146. `;
  147. export default Breadcrumbs;