suggestedOwnerHovercard.tsx 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. import {Fragment, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import moment from 'moment-timezone';
  4. import {openInviteMembersModal} from 'sentry/actionCreators/modal';
  5. import {Alert} from 'sentry/components/alert';
  6. import ActorAvatar from 'sentry/components/avatar/actorAvatar';
  7. import {Button} from 'sentry/components/button';
  8. import CommitLink from 'sentry/components/commitLink';
  9. import {Divider, Hovercard} from 'sentry/components/hovercard';
  10. import Link from 'sentry/components/links/link';
  11. import Version from 'sentry/components/version';
  12. import {IconCommit} from 'sentry/icons';
  13. import {t, tct} from 'sentry/locale';
  14. import {space} from 'sentry/styles/space';
  15. import type {Actor} from 'sentry/types/core';
  16. import type {Commit} from 'sentry/types/integrations';
  17. import type {Organization} from 'sentry/types/organization';
  18. import type {Release} from 'sentry/types/release';
  19. import {defined} from 'sentry/utils';
  20. import theme from 'sentry/utils/theme';
  21. type Props = {
  22. /**
  23. * The suggested actor.
  24. */
  25. actor: Actor;
  26. /**
  27. * Children are required, as they are passed to the hovercard component, without it,
  28. * we will not be able to trigger any hovercard actions
  29. */
  30. children: React.ReactNode;
  31. organization: Organization;
  32. /**
  33. * The list of commits the actor is suggested for. May be left blank if the
  34. * actor is not suggested for commits.
  35. */
  36. commits?: Commit[];
  37. /**
  38. * Used to pre-select release project
  39. */
  40. projectId?: string;
  41. release?: Release;
  42. /**
  43. * The list of ownership rules the actor is suggested for. May be left blank
  44. * if the actor is not suggested based on ownership rules.
  45. */
  46. rules?: any[] | null;
  47. };
  48. function SuggestedOwnerHovercard(props: Props) {
  49. const [commitsExpanded, setCommitsExpanded] = useState<boolean>(false);
  50. const [rulesExpanded, setRulesExpanded] = useState<boolean>(false);
  51. const {organization, actor, commits, rules, release, projectId} = props;
  52. const modalData = {
  53. initialData: [
  54. {
  55. emails: actor.email ? new Set([actor.email]) : new Set([]),
  56. },
  57. ],
  58. source: 'suggested_assignees',
  59. };
  60. return (
  61. <StyledHovercard
  62. skipWrapper
  63. header={
  64. <Fragment>
  65. <HovercardHeader>
  66. <ActorAvatar size={20} hasTooltip={false} actor={actor} />
  67. {actor.name || actor.email}
  68. </HovercardHeader>
  69. {actor.id === undefined && (
  70. <EmailAlert type="warning" showIcon>
  71. {tct(
  72. 'The email [actorEmail] is not a member of your organization. [inviteUser:Invite] them or link additional emails in [accountSettings:account settings].',
  73. {
  74. actorEmail: <strong>{actor.email}</strong>,
  75. accountSettings: <Link to="/settings/account/emails/" />,
  76. inviteUser: <a onClick={() => openInviteMembersModal(modalData)} />,
  77. }
  78. )}
  79. </EmailAlert>
  80. )}
  81. </Fragment>
  82. }
  83. body={
  84. <HovercardBody>
  85. {commits !== undefined && !release && (
  86. <Fragment>
  87. <Divider>
  88. <h6>{t('Commits')}</h6>
  89. </Divider>
  90. <div>
  91. {commits
  92. .slice(0, commitsExpanded ? commits.length : 3)
  93. .map(({message, dateCreated}, i) => (
  94. <CommitReasonItem key={i}>
  95. <CommitIcon />
  96. <CommitMessage message={message ?? undefined} date={dateCreated} />
  97. </CommitReasonItem>
  98. ))}
  99. </div>
  100. {commits.length > 3 && !commitsExpanded ? (
  101. <ViewMoreButton
  102. priority="link"
  103. size="zero"
  104. onClick={() => setCommitsExpanded(true)}
  105. >
  106. {t('View more')}
  107. </ViewMoreButton>
  108. ) : null}
  109. </Fragment>
  110. )}
  111. {commits !== undefined && release && (
  112. <Fragment>
  113. <Divider>
  114. <h6>{t('Suspect Release')}</h6>
  115. </Divider>
  116. <div>
  117. <CommitReasonItem>
  118. <OwnershipTag tagType="release" />
  119. <ReleaseValue>
  120. {tct('[actor] [verb] [commits] in [release]', {
  121. actor: actor.name,
  122. verb: commits.length > 1 ? t('made') : t('last committed'),
  123. commits:
  124. commits.length > 1 ? (
  125. // Link to release commits
  126. <Link
  127. to={{
  128. pathname: `/organizations/${
  129. organization?.slug
  130. }/releases/${encodeURIComponent(release.version)}/commits/`,
  131. query: {project: projectId},
  132. }}
  133. >
  134. {t('%s commits', commits.length)}
  135. </Link>
  136. ) : (
  137. <CommitLink
  138. inline
  139. showIcon={false}
  140. commitId={commits[0].id}
  141. repository={commits[0].repository}
  142. />
  143. ),
  144. release: (
  145. <Version version={release.version} projectId={projectId} />
  146. ),
  147. })}
  148. </ReleaseValue>
  149. </CommitReasonItem>
  150. </div>
  151. </Fragment>
  152. )}
  153. {defined(rules) && (
  154. <Fragment>
  155. <Divider>
  156. <h6>{t('Matching Ownership Rules')}</h6>
  157. </Divider>
  158. <div>
  159. {rules
  160. .slice(0, rulesExpanded ? rules.length : 3)
  161. .map(([type, matched], i) => (
  162. <RuleReasonItem key={i}>
  163. <OwnershipTag tagType={type} />
  164. <OwnershipValue>{matched}</OwnershipValue>
  165. </RuleReasonItem>
  166. ))}
  167. </div>
  168. {rules.length > 3 && !rulesExpanded ? (
  169. <ViewMoreButton
  170. priority="link"
  171. size="zero"
  172. onClick={() => setRulesExpanded(true)}
  173. >
  174. {t('View more')}
  175. </ViewMoreButton>
  176. ) : null}
  177. </Fragment>
  178. )}
  179. </HovercardBody>
  180. }
  181. {...props}
  182. />
  183. );
  184. }
  185. const tagColors = {
  186. url: theme.green200,
  187. path: theme.purple300,
  188. tag: theme.blue300,
  189. codeowners: theme.pink300,
  190. release: theme.pink200,
  191. };
  192. const StyledHovercard = styled(Hovercard)`
  193. width: 400px;
  194. `;
  195. const CommitIcon = styled(IconCommit)`
  196. margin-right: ${space(0.5)};
  197. flex-shrink: 0;
  198. `;
  199. const CommitMessage = styled(({message = '', date, ...props}) => (
  200. <div {...props}>
  201. {message.split('\n')[0]}
  202. <CommitDate date={date} />
  203. </div>
  204. ))`
  205. color: ${p => p.theme.textColor};
  206. font-size: ${p => p.theme.fontSizeExtraSmall};
  207. margin-top: ${space(0.25)};
  208. hyphens: auto;
  209. `;
  210. const CommitDate = styled(({date, ...props}) => (
  211. <div {...props}>{moment(date).fromNow()}</div>
  212. ))`
  213. margin-top: ${space(0.5)};
  214. color: ${p => p.theme.gray300};
  215. `;
  216. const CommitReasonItem = styled('div')`
  217. display: flex;
  218. align-items: flex-start;
  219. gap: ${space(1)};
  220. `;
  221. const RuleReasonItem = styled('div')`
  222. display: flex;
  223. align-items: flex-start;
  224. gap: ${space(1)};
  225. `;
  226. const OwnershipTag = styled(({tagType, ...props}) => <div {...props}>{tagType}</div>)`
  227. background: ${p => tagColors[p.tagType.indexOf('tags') === -1 ? p.tagType : 'tag']};
  228. color: ${p => p.theme.white};
  229. font-size: ${p => p.theme.fontSizeExtraSmall};
  230. padding: ${space(0.25)} ${space(0.5)};
  231. margin: ${space(0.25)} ${space(0.5)} ${space(0.25)} 0;
  232. border-radius: 2px;
  233. font-weight: ${p => p.theme.fontWeightBold};
  234. text-align: center;
  235. `;
  236. const ViewMoreButton = styled(Button)`
  237. border: none;
  238. color: ${p => p.theme.gray300};
  239. font-size: ${p => p.theme.fontSizeExtraSmall};
  240. padding: ${space(0.25)} ${space(0.5)};
  241. margin: ${space(1)} ${space(0.25)} ${space(0.25)} 0;
  242. width: 100%;
  243. min-width: 34px;
  244. `;
  245. const OwnershipValue = styled('code')`
  246. word-break: break-all;
  247. font-size: ${p => p.theme.fontSizeExtraSmall};
  248. margin-top: ${space(0.25)};
  249. `;
  250. const ReleaseValue = styled('div')`
  251. font-size: ${p => p.theme.fontSizeSmall};
  252. margin-top: ${space(0.5)};
  253. `;
  254. const EmailAlert = styled(Alert)`
  255. margin: 10px -13px -13px;
  256. border-radius: 0;
  257. border-color: #ece0b0;
  258. font-size: ${p => p.theme.fontSizeSmall};
  259. font-weight: ${p => p.theme.fontWeightNormal};
  260. box-shadow: none;
  261. `;
  262. const HovercardHeader = styled('div')`
  263. display: flex;
  264. align-items: center;
  265. gap: ${space(1)};
  266. `;
  267. const HovercardBody = styled('div')`
  268. margin-top: -${space(2)};
  269. `;
  270. export default SuggestedOwnerHovercard;