commitRow.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import {useCallback} from 'react';
  2. import styled from '@emotion/styled';
  3. import * as Sentry from '@sentry/react';
  4. import {openInviteMembersModal} from 'sentry/actionCreators/modal';
  5. import UserAvatar from 'sentry/components/avatar/userAvatar';
  6. import CommitLink from 'sentry/components/commitLink';
  7. import {Hovercard} from 'sentry/components/hovercard';
  8. import Link from 'sentry/components/links/link';
  9. import {PanelItem} from 'sentry/components/panels';
  10. import TextOverflow from 'sentry/components/textOverflow';
  11. import TimeSince from 'sentry/components/timeSince';
  12. import {IconWarning} from 'sentry/icons';
  13. import {t, tct} from 'sentry/locale';
  14. import ConfigStore from 'sentry/stores/configStore';
  15. import space from 'sentry/styles/space';
  16. import {Commit} from 'sentry/types';
  17. import Button from './button';
  18. export function formatCommitMessage(message: string | null) {
  19. if (!message) {
  20. return t('No message provided');
  21. }
  22. return message.split(/\n/)[0];
  23. }
  24. export interface CommitRowProps {
  25. commit: Commit;
  26. customAvatar?: React.ReactNode;
  27. onCommitClick?: () => void;
  28. onPullRequestClick?: () => void;
  29. }
  30. function CommitRow({
  31. commit,
  32. customAvatar,
  33. onPullRequestClick,
  34. onCommitClick,
  35. }: CommitRowProps) {
  36. const handleInviteClick = useCallback(() => {
  37. if (!commit.author?.email) {
  38. Sentry.captureException(
  39. new Error(`Commit author has no email or id, invite flow is broken.`)
  40. );
  41. return;
  42. }
  43. openInviteMembersModal({
  44. initialData: [
  45. {
  46. emails: new Set([commit.author.email]),
  47. },
  48. ],
  49. source: 'suspect_commit',
  50. });
  51. }, [commit.author]);
  52. const user = ConfigStore.get('user');
  53. const isUser = user?.id === commit.author?.id;
  54. return (
  55. <StyledPanelItem key={commit.id} data-test-id="commit-row">
  56. {customAvatar ? (
  57. customAvatar
  58. ) : commit.author && commit.author.id === undefined ? (
  59. <AvatarWrapper>
  60. <Hovercard
  61. skipWrapper
  62. body={
  63. <EmailWarning>
  64. {tct(
  65. 'The email [actorEmail] is not a member of your organization. [inviteUser:Invite] them or link additional emails in [accountSettings:account settings].',
  66. {
  67. actorEmail: <strong>{commit.author.email}</strong>,
  68. accountSettings: <StyledLink to="/settings/account/emails/" />,
  69. inviteUser: <StyledLink to="" onClick={handleInviteClick} />,
  70. }
  71. )}
  72. </EmailWarning>
  73. }
  74. >
  75. <UserAvatar size={36} user={commit.author} />
  76. <EmailWarningIcon data-test-id="email-warning">
  77. <IconWarning size="xs" />
  78. </EmailWarningIcon>
  79. </Hovercard>
  80. </AvatarWrapper>
  81. ) : (
  82. <div>
  83. <UserAvatar size={36} user={commit.author} />
  84. </div>
  85. )}
  86. <CommitMessage>
  87. <Message>
  88. {tct('[author] committed [commitLink]', {
  89. author: isUser ? t('You') : commit.author?.name ?? t('Unknown author'),
  90. commitLink: (
  91. <CommitLink
  92. inline
  93. showIcon={false}
  94. commitId={commit.id}
  95. repository={commit.repository}
  96. onClick={onCommitClick}
  97. />
  98. ),
  99. })}
  100. </Message>
  101. <Meta>
  102. {formatCommitMessage(commit.message)} &bull;{' '}
  103. <TimeSince date={commit.dateCreated} />
  104. </Meta>
  105. </CommitMessage>
  106. {commit.pullRequest && commit.pullRequest.externalUrl && (
  107. <Button
  108. external
  109. href={commit.pullRequest.externalUrl}
  110. onClick={onPullRequestClick}
  111. >
  112. {t('View Pull Request')}
  113. </Button>
  114. )}
  115. </StyledPanelItem>
  116. );
  117. }
  118. const StyledPanelItem = styled(PanelItem)`
  119. display: flex;
  120. align-items: center;
  121. gap: ${space(2)};
  122. `;
  123. const AvatarWrapper = styled('div')`
  124. position: relative;
  125. `;
  126. const EmailWarning = styled('div')`
  127. font-size: ${p => p.theme.fontSizeSmall};
  128. line-height: 1.4;
  129. margin: -4px;
  130. `;
  131. const StyledLink = styled(Link)`
  132. color: ${p => p.theme.textColor};
  133. border-bottom: 1px dotted ${p => p.theme.textColor};
  134. &:hover {
  135. color: ${p => p.theme.textColor};
  136. }
  137. `;
  138. const EmailWarningIcon = styled('span')`
  139. position: absolute;
  140. bottom: -6px;
  141. right: -7px;
  142. line-height: 12px;
  143. border-radius: 50%;
  144. border: 1px solid ${p => p.theme.background};
  145. background: ${p => p.theme.yellow200};
  146. padding: 1px 2px 3px 2px;
  147. `;
  148. const CommitMessage = styled('div')`
  149. flex: 1;
  150. flex-direction: column;
  151. min-width: 0;
  152. margin-right: ${space(2)};
  153. `;
  154. const Message = styled(TextOverflow)`
  155. font-size: ${p => p.theme.fontSizeLarge};
  156. line-height: 1.2;
  157. `;
  158. const Meta = styled(TextOverflow)`
  159. font-size: 13px;
  160. line-height: 1.5;
  161. margin: 0;
  162. color: ${p => p.theme.subText};
  163. `;
  164. export {CommitRow};