inviteRequestRow.tsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import {Button} from 'sentry/components/button';
  4. import Confirm from 'sentry/components/confirm';
  5. import HookOrDefault from 'sentry/components/hookOrDefault';
  6. import PanelItem from 'sentry/components/panels/panelItem';
  7. import RoleSelectControl from 'sentry/components/roleSelectControl';
  8. import Tag from 'sentry/components/tag';
  9. import TeamSelector from 'sentry/components/teamSelector';
  10. import {Tooltip} from 'sentry/components/tooltip';
  11. import {IconCheckmark, IconClose} from 'sentry/icons';
  12. import {t, tct} from 'sentry/locale';
  13. import {space} from 'sentry/styles/space';
  14. import {Member, Organization, OrgRole} from 'sentry/types';
  15. type Props = {
  16. allRoles: OrgRole[];
  17. inviteRequest: Member;
  18. inviteRequestBusy: {[key: string]: boolean};
  19. onApprove: (inviteRequest: Member) => void;
  20. onDeny: (inviteRequest: Member) => void;
  21. onUpdate: (data: Partial<Member>) => void;
  22. organization: Organization;
  23. };
  24. const InviteModalHook = HookOrDefault({
  25. hookName: 'member-invite-modal:customization',
  26. defaultComponent: ({onSendInvites, children}) =>
  27. children({sendInvites: onSendInvites, canSend: true}),
  28. });
  29. type InviteModalRenderFunc = React.ComponentProps<typeof InviteModalHook>['children'];
  30. function InviteRequestRow({
  31. inviteRequest,
  32. inviteRequestBusy,
  33. organization,
  34. onApprove,
  35. onDeny,
  36. onUpdate,
  37. allRoles,
  38. }: Props) {
  39. const role = allRoles.find(r => r.id === inviteRequest.role);
  40. const roleDisallowed = !(role && role.allowed);
  41. const {access} = organization;
  42. const canApprove = access.includes('member:admin');
  43. const hookRenderer: InviteModalRenderFunc = ({sendInvites, canSend, headerInfo}) => (
  44. <StyledPanelItem>
  45. <div>
  46. <h5 style={{marginBottom: space(0.5)}}>
  47. <UserName>{inviteRequest.email}</UserName>
  48. </h5>
  49. {inviteRequest.inviteStatus === 'requested_to_be_invited' ? (
  50. inviteRequest.inviterName && (
  51. <Description>
  52. <Tooltip
  53. title={t(
  54. 'An existing member has asked to invite this user to your organization'
  55. )}
  56. >
  57. {tct('Requested by [inviterName]', {
  58. inviterName: inviteRequest.inviterName,
  59. })}
  60. </Tooltip>
  61. </Description>
  62. )
  63. ) : (
  64. <JoinRequestIndicator
  65. tooltipText={t('This user has asked to join your organization.')}
  66. >
  67. {t('Join request')}
  68. </JoinRequestIndicator>
  69. )}
  70. </div>
  71. {canApprove ? (
  72. <StyledRoleSelectControl
  73. name="role"
  74. disableUnallowed
  75. onChange={r => onUpdate({role: r.value})}
  76. value={inviteRequest.role}
  77. roles={allRoles}
  78. />
  79. ) : (
  80. <div>{inviteRequest.roleName}</div>
  81. )}
  82. {canApprove ? (
  83. <TeamSelectControl
  84. name="teams"
  85. placeholder={t('None')}
  86. onChange={teams => onUpdate({teams: (teams || []).map(team => team.value)})}
  87. value={inviteRequest.teams}
  88. clearable
  89. multiple
  90. />
  91. ) : (
  92. <div>{inviteRequest.teams.join(', ')}</div>
  93. )}
  94. <ButtonGroup>
  95. <Button
  96. size="sm"
  97. busy={inviteRequestBusy[inviteRequest.id]}
  98. onClick={() => onDeny(inviteRequest)}
  99. icon={<IconClose />}
  100. disabled={!canApprove}
  101. title={
  102. canApprove
  103. ? undefined
  104. : t('This request needs to be reviewed by a privileged user')
  105. }
  106. >
  107. {t('Deny')}
  108. </Button>
  109. <Confirm
  110. onConfirm={sendInvites}
  111. disableConfirmButton={!canSend}
  112. disabled={!canApprove || roleDisallowed}
  113. message={
  114. <Fragment>
  115. {tct('Are you sure you want to invite [email] to your organization?', {
  116. email: inviteRequest.email,
  117. })}
  118. {headerInfo}
  119. </Fragment>
  120. }
  121. >
  122. <Button
  123. priority="primary"
  124. size="sm"
  125. busy={inviteRequestBusy[inviteRequest.id]}
  126. title={
  127. canApprove
  128. ? roleDisallowed
  129. ? t(
  130. `You do not have permission to approve a user of this role.
  131. Select a different role to approve this user.`
  132. )
  133. : undefined
  134. : t('This request needs to be reviewed by a privileged user')
  135. }
  136. icon={<IconCheckmark />}
  137. >
  138. {t('Approve')}
  139. </Button>
  140. </Confirm>
  141. </ButtonGroup>
  142. </StyledPanelItem>
  143. );
  144. return (
  145. <InviteModalHook
  146. willInvite
  147. organization={organization}
  148. onSendInvites={() => onApprove(inviteRequest)}
  149. >
  150. {hookRenderer}
  151. </InviteModalHook>
  152. );
  153. }
  154. const JoinRequestIndicator = styled(Tag)`
  155. text-transform: uppercase;
  156. `;
  157. const StyledPanelItem = styled(PanelItem)`
  158. display: grid;
  159. grid-template-columns: minmax(150px, auto) minmax(100px, 140px) 220px max-content;
  160. gap: ${space(2)};
  161. align-items: center;
  162. `;
  163. const UserName = styled('div')`
  164. font-size: ${p => p.theme.fontSizeLarge};
  165. overflow: hidden;
  166. text-overflow: ellipsis;
  167. `;
  168. const Description = styled('div')`
  169. display: block;
  170. color: ${p => p.theme.subText};
  171. font-size: 14px;
  172. overflow: hidden;
  173. text-overflow: ellipsis;
  174. `;
  175. const StyledRoleSelectControl = styled(RoleSelectControl)`
  176. max-width: 140px;
  177. `;
  178. const TeamSelectControl = styled(TeamSelector)`
  179. max-width: 220px;
  180. .Select-value-label {
  181. max-width: 150px;
  182. word-break: break-all;
  183. }
  184. `;
  185. const ButtonGroup = styled('div')`
  186. display: inline-grid;
  187. grid-template-columns: repeat(2, max-content);
  188. gap: ${space(1)};
  189. `;
  190. export default InviteRequestRow;