allTeamsRow.tsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. render() {
  150. const {team, urlPrefix, openMembership} = this.props;
  151. const display = (
  152. <IdBadge
  153. team={team}
  154. avatarSize={36}
  155. description={tn('%s Member', '%s Members', team.memberCount)}
  156. />
  157. );
  158. // You can only view team details if you have access to team -- this should account
  159. // for your role + org open membership
  160. const canViewTeam = team.hasAccess;
  161. return (
  162. <TeamPanelItem>
  163. <TeamNameWrapper>
  164. {canViewTeam ? (
  165. <TeamLink to={`${urlPrefix}teams/${team.slug}/`}>{display}</TeamLink>
  166. ) : (
  167. display
  168. )}
  169. </TeamNameWrapper>
  170. <Spacer>
  171. {this.state.loading ? (
  172. <Button size="sm" disabled>
  173. ...
  174. </Button>
  175. ) : team.isMember ? (
  176. <Button size="sm" onClick={this.handleLeaveTeam}>
  177. {t('Leave Team')}
  178. </Button>
  179. ) : team.isPending ? (
  180. <Button
  181. size="sm"
  182. disabled
  183. title={t(
  184. 'Your request to join this team is being reviewed by organization owners'
  185. )}
  186. >
  187. {t('Request Pending')}
  188. </Button>
  189. ) : openMembership ? (
  190. <Button size="sm" onClick={this.handleJoinTeam}>
  191. {t('Join Team')}
  192. </Button>
  193. ) : (
  194. <Button size="sm" onClick={this.handleRequestAccess}>
  195. {t('Request Access')}
  196. </Button>
  197. )}
  198. </Spacer>
  199. </TeamPanelItem>
  200. );
  201. }
  202. }
  203. const TeamLink = styled(Link)`
  204. display: inline-block;
  205. &.focus-visible {
  206. margin: -${space(1)};
  207. padding: ${space(1)};
  208. background: #f2eff5;
  209. border-radius: 3px;
  210. outline: none;
  211. }
  212. `;
  213. export {AllTeamsRow};
  214. export default withApi(AllTeamsRow);
  215. const TeamPanelItem = styled(PanelItem)`
  216. padding: 0;
  217. align-items: center;
  218. `;
  219. const Spacer = styled('div')`
  220. padding: ${space(2)};
  221. `;
  222. const TeamNameWrapper = styled(Spacer)`
  223. flex: 1;
  224. `;