index.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import {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 {t} from 'sentry/locale';
  10. import space from 'sentry/styles/space';
  11. import {getPrevReplayEvent} from 'sentry/utils/replays/getReplayEvent';
  12. import useCrumbHandlers from 'sentry/utils/replays/hooks/useCrumbHandlers';
  13. import {useCurrentItemScroller} from 'sentry/utils/replays/hooks/useCurrentItemScroller';
  14. import BreadcrumbItem from 'sentry/views/replays/detail/breadcrumbs/breadcrumbItem';
  15. import FluidPanel from 'sentry/views/replays/detail/layout/fluidPanel';
  16. function CrumbPlaceholder({number}: {number: number}) {
  17. return (
  18. <BreadcrumbContainer>
  19. {[...Array(number)].map((_, i) => (
  20. <PlaceholderMargin key={i} height="53px" />
  21. ))}
  22. </BreadcrumbContainer>
  23. );
  24. }
  25. type Props = {
  26. showTitle: boolean;
  27. };
  28. function Breadcrumbs({showTitle = true}: Props) {
  29. const {currentHoverTime, currentTime, replay} = useReplayContext();
  30. const replayRecord = replay?.getReplay();
  31. const allCrumbs = replay?.getRawCrumbs();
  32. const crumbListContainerRef = useRef<HTMLDivElement>(null);
  33. useCurrentItemScroller(crumbListContainerRef);
  34. const startTimestampMs = replayRecord?.startedAt.getTime() || 0;
  35. const {handleMouseEnter, handleMouseLeave, handleClick} =
  36. useCrumbHandlers(startTimestampMs);
  37. const isLoaded = Boolean(replayRecord);
  38. const crumbs =
  39. allCrumbs?.filter(crumb => !['console'].includes(crumb.category || '')) || [];
  40. const currentUserAction = getPrevReplayEvent({
  41. items: crumbs,
  42. targetTimestampMs: startTimestampMs + currentTime,
  43. allowExact: true,
  44. });
  45. const closestUserAction =
  46. currentHoverTime !== undefined
  47. ? getPrevReplayEvent({
  48. items: crumbs,
  49. targetTimestampMs: startTimestampMs + (currentHoverTime ?? 0),
  50. allowExact: true,
  51. })
  52. : undefined;
  53. const content = isLoaded ? (
  54. <BreadcrumbContainer>
  55. {crumbs.map(crumb => (
  56. <BreadcrumbItem
  57. key={crumb.id}
  58. crumb={crumb}
  59. startTimestampMs={startTimestampMs}
  60. isHovered={closestUserAction?.id === crumb.id}
  61. isSelected={currentUserAction?.id === crumb.id}
  62. onMouseEnter={handleMouseEnter}
  63. onMouseLeave={handleMouseLeave}
  64. onClick={handleClick}
  65. // We are controlling the hover state ourselves with `isHovered` prop
  66. allowHover={false}
  67. />
  68. ))}
  69. </BreadcrumbContainer>
  70. ) : (
  71. <CrumbPlaceholder number={4} />
  72. );
  73. return (
  74. <Panel>
  75. <FluidPanel
  76. bodyRef={crumbListContainerRef}
  77. title={showTitle ? <PanelHeader>{t('Breadcrumbs')}</PanelHeader> : undefined}
  78. >
  79. {content}
  80. </FluidPanel>
  81. </Panel>
  82. );
  83. }
  84. const BreadcrumbContainer = styled('div')`
  85. padding: ${space(0.5)};
  86. `;
  87. const Panel = styled(BasePanel)`
  88. width: 100%;
  89. height: 100%;
  90. overflow: hidden;
  91. margin-bottom: 0;
  92. `;
  93. const PanelHeader = styled(BasePanelHeader)`
  94. background-color: ${p => p.theme.background};
  95. border-bottom: 1px solid ${p => p.theme.innerBorder};
  96. font-size: ${p => p.theme.fontSizeSmall};
  97. color: ${p => p.theme.gray500};
  98. text-transform: capitalize;
  99. padding: ${space(1)} ${space(1.5)} ${space(1)};
  100. font-weight: 600;
  101. `;
  102. const PlaceholderMargin = styled(Placeholder)`
  103. margin-bottom: ${space(1)};
  104. width: auto;
  105. border-radius: ${p => p.theme.borderRadius};
  106. `;
  107. export default Breadcrumbs;