breadcrumbItem.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import {memo, useCallback} from 'react';
  2. import styled from '@emotion/styled';
  3. import BreadcrumbIcon from 'sentry/components/events/interfaces/breadcrumbs/breadcrumb/type/icon';
  4. import {PanelItem} from 'sentry/components/panels';
  5. import {getDetails} from 'sentry/components/replays/breadcrumbs/utils';
  6. import PlayerRelativeTime from 'sentry/components/replays/playerRelativeTime';
  7. import {SVGIconProps} from 'sentry/icons/svgIcon';
  8. import space from 'sentry/styles/space';
  9. import type {Crumb} from 'sentry/types/breadcrumbs';
  10. type MouseCallback = (crumb: Crumb, e: React.MouseEvent<HTMLElement>) => void;
  11. interface Props {
  12. crumb: Crumb;
  13. isHovered: boolean;
  14. isSelected: boolean;
  15. onClick: MouseCallback;
  16. onMouseEnter: MouseCallback;
  17. onMouseLeave: MouseCallback;
  18. startTimestamp: number;
  19. }
  20. function BreadcrumbItem({
  21. crumb,
  22. isHovered,
  23. isSelected,
  24. startTimestamp,
  25. onMouseEnter,
  26. onMouseLeave,
  27. onClick,
  28. }: Props) {
  29. const {title, description} = getDetails(crumb);
  30. const handleMouseEnter = useCallback(
  31. (e: React.MouseEvent<HTMLElement>) => onMouseEnter(crumb, e),
  32. [onMouseEnter, crumb]
  33. );
  34. const handleMouseLeave = useCallback(
  35. (e: React.MouseEvent<HTMLElement>) => onMouseLeave(crumb, e),
  36. [onMouseLeave, crumb]
  37. );
  38. const handleClick = useCallback(
  39. (e: React.MouseEvent<HTMLElement>) => onClick(crumb, e),
  40. [onClick, crumb]
  41. );
  42. return (
  43. <CrumbItem
  44. as="button"
  45. onMouseEnter={handleMouseEnter}
  46. onMouseLeave={handleMouseLeave}
  47. onClick={handleClick}
  48. isHovered={isHovered}
  49. isSelected={isSelected}
  50. aria-current={isSelected}
  51. >
  52. <IconWrapper color={crumb.color}>
  53. <BreadcrumbIcon type={crumb.type} />
  54. </IconWrapper>
  55. <CrumbDetails>
  56. <TitleContainer>
  57. <Title>{title}</Title>
  58. <PlayerRelativeTime relativeTime={startTimestamp} timestamp={crumb.timestamp} />
  59. </TitleContainer>
  60. <Description title={description}>{description}</Description>
  61. </CrumbDetails>
  62. </CrumbItem>
  63. );
  64. }
  65. const CrumbDetails = styled('div')`
  66. display: flex;
  67. flex-direction: column;
  68. overflow: hidden;
  69. `;
  70. const TitleContainer = styled('div')`
  71. display: flex;
  72. justify-content: space-between;
  73. gap: ${space(1)};
  74. `;
  75. const Title = styled('span')`
  76. ${p => p.theme.overflowEllipsis};
  77. text-transform: capitalize;
  78. font-weight: 600;
  79. color: ${p => p.theme.gray400};
  80. line-height: ${p => p.theme.text.lineHeightBody};
  81. `;
  82. const Description = styled('span')`
  83. ${p => p.theme.overflowEllipsis};
  84. font-size: 0.7rem;
  85. font-variant-numeric: tabular-nums;
  86. line-height: ${p => p.theme.text.lineHeightBody};
  87. color: ${p => p.theme.subText};
  88. `;
  89. type CrumbItemProps = {
  90. isHovered: boolean;
  91. isSelected: boolean;
  92. };
  93. const CrumbItem = styled(PanelItem)<CrumbItemProps>`
  94. display: grid;
  95. grid-template-columns: max-content auto;
  96. align-items: flex-start;
  97. gap: ${space(1)};
  98. width: 100%;
  99. font-size: ${p => p.theme.fontSizeMedium};
  100. background: transparent;
  101. padding: ${space(1)};
  102. text-align: left;
  103. border: none;
  104. position: relative;
  105. ${p => p.isSelected && `background-color: ${p.theme.purple100};`}
  106. ${p => p.isHovered && `background-color: ${p.theme.surface100};`}
  107. border-radius: ${p => p.theme.borderRadius};
  108. /* Draw a vertical line behind the breadcrumb icon. The line connects each row together, but is truncated for the first and last items */
  109. &::after {
  110. content: '';
  111. position: absolute;
  112. left: 19.5px;
  113. width: 1px;
  114. background: ${p => p.theme.gray200};
  115. height: 100%;
  116. }
  117. &:first-of-type::after {
  118. top: ${space(1)};
  119. bottom: 0;
  120. }
  121. &:last-of-type::after {
  122. top: 0;
  123. height: ${space(1)};
  124. }
  125. `;
  126. /**
  127. * Taken `from events/interfaces/.../breadcrumbs/types`
  128. */
  129. const IconWrapper = styled('div')<Required<Pick<SVGIconProps, 'color'>>>`
  130. display: flex;
  131. align-items: center;
  132. justify-content: center;
  133. width: 24px;
  134. height: 24px;
  135. border-radius: 50%;
  136. color: ${p => p.theme.white};
  137. background: ${p => p.theme[p.color] ?? p.color};
  138. box-shadow: ${p => p.theme.dropShadowLightest};
  139. position: relative;
  140. z-index: ${p => p.theme.zIndex.initial};
  141. `;
  142. const MemoizedBreadcrumbItem = memo(BreadcrumbItem);
  143. export default MemoizedBreadcrumbItem;