teamDetails.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import {cloneElement, isValidElement, useState} from 'react';
  2. import type {RouteComponentProps} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
  5. import {joinTeam} from 'sentry/actionCreators/teams';
  6. import Alert from 'sentry/components/alert';
  7. import Button from 'sentry/components/button';
  8. import IdBadge from 'sentry/components/idBadge';
  9. import ListLink from 'sentry/components/links/listLink';
  10. import LoadingIndicator from 'sentry/components/loadingIndicator';
  11. import NavTabs from 'sentry/components/navTabs';
  12. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  13. import {t, tct} from 'sentry/locale';
  14. import recreateRoute from 'sentry/utils/recreateRoute';
  15. import useApi from 'sentry/utils/useApi';
  16. import useTeams from 'sentry/utils/useTeams';
  17. type Props = {
  18. children: React.ReactNode;
  19. } & RouteComponentProps<{orgId: string; teamId: string}, {}>;
  20. function TeamDetails({children, ...props}: Props) {
  21. const api = useApi();
  22. const [requesting, setRequesting] = useState(false);
  23. const {teams, initiallyLoaded} = useTeams({slugs: [props.params.teamId]});
  24. const team = teams.find(({slug}) => slug === props.params.teamId);
  25. function handleRequestAccess(teamSlug: string) {
  26. setRequesting(true);
  27. joinTeam(
  28. api,
  29. {
  30. orgId: props.params.orgId,
  31. teamId: teamSlug,
  32. },
  33. {
  34. success: () => {
  35. addSuccessMessage(
  36. tct('You have requested access to [team]', {
  37. team: `#${teamSlug}`,
  38. })
  39. );
  40. setRequesting(false);
  41. },
  42. error: () => {
  43. addErrorMessage(
  44. tct('Unable to request access to [team]', {
  45. team: `#${teamSlug}`,
  46. })
  47. );
  48. setRequesting(false);
  49. },
  50. }
  51. );
  52. }
  53. // `/organizations/${orgId}/teams/${teamId}`;
  54. const routePrefix = recreateRoute('', {
  55. routes: props.routes,
  56. params: props.params,
  57. stepBack: -1,
  58. });
  59. const navigationTabs = [
  60. <ListLink key={0} to={`${routePrefix}members/`}>
  61. {t('Members')}
  62. </ListLink>,
  63. <ListLink key={1} to={`${routePrefix}projects/`}>
  64. {t('Projects')}
  65. </ListLink>,
  66. <ListLink key={2} to={`${routePrefix}notifications/`}>
  67. {t('Notifications')}
  68. </ListLink>,
  69. <ListLink key={3} to={`${routePrefix}settings/`}>
  70. {t('Settings')}
  71. </ListLink>,
  72. ];
  73. if (!initiallyLoaded) {
  74. return <LoadingIndicator />;
  75. }
  76. if (!team) {
  77. return (
  78. <Alert type="warning">
  79. <div>{t('You do not have access to this team.')}</div>
  80. </Alert>
  81. );
  82. }
  83. return (
  84. <div>
  85. <SentryDocumentTitle title={t('Team Details')} orgSlug={props.params.orgId} />
  86. {team.hasAccess ? (
  87. <div>
  88. <h3>
  89. <IdBadge hideAvatar team={team} avatarSize={36} />
  90. </h3>
  91. <NavTabs underlined>{navigationTabs}</NavTabs>
  92. {isValidElement(children) ? cloneElement<any>(children, {team}) : null}
  93. </div>
  94. ) : (
  95. <Alert type="warning">
  96. <RequestAccessWrapper>
  97. {tct('You do not have access to the [teamSlug] team.', {
  98. teamSlug: <strong>{`#${team.slug}`}</strong>,
  99. })}
  100. <Button
  101. disabled={requesting || team.isPending}
  102. size="sm"
  103. onClick={() => handleRequestAccess(team.slug)}
  104. >
  105. {team.isPending ? t('Request Pending') : t('Request Access')}
  106. </Button>
  107. </RequestAccessWrapper>
  108. </Alert>
  109. )}
  110. </div>
  111. );
  112. }
  113. export default TeamDetails;
  114. const RequestAccessWrapper = styled('div')`
  115. display: flex;
  116. justify-content: space-between;
  117. align-items: center;
  118. `;