allTeamsRow.tsx 8.0 KB

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