suggestedOwnerHovercard.tsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import {Component, Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import moment from 'moment';
  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 {Divider, Hovercard} from 'sentry/components/hovercard';
  9. import Link from 'sentry/components/links/link';
  10. import {IconCommit} from 'sentry/icons';
  11. import {t, tct} from 'sentry/locale';
  12. import space from 'sentry/styles/space';
  13. import {Actor, Commit} from 'sentry/types';
  14. import {defined} from 'sentry/utils';
  15. import theme from 'sentry/utils/theme';
  16. type Props = {
  17. /**
  18. * The suggested actor.
  19. */
  20. actor: Actor;
  21. /**
  22. * Children are required, as they are passed to the hovercard component, without it,
  23. * we will not be able to trigger any hovercard actions
  24. */
  25. children: React.ReactNode;
  26. /**
  27. * The list of commits the actor is suggested for. May be left blank if the
  28. * actor is not suggested for commits.
  29. */
  30. commits?: Commit[];
  31. /**
  32. * The list of ownership rules the actor is suggested for. May be left blank
  33. * if the actor is not suggested based on ownership rules.
  34. */
  35. rules?: any[] | null;
  36. };
  37. type State = {
  38. commitsExpanded: boolean;
  39. rulesExpanded: boolean;
  40. };
  41. class SuggestedOwnerHovercard extends Component<Props, State> {
  42. state: State = {
  43. commitsExpanded: false,
  44. rulesExpanded: false,
  45. };
  46. render() {
  47. const {actor, commits, rules, ...props} = this.props;
  48. const {commitsExpanded, rulesExpanded} = this.state;
  49. const modalData = {
  50. initialData: [
  51. {
  52. emails: actor.email ? new Set([actor.email]) : new Set([]),
  53. },
  54. ],
  55. source: 'suggested_assignees',
  56. };
  57. return (
  58. <Hovercard
  59. header={
  60. <Fragment>
  61. <HovercardHeader>
  62. <HovercardActorAvatar actor={actor} />
  63. {actor.name || actor.email}
  64. </HovercardHeader>
  65. {actor.id === undefined && (
  66. <EmailAlert type="warning" showIcon>
  67. {tct(
  68. 'The email [actorEmail] is not a member of your organization. [inviteUser:Invite] them or link additional emails in [accountSettings:account settings].',
  69. {
  70. actorEmail: <strong>{actor.email}</strong>,
  71. accountSettings: <Link to="/settings/account/emails/" />,
  72. inviteUser: <a onClick={() => openInviteMembersModal(modalData)} />,
  73. }
  74. )}
  75. </EmailAlert>
  76. )}
  77. </Fragment>
  78. }
  79. body={
  80. <HovercardBody>
  81. {commits !== undefined && (
  82. <Fragment>
  83. <Divider>
  84. <h6>{t('Commits')}</h6>
  85. </Divider>
  86. <div>
  87. {commits
  88. .slice(0, commitsExpanded ? commits.length : 3)
  89. .map(({message, dateCreated}, i) => (
  90. <CommitReasonItem key={i}>
  91. <CommitIcon />
  92. <CommitMessage
  93. message={message ?? undefined}
  94. date={dateCreated}
  95. />
  96. </CommitReasonItem>
  97. ))}
  98. </div>
  99. {commits.length > 3 && !commitsExpanded ? (
  100. <ViewMoreButton
  101. priority="link"
  102. size="zero"
  103. onClick={() => this.setState({commitsExpanded: true})}
  104. >
  105. {t('View more')}
  106. </ViewMoreButton>
  107. ) : null}
  108. </Fragment>
  109. )}
  110. {defined(rules) && (
  111. <Fragment>
  112. <Divider>
  113. <h6>{t('Matching Ownership Rules')}</h6>
  114. </Divider>
  115. <div>
  116. {rules
  117. .slice(0, rulesExpanded ? rules.length : 3)
  118. .map(([type, matched], i) => (
  119. <RuleReasonItem key={i}>
  120. <OwnershipTag tagType={type} />
  121. <OwnershipValue>{matched}</OwnershipValue>
  122. </RuleReasonItem>
  123. ))}
  124. </div>
  125. {rules.length > 3 && !rulesExpanded ? (
  126. <ViewMoreButton
  127. priority="link"
  128. size="zero"
  129. onClick={() => this.setState({rulesExpanded: true})}
  130. >
  131. {t('View more')}
  132. </ViewMoreButton>
  133. ) : null}
  134. </Fragment>
  135. )}
  136. </HovercardBody>
  137. }
  138. {...props}
  139. />
  140. );
  141. }
  142. }
  143. const tagColors = {
  144. url: theme.green200,
  145. path: theme.purple300,
  146. tag: theme.blue300,
  147. codeowners: theme.pink300,
  148. };
  149. const CommitIcon = styled(IconCommit)`
  150. margin-right: ${space(0.5)};
  151. flex-shrink: 0;
  152. `;
  153. const CommitMessage = styled(({message = '', date, ...props}) => (
  154. <div {...props}>
  155. {message.split('\n')[0]}
  156. <CommitDate date={date} />
  157. </div>
  158. ))`
  159. color: ${p => p.theme.textColor};
  160. font-size: ${p => p.theme.fontSizeExtraSmall};
  161. margin-top: ${space(0.25)};
  162. hyphens: auto;
  163. `;
  164. const CommitDate = styled(({date, ...props}) => (
  165. <div {...props}>{moment(date).fromNow()}</div>
  166. ))`
  167. margin-top: ${space(0.5)};
  168. color: ${p => p.theme.gray300};
  169. `;
  170. const CommitReasonItem = styled('div')`
  171. display: flex;
  172. align-items: flex-start;
  173. &:not(:last-child) {
  174. margin-bottom: ${space(1)};
  175. }
  176. `;
  177. const RuleReasonItem = styled('code')`
  178. display: flex;
  179. align-items: flex-start;
  180. &:not(:last-child) {
  181. margin-bottom: ${space(1)};
  182. }
  183. `;
  184. const OwnershipTag = styled(({tagType, ...props}) => <div {...props}>{tagType}</div>)`
  185. background: ${p => tagColors[p.tagType.indexOf('tags') === -1 ? p.tagType : 'tag']};
  186. color: ${p => p.theme.white};
  187. font-size: ${p => p.theme.fontSizeExtraSmall};
  188. padding: ${space(0.25)} ${space(0.5)};
  189. margin: ${space(0.25)} ${space(0.5)} ${space(0.25)} 0;
  190. border-radius: 2px;
  191. font-weight: bold;
  192. text-align: center;
  193. `;
  194. const ViewMoreButton = styled(Button)`
  195. border: none;
  196. color: ${p => p.theme.gray300};
  197. font-size: ${p => p.theme.fontSizeExtraSmall};
  198. padding: ${space(0.25)} ${space(0.5)};
  199. margin: ${space(1)} ${space(0.25)} ${space(0.25)} 0;
  200. width: 100%;
  201. min-width: 34px;
  202. `;
  203. const OwnershipValue = styled('code')`
  204. word-break: break-all;
  205. line-height: 1.2;
  206. `;
  207. const EmailAlert = styled(Alert)`
  208. margin: 10px -13px -13px;
  209. border-radius: 0;
  210. border-color: #ece0b0;
  211. font-size: ${p => p.theme.fontSizeSmall};
  212. font-weight: normal;
  213. box-shadow: none;
  214. `;
  215. const HovercardHeader = styled('div')`
  216. display: flex;
  217. align-items: center;
  218. `;
  219. const HovercardActorAvatar = styled(p => (
  220. <ActorAvatar size={20} hasTooltip={false} {...p} />
  221. ))`
  222. margin-right: ${space(1)};
  223. `;
  224. const HovercardBody = styled('div')`
  225. margin-top: -${space(2)};
  226. `;
  227. export default SuggestedOwnerHovercard;