index.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 {getPrevBreadcrumb} from 'sentry/utils/replays/getBreadcrumb';
  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. function Breadcrumbs({}: Props) {
  27. const {currentHoverTime, currentTime, replay} = useReplayContext();
  28. const replayRecord = replay?.getReplay();
  29. const allCrumbs = replay?.getRawCrumbs();
  30. const crumbListContainerRef = useRef<HTMLDivElement>(null);
  31. useCurrentItemScroller(crumbListContainerRef);
  32. const startTimestampMs = replayRecord?.startedAt.getTime() || 0;
  33. const {handleMouseEnter, handleMouseLeave, handleClick} =
  34. useCrumbHandlers(startTimestampMs);
  35. const isLoaded = Boolean(replayRecord);
  36. const crumbs =
  37. allCrumbs?.filter(crumb => !['console'].includes(crumb.category || '')) || [];
  38. const currentUserAction = getPrevBreadcrumb({
  39. crumbs,
  40. targetTimestampMs: startTimestampMs + currentTime,
  41. allowExact: true,
  42. });
  43. const closestUserAction =
  44. currentHoverTime !== undefined
  45. ? getPrevBreadcrumb({
  46. crumbs,
  47. targetTimestampMs: startTimestampMs + (currentHoverTime ?? 0),
  48. allowExact: true,
  49. })
  50. : undefined;
  51. const content = isLoaded ? (
  52. <BreadcrumbContainer>
  53. {crumbs.map(crumb => (
  54. <BreadcrumbItem
  55. key={crumb.id}
  56. crumb={crumb}
  57. startTimestampMs={startTimestampMs}
  58. isHovered={closestUserAction?.id === crumb.id}
  59. isSelected={currentUserAction?.id === crumb.id}
  60. onMouseEnter={handleMouseEnter}
  61. onMouseLeave={handleMouseLeave}
  62. onClick={handleClick}
  63. />
  64. ))}
  65. </BreadcrumbContainer>
  66. ) : (
  67. <CrumbPlaceholder number={4} />
  68. );
  69. return (
  70. <Panel>
  71. <FluidPanel
  72. bodyRef={crumbListContainerRef}
  73. title={<PanelHeader>{t('Breadcrumbs')}</PanelHeader>}
  74. >
  75. {content}
  76. </FluidPanel>
  77. </Panel>
  78. );
  79. }
  80. const BreadcrumbContainer = styled('div')`
  81. padding: ${space(0.5)};
  82. `;
  83. const Panel = styled(BasePanel)`
  84. width: 100%;
  85. height: 100%;
  86. overflow: hidden;
  87. margin-bottom: 0;
  88. `;
  89. const PanelHeader = styled(BasePanelHeader)`
  90. background-color: ${p => p.theme.background};
  91. border-bottom: 1px solid ${p => p.theme.innerBorder};
  92. font-size: ${p => p.theme.fontSizeSmall};
  93. color: ${p => p.theme.gray500};
  94. text-transform: capitalize;
  95. padding: ${space(1)} ${space(1.5)} ${space(1)};
  96. font-weight: 600;
  97. `;
  98. const PlaceholderMargin = styled(Placeholder)`
  99. margin-bottom: ${space(1)};
  100. width: auto;
  101. border-radius: ${p => p.theme.borderRadius};
  102. `;
  103. export default Breadcrumbs;