allTeamsRow.tsx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. import {Component} from 'react';
  2. import styled from '@emotion/styled';
  3. import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
  4. import {fetchOrganizationDetails} from 'sentry/actionCreators/organizations';
  5. import {joinTeam, leaveTeam} from 'sentry/actionCreators/teams';
  6. import TeamActions from 'sentry/actions/teamActions';
  7. import {Client} from 'sentry/api';
  8. import Button from 'sentry/components/button';
  9. import IdBadge from 'sentry/components/idBadge';
  10. import Link from 'sentry/components/links/link';
  11. import {PanelItem} from 'sentry/components/panels';
  12. import {t, tct, tn} from 'sentry/locale';
  13. import space from 'sentry/styles/space';
  14. import {Organization, Team} from 'sentry/types';
  15. import withApi from 'sentry/utils/withApi';
  16. type Props = {
  17. api: Client;
  18. openMembership: boolean;
  19. organization: Organization;
  20. team: Team;
  21. urlPrefix: string;
  22. };
  23. type State = {
  24. error: boolean;
  25. loading: boolean;
  26. };
  27. class AllTeamsRow extends Component<Props, State> {
  28. state: State = {
  29. loading: false,
  30. error: false,
  31. };
  32. reloadProjects() {
  33. const {organization} = this.props;
  34. // After a change in teams has happened, refresh the project store
  35. fetchOrganizationDetails(organization.slug, {
  36. loadProjects: true,
  37. });
  38. }
  39. handleRequestAccess = () => {
  40. const {team} = this.props;
  41. try {
  42. this.joinTeam({
  43. successMessage: tct('You have requested access to [team]', {
  44. team: `#${team.slug}`,
  45. }),
  46. errorMessage: tct('Unable to request access to [team]', {
  47. team: `#${team.slug}`,
  48. }),
  49. });
  50. // Update team so that `isPending` is true
  51. TeamActions.updateSuccess(team.slug, {
  52. ...team,
  53. isPending: true,
  54. });
  55. } catch (_err) {
  56. // No need to do anything
  57. }
  58. };
  59. handleJoinTeam = async () => {
  60. const {team} = this.props;
  61. await this.joinTeam({
  62. successMessage: tct('You have joined [team]', {
  63. team: `#${team.slug}`,
  64. }),
  65. errorMessage: tct('Unable to join [team]', {
  66. team: `#${team.slug}`,
  67. }),
  68. });
  69. this.reloadProjects();
  70. };
  71. joinTeam = ({
  72. successMessage,
  73. errorMessage,
  74. }: {
  75. errorMessage: React.ReactNode;
  76. successMessage: React.ReactNode;
  77. }) => {
  78. const {api, organization, team} = this.props;
  79. this.setState({
  80. loading: true,
  81. });
  82. return new Promise<void>((resolve, reject) =>
  83. joinTeam(
  84. api,
  85. {
  86. orgId: organization.slug,
  87. teamId: team.slug,
  88. },
  89. {
  90. success: () => {
  91. this.setState({
  92. loading: false,
  93. error: false,
  94. });
  95. addSuccessMessage(successMessage);
  96. resolve();
  97. },
  98. error: () => {
  99. this.setState({
  100. loading: false,
  101. error: true,
  102. });
  103. addErrorMessage(errorMessage);
  104. reject(new Error('Unable to join team'));
  105. },
  106. }
  107. )
  108. );
  109. };
  110. handleLeaveTeam = () => {
  111. const {api, organization, team} = this.props;
  112. this.setState({
  113. loading: true,
  114. });
  115. leaveTeam(
  116. api,
  117. {
  118. orgId: organization.slug,
  119. teamId: team.slug,
  120. },
  121. {
  122. success: () => {
  123. this.setState({
  124. loading: false,
  125. error: false,
  126. });
  127. addSuccessMessage(
  128. tct('You have left [team]', {
  129. team: `#${team.slug}`,
  130. })
  131. );
  132. // Reload ProjectsStore
  133. this.reloadProjects();
  134. },
  135. error: () => {
  136. this.setState({
  137. loading: false,
  138. error: true,
  139. });
  140. addErrorMessage(
  141. tct('Unable to leave [team]', {
  142. team: `#${team.slug}`,
  143. })
  144. );
  145. },
  146. }
  147. );
  148. };
  149. getTeamRoleName = () => {
  150. const {organization, team} = this.props;
  151. if (!organization.features.includes('team-roles') || !team.teamRole) {
  152. return null;
  153. }
  154. const {teamRoleList} = organization;
  155. const roleName = teamRoleList.find(r => r.id === team.teamRole)?.name;
  156. return roleName;
  157. };
  158. render() {
  159. const {team, urlPrefix, openMembership} = this.props;
  160. const display = (
  161. <IdBadge
  162. team={team}
  163. avatarSize={36}
  164. description={tn('%s Member', '%s Members', team.memberCount)}
  165. />
  166. );
  167. // You can only view team details if you have access to team -- this should account
  168. // for your role + org open membership
  169. const canViewTeam = team.hasAccess;
  170. return (
  171. <TeamPanelItem>
  172. <div>
  173. {canViewTeam ? (
  174. <TeamLink data-test-id="team-link" to={`${urlPrefix}teams/${team.slug}/`}>
  175. {display}
  176. </TeamLink>
  177. ) : (
  178. display
  179. )}
  180. </div>
  181. <div>{this.getTeamRoleName()}</div>
  182. <div>
  183. {this.state.loading ? (
  184. <Button size="sm" disabled>
  185. ...
  186. </Button>
  187. ) : team.isMember ? (
  188. <Button size="sm" onClick={this.handleLeaveTeam}>
  189. {t('Leave Team')}
  190. </Button>
  191. ) : team.isPending ? (
  192. <Button
  193. size="sm"
  194. disabled
  195. title={t(
  196. 'Your request to join this team is being reviewed by organization owners'
  197. )}
  198. >
  199. {t('Request Pending')}
  200. </Button>
  201. ) : openMembership ? (
  202. <Button size="sm" onClick={this.handleJoinTeam}>
  203. {t('Join Team')}
  204. </Button>
  205. ) : (
  206. <Button size="sm" onClick={this.handleRequestAccess}>
  207. {t('Request Access')}
  208. </Button>
  209. )}
  210. </div>
  211. </TeamPanelItem>
  212. );
  213. }
  214. }
  215. const TeamLink = styled(Link)`
  216. display: inline-block;
  217. &.focus-visible {
  218. margin: -${space(1)};
  219. padding: ${space(1)};
  220. background: #f2eff5;
  221. border-radius: 3px;
  222. outline: none;
  223. }
  224. `;
  225. export {AllTeamsRow};
  226. export default withApi(AllTeamsRow);
  227. const TeamPanelItem = styled(PanelItem)`
  228. display: grid;
  229. grid-template-columns: minmax(150px, 4fr) minmax(90px, 1fr) min-content;
  230. gap: ${space(2)};
  231. align-items: center;
  232. > div:last-child {
  233. margin-left: auto;
  234. }
  235. `;