quickContextWrapper.tsx 5.2 KB

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