projectTeams.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 access = this.props.organization.access;
  35. return (
  36. access.includes('org:write') &&
  37. access.includes('team:write') &&
  38. access.includes('project:write')
  39. );
  40. };
  41. handleRemove = (teamSlug: Team['slug']) => {
  42. if (this.state.loading) {
  43. return;
  44. }
  45. const {organization, project} = this.props;
  46. removeTeamFromProject(this.api, organization.slug, project.slug, teamSlug)
  47. .then(() => this.handleRemovedTeam(teamSlug))
  48. .catch(() => {
  49. addErrorMessage(t('Could not remove the %s team', teamSlug));
  50. this.setState({loading: false});
  51. });
  52. };
  53. handleRemovedTeam = (teamSlug: Team['slug']) => {
  54. this.setState(prevState => ({
  55. projectTeams: [
  56. ...(prevState.projectTeams || []).filter(team => team.slug !== teamSlug),
  57. ],
  58. }));
  59. };
  60. handleAddedTeam = (team: Team) => {
  61. this.setState(prevState => ({
  62. projectTeams: [...(prevState.projectTeams || []), team],
  63. }));
  64. };
  65. handleAdd = (teamSlug: string) => {
  66. if (this.state.loading) {
  67. return;
  68. }
  69. const team = TeamStore.getBySlug(teamSlug);
  70. if (!team) {
  71. addErrorMessage(tct('Unable to find "[teamSlug]"', {teamSlug}));
  72. this.setState({error: true});
  73. return;
  74. }
  75. const {organization, project} = this.props;
  76. addTeamToProject(this.api, organization.slug, project.slug, team).then(
  77. () => {
  78. this.handleAddedTeam(team);
  79. },
  80. () => {
  81. this.setState({
  82. error: true,
  83. loading: false,
  84. });
  85. }
  86. );
  87. };
  88. handleCreateTeam = (e: React.MouseEvent) => {
  89. e.stopPropagation();
  90. e.preventDefault();
  91. const {project, organization} = this.props;
  92. if (!this.canCreateTeam()) {
  93. return;
  94. }
  95. openCreateTeamModal({
  96. project,
  97. organization,
  98. onClose: data => {
  99. addTeamToProject(this.api, organization.slug, project.slug, data).then(
  100. this.remountComponent,
  101. this.remountComponent
  102. );
  103. },
  104. });
  105. };
  106. renderBody() {
  107. const {project, organization} = this.props;
  108. const canCreateTeam = this.canCreateTeam();
  109. const hasAccess = organization.access.includes('project:write');
  110. const confirmRemove = t(
  111. '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?',
  112. project.slug
  113. );
  114. const {projectTeams} = this.state;
  115. const menuHeader = (
  116. <StyledTeamsLabel>
  117. {t('Teams')}
  118. <Tooltip
  119. disabled={canCreateTeam}
  120. title={t('You must be a project admin to create teams')}
  121. position="top"
  122. >
  123. <StyledCreateTeamLink
  124. to="#create-team"
  125. disabled={!canCreateTeam}
  126. onClick={this.handleCreateTeam}
  127. >
  128. {t('Create Team')}
  129. </StyledCreateTeamLink>
  130. </Tooltip>
  131. </StyledTeamsLabel>
  132. );
  133. return (
  134. <div>
  135. <SettingsPageHeader title={t('Project Teams for %s', project.slug)} />
  136. <TeamSelect
  137. disabled={!hasAccess}
  138. enforceIdpProvisioned={false}
  139. organization={organization}
  140. menuHeader={menuHeader}
  141. selectedTeams={projectTeams ?? []}
  142. onAddTeam={this.handleAdd}
  143. onRemoveTeam={this.handleRemove}
  144. confirmLastTeamRemoveMessage={confirmRemove}
  145. />
  146. </div>
  147. );
  148. }
  149. }
  150. const StyledTeamsLabel = styled('div')`
  151. font-size: 0.875em;
  152. padding: ${space(0.5)} 0px;
  153. text-transform: uppercase;
  154. `;
  155. const StyledCreateTeamLink = styled(Link)`
  156. float: right;
  157. text-transform: none;
  158. ${p =>
  159. p.disabled &&
  160. css`
  161. cursor: not-allowed;
  162. color: ${p.theme.gray300};
  163. opacity: 0.6;
  164. `};
  165. `;
  166. export default ProjectTeams;