quickContextHovercard.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import type {ComponentProps} from 'react';
  2. import styled from '@emotion/styled';
  3. import type {Location} from 'history';
  4. import {CopyToClipboardButton} from 'sentry/components/copyToClipboardButton';
  5. import {Body, Hovercard} from 'sentry/components/hovercard';
  6. import Version from 'sentry/components/version';
  7. import {t} from 'sentry/locale';
  8. import {space} from 'sentry/styles/space';
  9. import type {Organization, Project} from 'sentry/types';
  10. import {trackAnalytics} from 'sentry/utils/analytics';
  11. import type {EventData} from 'sentry/utils/discover/eventView';
  12. import type EventView from 'sentry/utils/discover/eventView';
  13. import {getShortEventId} from 'sentry/utils/events';
  14. import {useLocation} from 'sentry/utils/useLocation';
  15. import EventContext from './eventContext';
  16. import IssueContext from './issueContext';
  17. import ReleaseContext from './releaseContext';
  18. import {NoContextWrapper} from './styles';
  19. import {ContextType} from './utils';
  20. const HOVER_DELAY: number = 400;
  21. function getHoverBody(
  22. dataRow: EventData,
  23. contextType: ContextType,
  24. organization: Organization,
  25. location?: Location,
  26. projects?: Project[],
  27. eventView?: EventView
  28. ) {
  29. switch (contextType) {
  30. case ContextType.ISSUE:
  31. return <IssueContext dataRow={dataRow} organization={organization} />;
  32. case ContextType.RELEASE:
  33. return <ReleaseContext dataRow={dataRow} organization={organization} />;
  34. case ContextType.EVENT:
  35. return (
  36. <EventContext
  37. dataRow={dataRow}
  38. organization={organization}
  39. location={location}
  40. projects={projects}
  41. eventView={eventView}
  42. />
  43. );
  44. default:
  45. return <NoContextWrapper>{t('There is no context available.')}</NoContextWrapper>;
  46. }
  47. }
  48. // NOTE: Will be adding switch cases as more contexts require headers.
  49. function getHoverHeader(
  50. dataRow: EventData,
  51. contextType: ContextType,
  52. organization: Organization
  53. ) {
  54. switch (contextType) {
  55. case ContextType.RELEASE:
  56. return (
  57. <HoverHeader
  58. title={t('Release')}
  59. organization={organization}
  60. copyLabel={<StyledVersion version={dataRow.release} truncate anchor={false} />}
  61. copyContent={dataRow.release}
  62. />
  63. );
  64. case ContextType.ISSUE:
  65. return (
  66. <HoverHeader
  67. title={t('Issue')}
  68. organization={organization}
  69. copyLabel={dataRow.issue}
  70. copyContent={dataRow.issue}
  71. />
  72. );
  73. case ContextType.EVENT:
  74. return (
  75. dataRow.id && (
  76. <HoverHeader
  77. title={t('Event ID')}
  78. organization={organization}
  79. copyLabel={getShortEventId(dataRow.id)}
  80. copyContent={dataRow.id}
  81. />
  82. )
  83. );
  84. default:
  85. return null;
  86. }
  87. }
  88. type HoverHeaderProps = {
  89. organization: Organization;
  90. title: string;
  91. copyContent?: string;
  92. copyLabel?: React.ReactNode;
  93. hideCopy?: boolean;
  94. };
  95. function HoverHeader({
  96. title,
  97. hideCopy = false,
  98. copyLabel,
  99. copyContent,
  100. organization,
  101. }: HoverHeaderProps) {
  102. return (
  103. <HoverHeaderWrapper>
  104. {title}
  105. <HoverHeaderContent>
  106. {copyLabel}
  107. {!hideCopy && copyContent && (
  108. <CopyToClipboardButton
  109. borderless
  110. data-test-id="quick-context-hover-header-copy-button"
  111. iconSize="xs"
  112. onCopy={() => {
  113. trackAnalytics('discover_v2.quick_context_header_copy', {
  114. organization,
  115. clipBoardTitle: title,
  116. });
  117. }}
  118. size="zero"
  119. text={copyContent}
  120. />
  121. )}
  122. </HoverHeaderContent>
  123. </HoverHeaderWrapper>
  124. );
  125. }
  126. interface ContextProps extends ComponentProps<typeof Hovercard> {
  127. children: React.ReactNode;
  128. contextType: ContextType;
  129. dataRow: EventData;
  130. organization: Organization;
  131. eventView?: EventView;
  132. projects?: Project[];
  133. }
  134. export function QuickContextHovercard(props: ContextProps) {
  135. const location = useLocation();
  136. const {
  137. children,
  138. dataRow,
  139. contextType,
  140. organization,
  141. projects,
  142. eventView,
  143. ...hovercardProps
  144. } = props;
  145. return (
  146. <StyledHovercard
  147. {...hovercardProps}
  148. showUnderline
  149. delay={HOVER_DELAY}
  150. header={getHoverHeader(dataRow, contextType, organization)}
  151. body={getHoverBody(
  152. dataRow,
  153. contextType,
  154. organization,
  155. location,
  156. projects,
  157. eventView
  158. )}
  159. >
  160. {children}
  161. </StyledHovercard>
  162. );
  163. }
  164. const StyledHovercard = styled(Hovercard)`
  165. ${Body} {
  166. padding: 0;
  167. }
  168. min-width: max-content;
  169. `;
  170. const HoverHeaderWrapper = styled('div')`
  171. display: flex;
  172. align-items: center;
  173. justify-content: space-between;
  174. `;
  175. const HoverHeaderContent = styled('div')`
  176. display: flex;
  177. flex: 1;
  178. align-items: center;
  179. justify-content: flex-end;
  180. gap: ${space(0.5)};
  181. `;
  182. const StyledVersion = styled(Version)`
  183. max-width: 190px;
  184. `;