organizationMemberRow.tsx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. import {Fragment, PureComponent} from 'react';
  2. import styled from '@emotion/styled';
  3. import UserAvatar from 'sentry/components/avatar/userAvatar';
  4. import {Button} from 'sentry/components/button';
  5. import Confirm from 'sentry/components/confirm';
  6. import HookOrDefault from 'sentry/components/hookOrDefault';
  7. import Link from 'sentry/components/links/link';
  8. import LoadingIndicator from 'sentry/components/loadingIndicator';
  9. import {OrgRoleInfo} from 'sentry/components/orgRole';
  10. import {PanelItem} from 'sentry/components/panels';
  11. import {IconCheckmark, IconClose, IconFlag, IconMail, IconSubtract} from 'sentry/icons';
  12. import {t, tct} from 'sentry/locale';
  13. import {space} from 'sentry/styles/space';
  14. import {AvatarUser, Member, Organization} from 'sentry/types';
  15. import isMemberDisabledFromLimit from 'sentry/utils/isMemberDisabledFromLimit';
  16. type Props = {
  17. canAddMembers: boolean;
  18. canRemoveMembers: boolean;
  19. currentUser: AvatarUser;
  20. member: Member;
  21. memberCanLeave: boolean;
  22. onLeave: (member: Member) => void;
  23. onRemove: (member: Member) => void;
  24. onSendInvite: (member: Member) => void;
  25. organization: Organization;
  26. requireLink: boolean;
  27. status: '' | 'loading' | 'success' | 'error' | null;
  28. };
  29. type State = {
  30. busy: boolean;
  31. };
  32. const DisabledMemberTooltip = HookOrDefault({
  33. hookName: 'component:disabled-member-tooltip',
  34. defaultComponent: ({children}) => <Fragment>{children}</Fragment>,
  35. });
  36. export default class OrganizationMemberRow extends PureComponent<Props, State> {
  37. state: State = {
  38. busy: false,
  39. };
  40. handleRemove = () => {
  41. const {onRemove} = this.props;
  42. if (typeof onRemove !== 'function') {
  43. return;
  44. }
  45. this.setState({busy: true});
  46. onRemove(this.props.member);
  47. };
  48. handleLeave = () => {
  49. const {onLeave} = this.props;
  50. if (typeof onLeave !== 'function') {
  51. return;
  52. }
  53. this.setState({busy: true});
  54. onLeave(this.props.member);
  55. };
  56. handleSendInvite = () => {
  57. const {onSendInvite, member} = this.props;
  58. if (typeof onSendInvite !== 'function') {
  59. return;
  60. }
  61. onSendInvite(member);
  62. };
  63. renderMemberRole() {
  64. const {member, organization} = this.props;
  65. const {roleName, pending, expired} = member;
  66. if (isMemberDisabledFromLimit(member)) {
  67. return <DisabledMemberTooltip>{t('Deactivated')}</DisabledMemberTooltip>;
  68. }
  69. if (pending) {
  70. return (
  71. <InvitedRole>
  72. <IconMail size="md" />
  73. {expired ? t('Expired Invite') : tct('Invited [roleName]', {roleName})}
  74. </InvitedRole>
  75. );
  76. }
  77. return <OrgRoleInfo member={member} organization={organization} />;
  78. }
  79. render() {
  80. const {
  81. member,
  82. organization,
  83. status,
  84. requireLink,
  85. memberCanLeave,
  86. currentUser,
  87. canRemoveMembers,
  88. canAddMembers,
  89. } = this.props;
  90. const {id, flags, email, name, pending, user} = member;
  91. // if member is not the only owner, they can leave
  92. const isIdpProvisioned = flags['idp:provisioned'];
  93. const needsSso = !flags['sso:linked'] && requireLink;
  94. const isCurrentUser = currentUser.email === email;
  95. const showRemoveButton = !isCurrentUser;
  96. const showLeaveButton = isCurrentUser;
  97. const canRemoveMember = canRemoveMembers && !isCurrentUser && !isIdpProvisioned;
  98. // member has a `user` property if they are registered with sentry
  99. // i.e. has accepted an invite to join org
  100. const has2fa = user && user.has2fa;
  101. const detailsUrl = `/settings/${organization.slug}/members/${id}/`;
  102. const isInviteSuccessful = status === 'success';
  103. const isInviting = status === 'loading';
  104. const showResendButton = pending || needsSso;
  105. return (
  106. <StyledPanelItem data-test-id={email}>
  107. <MemberHeading>
  108. <UserAvatar size={32} user={user ?? {id: email, email}} />
  109. <MemberDescription to={detailsUrl}>
  110. <h5 style={{margin: '0 0 3px'}}>
  111. <UserName>{name}</UserName>
  112. </h5>
  113. <Email>{email}</Email>
  114. </MemberDescription>
  115. </MemberHeading>
  116. <div data-test-id="member-role">{this.renderMemberRole()}</div>
  117. <div data-test-id="member-status">
  118. {showResendButton ? (
  119. <Fragment>
  120. {isInviting && (
  121. <LoadingContainer>
  122. <LoadingIndicator mini />
  123. </LoadingContainer>
  124. )}
  125. {isInviteSuccessful && <span>{t('Sent!')}</span>}
  126. {!isInviting && !isInviteSuccessful && (
  127. <Button
  128. disabled={!canAddMembers}
  129. priority="primary"
  130. size="sm"
  131. onClick={this.handleSendInvite}
  132. >
  133. {pending ? t('Resend invite') : t('Resend SSO link')}
  134. </Button>
  135. )}
  136. </Fragment>
  137. ) : (
  138. <AuthStatus>
  139. {has2fa ? (
  140. <IconCheckmark isCircled color="success" />
  141. ) : (
  142. <IconFlag color="error" />
  143. )}
  144. {has2fa ? t('2FA Enabled') : t('2FA Not Enabled')}
  145. </AuthStatus>
  146. )}
  147. </div>
  148. {showRemoveButton || showLeaveButton ? (
  149. <RightColumn>
  150. {showRemoveButton && canRemoveMember && (
  151. <Confirm
  152. message={tct('Are you sure you want to remove [name] from [orgName]?', {
  153. name,
  154. orgName: organization.slug,
  155. })}
  156. onConfirm={this.handleRemove}
  157. >
  158. <Button
  159. data-test-id="remove"
  160. icon={<IconSubtract isCircled size="xs" />}
  161. size="sm"
  162. busy={this.state.busy}
  163. >
  164. {t('Remove')}
  165. </Button>
  166. </Confirm>
  167. )}
  168. {showRemoveButton && !canRemoveMember && (
  169. <Button
  170. disabled
  171. size="sm"
  172. title={
  173. isIdpProvisioned
  174. ? t(
  175. "This user is managed through your organization's identity provider."
  176. )
  177. : t('You do not have access to remove members')
  178. }
  179. icon={<IconSubtract isCircled size="xs" />}
  180. >
  181. {t('Remove')}
  182. </Button>
  183. )}
  184. {showLeaveButton && memberCanLeave && (
  185. <Confirm
  186. message={tct('Are you sure you want to leave [orgName]?', {
  187. orgName: organization.slug,
  188. })}
  189. onConfirm={this.handleLeave}
  190. >
  191. <Button priority="danger" size="sm" icon={<IconClose size="xs" />}>
  192. {t('Leave')}
  193. </Button>
  194. </Confirm>
  195. )}
  196. {showLeaveButton && !memberCanLeave && (
  197. <Button
  198. size="sm"
  199. icon={<IconClose size="xs" />}
  200. disabled
  201. title={
  202. isIdpProvisioned
  203. ? t(
  204. "Your account is managed through your organization's identity provider."
  205. )
  206. : t(
  207. 'You cannot leave this organization as you are the only organization owner.'
  208. )
  209. }
  210. >
  211. {t('Leave')}
  212. </Button>
  213. )}
  214. </RightColumn>
  215. ) : null}
  216. </StyledPanelItem>
  217. );
  218. }
  219. }
  220. const StyledPanelItem = styled(PanelItem)`
  221. display: grid;
  222. grid-template-columns: minmax(150px, 4fr) minmax(90px, 2fr) minmax(120px, 2fr) minmax(
  223. 100px,
  224. 1fr
  225. );
  226. gap: ${space(2)};
  227. align-items: center;
  228. `;
  229. // Force action button at the end to align to right
  230. const RightColumn = styled('div')`
  231. display: flex;
  232. justify-content: flex-end;
  233. `;
  234. const Section = styled('div')`
  235. display: inline-grid;
  236. grid-template-columns: max-content auto;
  237. gap: ${space(1)};
  238. align-items: center;
  239. `;
  240. const MemberHeading = styled(Section)``;
  241. const MemberDescription = styled(Link)`
  242. overflow: hidden;
  243. `;
  244. const UserName = styled('div')`
  245. display: block;
  246. overflow: hidden;
  247. font-size: ${p => p.theme.fontSizeMedium};
  248. text-overflow: ellipsis;
  249. `;
  250. const Email = styled('div')`
  251. color: ${p => p.theme.subText};
  252. font-size: ${p => p.theme.fontSizeSmall};
  253. overflow: hidden;
  254. text-overflow: ellipsis;
  255. `;
  256. const InvitedRole = styled(Section)``;
  257. const LoadingContainer = styled('div')`
  258. margin-top: 0;
  259. margin-bottom: ${space(1.5)};
  260. `;
  261. const AuthStatus = styled(Section)``;