quickContextHovercard.tsx 4.9 KB

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