projectTeams.tsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import {RouteComponentProps} from 'react-router';
  2. import {css} from '@emotion/react';
  3. import styled from '@emotion/styled';
  4. import {addErrorMessage} from 'sentry/actionCreators/indicator';
  5. import {openCreateTeamModal} from 'sentry/actionCreators/modal';
  6. import {addTeamToProject, removeTeamFromProject} from 'sentry/actionCreators/projects';
  7. import Link from 'sentry/components/links/link';
  8. import {Tooltip} from 'sentry/components/tooltip';
  9. import {t, tct} from 'sentry/locale';
  10. import TeamStore from 'sentry/stores/teamStore';
  11. import {space} from 'sentry/styles/space';
  12. import {Organization, Project, Team} from 'sentry/types';
  13. import routeTitleGen from 'sentry/utils/routeTitle';
  14. import AsyncView from 'sentry/views/asyncView';
  15. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  16. import TeamSelect from 'sentry/views/settings/components/teamSelect';
  17. type Props = {
  18. organization: Organization;
  19. project: Project;
  20. } & RouteComponentProps<{projectId: string}, {}>;
  21. type State = {
  22. projectTeams: null | Team[];
  23. } & AsyncView['state'];
  24. class ProjectTeams extends AsyncView<Props, State> {
  25. getEndpoints(): ReturnType<AsyncView['getEndpoints']> {
  26. const {organization, project} = this.props;
  27. return [['projectTeams', `/projects/${organization.slug}/${project.slug}/teams/`]];
  28. }
  29. getTitle() {
  30. const {projectId} = this.props.params;
  31. return routeTitleGen(t('Project Teams'), projectId, false);
  32. }
  33. canCreateTeam = () => {
  34. const {organization} = this.props;
  35. const access = new Set(organization.access);
  36. return (
  37. access.has('org:write') && access.has('team:write') && access.has('project:write')
  38. );
  39. };
  40. handleRemove = (teamSlug: Team['slug']) => {
  41. if (this.state.loading) {
  42. return;
  43. }
  44. const {organization, project} = this.props;
  45. removeTeamFromProject(this.api, organization.slug, project.slug, teamSlug)
  46. .then(() => this.handleRemovedTeam(teamSlug))
  47. .catch(() => {
  48. addErrorMessage(t('Could not remove the %s team', teamSlug));
  49. this.setState({loading: false});
  50. });
  51. };
  52. handleRemovedTeam = (teamSlug: Team['slug']) => {
  53. this.setState(prevState => ({
  54. projectTeams: [
  55. ...(prevState.projectTeams || []).filter(team => team.slug !== teamSlug),
  56. ],
  57. }));
  58. };
  59. handleAddedTeam = (team: Team) => {
  60. this.setState(prevState => ({
  61. projectTeams: [...(prevState.projectTeams || []), team],
  62. }));
  63. };
  64. handleAdd = (teamSlug: string) => {
  65. if (this.state.loading) {
  66. return;
  67. }
  68. const team = TeamStore.getBySlug(teamSlug);
  69. if (!team) {
  70. addErrorMessage(tct('Unable to find "[teamSlug]"', {teamSlug}));
  71. this.setState({error: true});
  72. return;
  73. }
  74. const {organization, project} = this.props;
  75. addTeamToProject(this.api, organization.slug, project.slug, team).then(
  76. () => {
  77. this.handleAddedTeam(team);
  78. },
  79. () => {
  80. this.setState({
  81. error: true,
  82. loading: false,
  83. });
  84. }
  85. );
  86. };
  87. handleCreateTeam = (e: React.MouseEvent) => {
  88. e.stopPropagation();
  89. e.preventDefault();
  90. const {project, organization} = this.props;
  91. if (!this.canCreateTeam()) {
  92. return;
  93. }
  94. openCreateTeamModal({
  95. project,
  96. organization,
  97. onClose: data => {
  98. addTeamToProject(this.api, organization.slug, project.slug, data).then(
  99. this.remountComponent,
  100. this.remountComponent
  101. );
  102. },
  103. });
  104. };
  105. renderBody() {
  106. const {project, organization} = this.props;
  107. const canCreateTeam = this.canCreateTeam();
  108. const hasAccess = organization.access.includes('project:write');
  109. const confirmRemove = t(
  110. 'This is the last team with access to this project. Removing it will mean only organization owners and managers will be able to access the project pages. Are you sure you want to remove this team from the project %s?',
  111. project.slug
  112. );
  113. const {projectTeams} = this.state;
  114. const menuHeader = (
  115. <StyledTeamsLabel>
  116. {t('Teams')}
  117. <Tooltip
  118. disabled={canCreateTeam}
  119. title={t('You must be a project admin to create teams')}
  120. position="top"
  121. >
  122. <StyledCreateTeamLink
  123. to="#create-team"
  124. disabled={!canCreateTeam}
  125. onClick={this.handleCreateTeam}
  126. >
  127. {t('Create Team')}
  128. </StyledCreateTeamLink>
  129. </Tooltip>
  130. </StyledTeamsLabel>
  131. );
  132. return (
  133. <div>
  134. <SettingsPageHeader title={t('Project Teams for %s', project.slug)} />
  135. <TeamSelect
  136. disabled={!hasAccess}
  137. enforceIdpProvisioned={false}
  138. organization={organization}
  139. menuHeader={menuHeader}
  140. selectedTeams={projectTeams ?? []}
  141. onAddTeam={this.handleAdd}
  142. onRemoveTeam={this.handleRemove}
  143. confirmLastTeamRemoveMessage={confirmRemove}
  144. />
  145. </div>
  146. );
  147. }
  148. }
  149. const StyledTeamsLabel = styled('div')`
  150. font-size: 0.875em;
  151. padding: ${space(0.5)} 0px;
  152. text-transform: uppercase;
  153. `;
  154. const StyledCreateTeamLink = styled(Link)`
  155. float: right;
  156. text-transform: none;
  157. ${p =>
  158. p.disabled &&
  159. css`
  160. cursor: not-allowed;
  161. color: ${p.theme.gray300};
  162. opacity: 0.6;
  163. `};
  164. `;
  165. export default ProjectTeams;