index.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import {useCallback, useRef} from 'react';
  2. import styled from '@emotion/styled';
  3. import {
  4. Panel as BasePanel,
  5. PanelHeader as BasePanelHeader,
  6. } from 'sentry/components/panels';
  7. import Placeholder from 'sentry/components/placeholder';
  8. import {useReplayContext} from 'sentry/components/replays/replayContext';
  9. import {relativeTimeInMs} from 'sentry/components/replays/utils';
  10. import {t} from 'sentry/locale';
  11. import space from 'sentry/styles/space';
  12. import {Crumb} from 'sentry/types/breadcrumbs';
  13. import {getPrevBreadcrumb} from 'sentry/utils/replays/getBreadcrumb';
  14. import {useCurrentItemScroller} from 'sentry/utils/replays/hooks/useCurrentItemScroller';
  15. import FluidPanel from 'sentry/views/replays/detail/layout/fluidPanel';
  16. import BreadcrumbItem from './breadcrumbItem';
  17. function CrumbPlaceholder({number}: {number: number}) {
  18. return (
  19. <BreadcrumbContainer>
  20. {[...Array(number)].map((_, i) => (
  21. <PlaceholderMargin key={i} height="53px" />
  22. ))}
  23. </BreadcrumbContainer>
  24. );
  25. }
  26. type Props = {};
  27. function Breadcrumbs({}: Props) {
  28. const {
  29. clearAllHighlights,
  30. currentHoverTime,
  31. currentTime,
  32. highlight,
  33. removeHighlight,
  34. replay,
  35. setCurrentHoverTime,
  36. setCurrentTime,
  37. } = useReplayContext();
  38. const event = replay?.getEvent();
  39. const allCrumbs = replay?.getRawCrumbs();
  40. const crumbListContainerRef = useRef<HTMLDivElement>(null);
  41. useCurrentItemScroller(crumbListContainerRef);
  42. const startTimestamp = event?.startTimestamp || 0;
  43. const isLoaded = Boolean(event);
  44. const crumbs =
  45. allCrumbs?.filter(crumb => !['console'].includes(crumb.category || '')) || [];
  46. const currentUserAction = getPrevBreadcrumb({
  47. crumbs,
  48. targetTimestampMs: startTimestamp * 1000 + currentTime,
  49. allowExact: true,
  50. });
  51. const closestUserAction =
  52. currentHoverTime !== undefined
  53. ? getPrevBreadcrumb({
  54. crumbs,
  55. targetTimestampMs: startTimestamp * 1000 + (currentHoverTime ?? 0),
  56. allowExact: true,
  57. })
  58. : undefined;
  59. const handleMouseEnter = useCallback(
  60. (item: Crumb) => {
  61. if (startTimestamp) {
  62. setCurrentHoverTime(relativeTimeInMs(item.timestamp ?? '', startTimestamp));
  63. }
  64. if (item.data && 'nodeId' in item.data) {
  65. // XXX: Kind of hacky, but mouseLeave does not fire if you move from a
  66. // crumb to a tooltip
  67. clearAllHighlights();
  68. highlight({nodeId: item.data.nodeId, annotation: item.data.label});
  69. }
  70. },
  71. [setCurrentHoverTime, startTimestamp, highlight, clearAllHighlights]
  72. );
  73. const handleMouseLeave = useCallback(
  74. (item: Crumb) => {
  75. setCurrentHoverTime(undefined);
  76. if (item.data && 'nodeId' in item.data) {
  77. removeHighlight({nodeId: item.data.nodeId});
  78. }
  79. },
  80. [setCurrentHoverTime, removeHighlight]
  81. );
  82. const handleClick = useCallback(
  83. (crumb: Crumb) => {
  84. crumb.timestamp !== undefined
  85. ? setCurrentTime(relativeTimeInMs(crumb.timestamp, startTimestamp))
  86. : null;
  87. },
  88. [setCurrentTime, startTimestamp]
  89. );
  90. const content = isLoaded ? (
  91. <BreadcrumbContainer>
  92. {crumbs.map(crumb => (
  93. <BreadcrumbItem
  94. key={crumb.id}
  95. crumb={crumb}
  96. startTimestamp={startTimestamp}
  97. isHovered={closestUserAction?.id === crumb.id}
  98. isSelected={currentUserAction?.id === crumb.id}
  99. onMouseEnter={handleMouseEnter}
  100. onMouseLeave={handleMouseLeave}
  101. onClick={handleClick}
  102. />
  103. ))}
  104. </BreadcrumbContainer>
  105. ) : (
  106. <CrumbPlaceholder number={4} />
  107. );
  108. return (
  109. <Panel>
  110. <FluidPanel
  111. bodyRef={crumbListContainerRef}
  112. title={<PanelHeader>{t('Breadcrumbs')}</PanelHeader>}
  113. >
  114. {content}
  115. </FluidPanel>
  116. </Panel>
  117. );
  118. }
  119. const BreadcrumbContainer = styled('div')`
  120. padding: ${space(0.5)};
  121. `;
  122. const Panel = styled(BasePanel)`
  123. width: 100%;
  124. height: 100%;
  125. overflow: hidden;
  126. margin-bottom: 0;
  127. `;
  128. const PanelHeader = styled(BasePanelHeader)`
  129. background-color: ${p => p.theme.background};
  130. border-bottom: 1px solid ${p => p.theme.innerBorder};
  131. font-size: ${p => p.theme.fontSizeSmall};
  132. color: ${p => p.theme.gray500};
  133. text-transform: capitalize;
  134. padding: ${space(1)} ${space(1.5)} ${space(1)};
  135. font-weight: 600;
  136. `;
  137. const PlaceholderMargin = styled(Placeholder)`
  138. margin-bottom: ${space(1)};
  139. width: auto;
  140. border-radius: ${p => p.theme.borderRadius};
  141. `;
  142. export default Breadcrumbs;