organizationMembersWrapper.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import {cloneElement, Fragment} from 'react';
  2. import type {RouteComponentProps} from 'react-router';
  3. import {openInviteMembersModal} from 'sentry/actionCreators/modal';
  4. import FeatureDisabled from 'sentry/components/acl/featureDisabled';
  5. import {Button} from 'sentry/components/button';
  6. import HookOrDefault from 'sentry/components/hookOrDefault';
  7. import {Hovercard} from 'sentry/components/hovercard';
  8. import {Tooltip} from 'sentry/components/tooltip';
  9. import {IconMail} from 'sentry/icons';
  10. import {t} from 'sentry/locale';
  11. import type {Member, Organization} from 'sentry/types/organization';
  12. import routeTitleGen from 'sentry/utils/routeTitle';
  13. import withOrganization from 'sentry/utils/withOrganization';
  14. import DeprecatedAsyncView from 'sentry/views/deprecatedAsyncView';
  15. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  16. type Props = {
  17. organization: Organization;
  18. children?: any;
  19. } & RouteComponentProps<{}, {}>;
  20. type State = DeprecatedAsyncView['state'] & {
  21. inviteRequests: Member[];
  22. };
  23. const InviteMembersButtonHook = HookOrDefault({
  24. hookName: 'member-invite-button:customization',
  25. defaultComponent: ({children, organization, onTriggerModal}) => {
  26. const isSsoRequired = organization.requiresSso;
  27. const disabled = isSsoRequired || !organization.features.includes('invite-members');
  28. return children({disabled, isSsoRequired, onTriggerModal});
  29. },
  30. });
  31. class OrganizationMembersWrapper extends DeprecatedAsyncView<Props, State> {
  32. getEndpoints(): ReturnType<DeprecatedAsyncView['getEndpoints']> {
  33. const {organization} = this.props;
  34. return [
  35. ['inviteRequests', `/organizations/${organization.slug}/invite-requests/`],
  36. ['requestList', `/organizations/${organization.slug}/access-requests/`],
  37. ];
  38. }
  39. getTitle() {
  40. const {organization} = this.props;
  41. return routeTitleGen(t('Members'), organization.slug, false);
  42. }
  43. get onRequestsTab() {
  44. return location.pathname.includes('/requests/');
  45. }
  46. get hasWriteAccess() {
  47. const {organization} = this.props;
  48. if (!organization || !organization.access) {
  49. return false;
  50. }
  51. return organization.access.includes('member:write');
  52. }
  53. get showInviteRequests() {
  54. return this.hasWriteAccess;
  55. }
  56. get showNavTabs() {
  57. const {requestList} = this.state;
  58. // show the requests tab if there are pending team requests,
  59. // or if the user has access to approve or deny invite requests
  60. return (requestList && requestList.length > 0) || this.showInviteRequests;
  61. }
  62. get requestCount() {
  63. const {requestList, inviteRequests} = this.state;
  64. let count = requestList.length;
  65. // if the user can't see the invite requests panel,
  66. // exclude those requests from the total count
  67. if (this.showInviteRequests) {
  68. count += inviteRequests.length;
  69. }
  70. return count ? count.toString() : null;
  71. }
  72. removeAccessRequest = (id: string) =>
  73. this.setState(state => ({
  74. requestList: state.requestList.filter(request => request.id !== id),
  75. }));
  76. removeInviteRequest = (id: string) =>
  77. this.setState(state => ({
  78. inviteRequests: state.inviteRequests.filter(request => request.id !== id),
  79. }));
  80. updateInviteRequest = (id: string, data: Partial<Member>) =>
  81. this.setState(state => {
  82. const inviteRequests = [...state.inviteRequests];
  83. const inviteIndex = inviteRequests.findIndex(request => request.id === id);
  84. inviteRequests[inviteIndex] = {...inviteRequests[inviteIndex], ...data};
  85. return {inviteRequests};
  86. });
  87. renderBody() {
  88. const {children, organization} = this.props;
  89. const {requestList, inviteRequests} = this.state;
  90. const action = (
  91. <InviteMembersButtonHook
  92. organization={organization}
  93. onTriggerModal={() =>
  94. openInviteMembersModal({
  95. onClose: () => {
  96. this.fetchData();
  97. },
  98. source: 'members_settings',
  99. })
  100. }
  101. >
  102. {renderInviteMembersButton}
  103. </InviteMembersButtonHook>
  104. );
  105. return (
  106. <Fragment>
  107. <SettingsPageHeader title="Members" action={action} />
  108. {children &&
  109. cloneElement(children, {
  110. requestList,
  111. inviteRequests,
  112. onRemoveInviteRequest: this.removeInviteRequest,
  113. onUpdateInviteRequest: this.updateInviteRequest,
  114. onRemoveAccessRequest: this.removeAccessRequest,
  115. showInviteRequests: this.showInviteRequests,
  116. })}
  117. </Fragment>
  118. );
  119. }
  120. }
  121. function renderInviteMembersButton({
  122. disabled,
  123. isSsoRequired,
  124. onTriggerModal,
  125. }: {
  126. onTriggerModal: () => void;
  127. disabled?: boolean;
  128. isSsoRequired?: boolean;
  129. }) {
  130. const action = (
  131. <Button
  132. priority="primary"
  133. size="sm"
  134. onClick={onTriggerModal}
  135. data-test-id="email-invite"
  136. icon={<IconMail />}
  137. disabled={disabled}
  138. >
  139. {t('Invite Members')}
  140. </Button>
  141. );
  142. return disabled ? (
  143. isSsoRequired ? (
  144. <Tooltip
  145. skipWrapper
  146. title={t(
  147. `Your organization must use its single sign-on provider to register new members.`
  148. )}
  149. >
  150. {action}
  151. </Tooltip>
  152. ) : (
  153. <Hovercard
  154. body={
  155. <FeatureDisabled
  156. featureName={t('Invite Members')}
  157. features="organizations:invite-members"
  158. hideHelpToggle
  159. />
  160. }
  161. >
  162. {action}
  163. </Hovercard>
  164. )
  165. ) : (
  166. action
  167. );
  168. }
  169. export default withOrganization(OrganizationMembersWrapper);