organizationMemberRow.tsx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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/panelItem';
  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
  109. size={32}
  110. user={user ?? {email, id: email, name: email, type: 'user'}}
  111. />
  112. <MemberDescription to={detailsUrl}>
  113. <h5 style={{margin: '0 0 3px'}}>
  114. <UserName>{name}</UserName>
  115. </h5>
  116. <Email>{email}</Email>
  117. </MemberDescription>
  118. </MemberHeading>
  119. <div data-test-id="member-role">{this.renderMemberRole()}</div>
  120. <div data-test-id="member-status">
  121. {showResendButton ? (
  122. <Fragment>
  123. {isInviting && (
  124. <LoadingContainer>
  125. <LoadingIndicator mini />
  126. </LoadingContainer>
  127. )}
  128. {isInviteSuccessful && <span>{t('Sent!')}</span>}
  129. {!isInviting && !isInviteSuccessful && (
  130. <Button
  131. disabled={!canAddMembers}
  132. priority="primary"
  133. size="sm"
  134. onClick={this.handleSendInvite}
  135. >
  136. {pending ? t('Resend invite') : t('Resend SSO link')}
  137. </Button>
  138. )}
  139. </Fragment>
  140. ) : (
  141. <AuthStatus>
  142. {has2fa ? (
  143. <IconCheckmark isCircled color="success" />
  144. ) : (
  145. <IconFlag color="error" />
  146. )}
  147. {has2fa ? t('2FA Enabled') : t('2FA Not Enabled')}
  148. </AuthStatus>
  149. )}
  150. </div>
  151. {showRemoveButton || showLeaveButton ? (
  152. <RightColumn>
  153. {showRemoveButton && canRemoveMember && (
  154. <Confirm
  155. message={tct('Are you sure you want to remove [name] from [orgName]?', {
  156. name,
  157. orgName: organization.slug,
  158. })}
  159. onConfirm={this.handleRemove}
  160. >
  161. <Button
  162. data-test-id="remove"
  163. icon={<IconSubtract isCircled size="xs" />}
  164. size="sm"
  165. busy={this.state.busy}
  166. >
  167. {t('Remove')}
  168. </Button>
  169. </Confirm>
  170. )}
  171. {showRemoveButton && !canRemoveMember && (
  172. <Button
  173. disabled
  174. size="sm"
  175. title={
  176. isIdpProvisioned
  177. ? t(
  178. "This user is managed through your organization's identity provider."
  179. )
  180. : t('You do not have access to remove members')
  181. }
  182. icon={<IconSubtract isCircled size="xs" />}
  183. >
  184. {t('Remove')}
  185. </Button>
  186. )}
  187. {showLeaveButton && memberCanLeave && (
  188. <Confirm
  189. message={tct('Are you sure you want to leave [orgName]?', {
  190. orgName: organization.slug,
  191. })}
  192. onConfirm={this.handleLeave}
  193. >
  194. <Button priority="danger" size="sm" icon={<IconClose size="xs" />}>
  195. {t('Leave')}
  196. </Button>
  197. </Confirm>
  198. )}
  199. {showLeaveButton && !memberCanLeave && (
  200. <Button
  201. size="sm"
  202. icon={<IconClose size="xs" />}
  203. disabled
  204. title={
  205. isIdpProvisioned
  206. ? t(
  207. "Your account is managed through your organization's identity provider."
  208. )
  209. : t(
  210. 'You cannot leave this organization as you are the only organization owner.'
  211. )
  212. }
  213. >
  214. {t('Leave')}
  215. </Button>
  216. )}
  217. </RightColumn>
  218. ) : null}
  219. </StyledPanelItem>
  220. );
  221. }
  222. }
  223. const StyledPanelItem = styled(PanelItem)`
  224. display: grid;
  225. grid-template-columns: minmax(150px, 4fr) minmax(90px, 2fr) minmax(120px, 2fr) minmax(
  226. 100px,
  227. 1fr
  228. );
  229. gap: ${space(2)};
  230. align-items: center;
  231. `;
  232. // Force action button at the end to align to right
  233. const RightColumn = styled('div')`
  234. display: flex;
  235. justify-content: flex-end;
  236. `;
  237. const Section = styled('div')`
  238. display: inline-grid;
  239. grid-template-columns: max-content auto;
  240. gap: ${space(1)};
  241. align-items: center;
  242. `;
  243. const MemberHeading = styled(Section)``;
  244. const MemberDescription = styled(Link)`
  245. overflow: hidden;
  246. `;
  247. const UserName = styled('div')`
  248. display: block;
  249. overflow: hidden;
  250. font-size: ${p => p.theme.fontSizeMedium};
  251. text-overflow: ellipsis;
  252. `;
  253. const Email = styled('div')`
  254. color: ${p => p.theme.subText};
  255. font-size: ${p => p.theme.fontSizeSmall};
  256. overflow: hidden;
  257. text-overflow: ellipsis;
  258. `;
  259. const InvitedRole = styled(Section)``;
  260. const LoadingContainer = styled('div')`
  261. margin-top: 0;
  262. margin-bottom: ${space(1.5)};
  263. `;
  264. const AuthStatus = styled(Section)``;