teamNotifications.tsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import {Fragment} 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 {hasEveryAccess} from 'sentry/components/acl/access';
  6. import {Button} from 'sentry/components/button';
  7. import Confirm from 'sentry/components/confirm';
  8. import type DeprecatedAsyncComponent from 'sentry/components/deprecatedAsyncComponent';
  9. import EmptyMessage from 'sentry/components/emptyMessage';
  10. import TextField from 'sentry/components/forms/fields/textField';
  11. import ExternalLink from 'sentry/components/links/externalLink';
  12. import Panel from 'sentry/components/panels/panel';
  13. import PanelBody from 'sentry/components/panels/panelBody';
  14. import PanelHeader from 'sentry/components/panels/panelHeader';
  15. import {Tooltip} from 'sentry/components/tooltip';
  16. import {IconDelete} from 'sentry/icons';
  17. import {t, tct} from 'sentry/locale';
  18. import {space} from 'sentry/styles/space';
  19. import type {ExternalTeam, Integration} from 'sentry/types/integrations';
  20. import type {Organization, Team} from 'sentry/types/organization';
  21. import {toTitleCase} from 'sentry/utils/string/toTitleCase';
  22. import withOrganization from 'sentry/utils/withOrganization';
  23. import DeprecatedAsyncView from 'sentry/views/deprecatedAsyncView';
  24. import PermissionAlert from 'sentry/views/settings/project/permissionAlert';
  25. type Props = RouteComponentProps<{teamId: string}, {}> & {
  26. organization: Organization;
  27. team: Team;
  28. };
  29. type State = DeprecatedAsyncView['state'] & {
  30. integrations: Integration[];
  31. teamDetails: Team;
  32. };
  33. const DOCS_LINK =
  34. 'https://docs.sentry.io/product/integrations/notification-incidents/slack/#team-notifications';
  35. const NOTIFICATION_PROVIDERS = ['slack'];
  36. class TeamNotificationSettings extends DeprecatedAsyncView<Props, State> {
  37. getTitle() {
  38. return 'Team Notification Settings';
  39. }
  40. getEndpoints(): ReturnType<DeprecatedAsyncComponent['getEndpoints']> {
  41. const {organization, team} = this.props;
  42. return [
  43. [
  44. 'teamDetails',
  45. `/teams/${organization.slug}/${team.slug}/`,
  46. {query: {expand: ['externalTeams']}},
  47. ],
  48. [
  49. 'integrations',
  50. `/organizations/${organization.slug}/integrations/`,
  51. {query: {includeConfig: 0}},
  52. ],
  53. ];
  54. }
  55. handleDelete = async (mapping: ExternalTeam) => {
  56. try {
  57. const {organization, team} = this.props;
  58. const endpoint = `/teams/${organization.slug}/${team.slug}/external-teams/${mapping.id}/`;
  59. await this.api.requestPromise(endpoint, {
  60. method: 'DELETE',
  61. });
  62. addSuccessMessage(t('Deletion successful'));
  63. this.fetchData();
  64. } catch {
  65. addErrorMessage(t('An error occurred'));
  66. }
  67. };
  68. renderBody() {
  69. const {team} = this.props;
  70. return (
  71. <Fragment>
  72. <PermissionAlert access={['team:write']} team={team} />
  73. <Panel>
  74. <PanelHeader>{t('Notifications')}</PanelHeader>
  75. <PanelBody>{this.renderPanelBody()}</PanelBody>
  76. </Panel>
  77. </Fragment>
  78. );
  79. }
  80. renderPanelBody() {
  81. const {organization, team} = this.props;
  82. const {teamDetails, integrations} = this.state;
  83. const notificationIntegrations = integrations.filter(integration =>
  84. NOTIFICATION_PROVIDERS.includes(integration.provider.key)
  85. );
  86. if (!notificationIntegrations.length) {
  87. return (
  88. <EmptyMessage>
  89. {t('No Notification Integrations have been installed yet.')}
  90. </EmptyMessage>
  91. );
  92. }
  93. const externalTeams = (teamDetails.externalTeams || []).filter(externalTeam =>
  94. NOTIFICATION_PROVIDERS.includes(externalTeam.provider)
  95. );
  96. if (!externalTeams.length) {
  97. return (
  98. <EmptyMessage>
  99. <div>{t('No teams have been linked yet.')}</div>
  100. <NotDisabledSubText>
  101. {tct('Head over to Slack and type [code] to get started. [link].', {
  102. code: <code>/sentry link team</code>,
  103. link: <ExternalLink href={DOCS_LINK}>{t('Learn more')}</ExternalLink>,
  104. })}
  105. </NotDisabledSubText>
  106. </EmptyMessage>
  107. );
  108. }
  109. const integrationsById = Object.fromEntries(
  110. notificationIntegrations.map(integration => [integration.id, integration])
  111. );
  112. const hasWriteAccess = hasEveryAccess(['team:write'], {organization, team});
  113. return externalTeams.map(externalTeam => (
  114. <FormFieldWrapper key={externalTeam.id}>
  115. <StyledFormField
  116. disabled
  117. label={
  118. <div>
  119. <NotDisabledText>
  120. {toTitleCase(externalTeam.provider)}:
  121. {integrationsById[externalTeam.integrationId].name}
  122. </NotDisabledText>
  123. <NotDisabledSubText>
  124. {tct('Unlink this channel in Slack with [code]. [link].', {
  125. code: <code>/sentry unlink team</code>,
  126. link: <ExternalLink href={DOCS_LINK}>{t('Learn more')}</ExternalLink>,
  127. })}
  128. </NotDisabledSubText>
  129. </div>
  130. }
  131. labelText={t('Unlink this channel in slack with `/slack unlink team`')}
  132. name="externalName"
  133. value={externalTeam.externalName}
  134. />
  135. <DeleteButtonWrapper>
  136. <Tooltip
  137. title={t(
  138. 'You must be an organization owner, manager or admin to remove a Slack team link'
  139. )}
  140. disabled={hasWriteAccess}
  141. >
  142. <Confirm
  143. disabled={!hasWriteAccess}
  144. onConfirm={() => this.handleDelete(externalTeam)}
  145. message={t('Are you sure you want to remove this Slack team link?')}
  146. >
  147. <Button
  148. size="sm"
  149. icon={<IconDelete size="md" />}
  150. aria-label={t('delete')}
  151. disabled={!hasWriteAccess}
  152. />
  153. </Confirm>
  154. </Tooltip>
  155. </DeleteButtonWrapper>
  156. </FormFieldWrapper>
  157. ));
  158. }
  159. }
  160. export default withOrganization(TeamNotificationSettings);
  161. const NotDisabledText = styled('div')`
  162. color: ${p => p.theme.textColor};
  163. line-height: ${space(2)};
  164. `;
  165. const NotDisabledSubText = styled('div')`
  166. color: ${p => p.theme.subText};
  167. font-size: ${p => p.theme.fontSizeRelativeSmall};
  168. line-height: 1.4;
  169. margin-top: ${space(1)};
  170. `;
  171. const FormFieldWrapper = styled('div')`
  172. display: flex;
  173. align-items: center;
  174. justify-content: flex-start;
  175. `;
  176. const StyledFormField = styled(TextField)`
  177. flex: 1;
  178. `;
  179. const DeleteButtonWrapper = styled('div')`
  180. margin-right: ${space(4)};
  181. padding-right: ${space(0.5)};
  182. `;