installedIntegration.tsx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import {Component, Fragment} from 'react';
  2. import {useTheme} from '@emotion/react';
  3. import styled from '@emotion/styled';
  4. import Access from 'sentry/components/acl/access';
  5. import {Alert} from 'sentry/components/alert';
  6. import {Button} from 'sentry/components/button';
  7. import CircleIndicator from 'sentry/components/circleIndicator';
  8. import Confirm from 'sentry/components/confirm';
  9. import {Tooltip} from 'sentry/components/tooltip';
  10. import {IconDelete, IconSettings, IconWarning} from 'sentry/icons';
  11. import {t} from 'sentry/locale';
  12. import {space} from 'sentry/styles/space';
  13. import {Integration, IntegrationProvider, ObjectStatus, Organization} from 'sentry/types';
  14. import {IntegrationAnalyticsKey} from 'sentry/utils/analytics/integrations';
  15. import {getIntegrationStatus} from 'sentry/utils/integrationUtil';
  16. import {AddIntegrationButton} from './addIntegrationButton';
  17. import IntegrationItem from './integrationItem';
  18. type Props = {
  19. integration: Integration;
  20. onDisable: (integration: Integration) => void;
  21. onRemove: (integration: Integration) => void;
  22. organization: Organization;
  23. provider: IntegrationProvider;
  24. trackIntegrationAnalytics: (eventKey: IntegrationAnalyticsKey) => void; // analytics callback
  25. requiresUpgrade?: boolean;
  26. };
  27. export default class InstalledIntegration extends Component<Props> {
  28. handleUninstallClick = () => {
  29. this.props.trackIntegrationAnalytics('integrations.uninstall_clicked');
  30. };
  31. getRemovalBodyAndText(aspects: Integration['provider']['aspects']) {
  32. if (aspects && aspects.removal_dialog) {
  33. return {
  34. body: aspects.removal_dialog.body,
  35. actionText: aspects.removal_dialog.actionText,
  36. };
  37. }
  38. return {
  39. body: t(
  40. 'Deleting this integration will remove any project associated data. This action cannot be undone. Are you sure you want to delete this integration?'
  41. ),
  42. actionText: t('Delete'),
  43. };
  44. }
  45. handleRemove(integration: Integration) {
  46. this.props.onRemove(integration);
  47. this.props.trackIntegrationAnalytics('integrations.uninstall_completed');
  48. }
  49. get integrationStatus() {
  50. const {integration} = this.props;
  51. return getIntegrationStatus(integration);
  52. }
  53. get removeConfirmProps() {
  54. const {integration} = this.props;
  55. const {body, actionText} = this.getRemovalBodyAndText(integration.provider.aspects);
  56. const message = (
  57. <Fragment>
  58. <Alert type="error" showIcon>
  59. {t('Deleting this integration has consequences!')}
  60. </Alert>
  61. {body}
  62. </Fragment>
  63. );
  64. return {
  65. message,
  66. confirmText: actionText,
  67. onConfirm: () => this.handleRemove(integration),
  68. };
  69. }
  70. get disableConfirmProps() {
  71. const {integration} = this.props;
  72. const {body, actionText} = integration.provider.aspects.disable_dialog || {};
  73. const message = (
  74. <Fragment>
  75. <Alert type="error" showIcon>
  76. {t('This integration cannot be removed in Sentry')}
  77. </Alert>
  78. {body}
  79. </Fragment>
  80. );
  81. return {
  82. message,
  83. confirmText: actionText,
  84. onConfirm: () => this.props.onDisable(integration),
  85. };
  86. }
  87. render() {
  88. const {integration, organization, provider, requiresUpgrade} = this.props;
  89. const removeConfirmProps =
  90. this.integrationStatus === 'active' && integration.provider.canDisable
  91. ? this.disableConfirmProps
  92. : this.removeConfirmProps;
  93. const allowMemberConfiguration = ['github', 'gitlab'].includes(
  94. this.props.provider.key
  95. );
  96. return (
  97. <Access access={['org:integrations']}>
  98. {({hasAccess}) => {
  99. const disableAction = !(hasAccess && this.integrationStatus === 'active');
  100. return (
  101. <Fragment>
  102. <IntegrationItemBox>
  103. <IntegrationItem integration={integration} />
  104. </IntegrationItemBox>
  105. <div>
  106. <Tooltip
  107. disabled={allowMemberConfiguration || hasAccess}
  108. position="left"
  109. title={t(
  110. 'You must be an organization owner, manager or admin to configure'
  111. )}
  112. >
  113. {requiresUpgrade && (
  114. <AddIntegrationButton
  115. analyticsParams={{
  116. view: 'integrations_directory_integration_detail',
  117. already_installed: true,
  118. }}
  119. buttonText={t('Update Now')}
  120. data-test-id="integration-upgrade-button"
  121. disabled={disableAction}
  122. icon={<IconWarning />}
  123. onAddIntegration={() => {}}
  124. organization={organization}
  125. provider={provider}
  126. priority="primary"
  127. size="sm"
  128. />
  129. )}
  130. <StyledButton
  131. borderless
  132. icon={<IconSettings />}
  133. disabled={!allowMemberConfiguration && disableAction}
  134. to={`/settings/${organization.slug}/integrations/${provider.key}/${integration.id}/`}
  135. data-test-id="integration-configure-button"
  136. >
  137. {t('Configure')}
  138. </StyledButton>
  139. </Tooltip>
  140. </div>
  141. <div>
  142. <Tooltip
  143. disabled={hasAccess}
  144. title={t(
  145. 'You must be an organization owner, manager or admin to uninstall'
  146. )}
  147. >
  148. <Confirm
  149. priority="danger"
  150. onConfirming={this.handleUninstallClick}
  151. disabled={!hasAccess}
  152. {...removeConfirmProps}
  153. >
  154. <StyledButton
  155. disabled={!hasAccess}
  156. borderless
  157. icon={<IconDelete />}
  158. data-test-id="integration-remove-button"
  159. >
  160. {t('Uninstall')}
  161. </StyledButton>
  162. </Confirm>
  163. </Tooltip>
  164. </div>
  165. <StyledIntegrationStatus
  166. status={this.integrationStatus}
  167. // Let the hook handle the alert for disabled org integrations
  168. hideTooltip={integration.organizationIntegrationStatus === 'disabled'}
  169. />
  170. </Fragment>
  171. );
  172. }}
  173. </Access>
  174. );
  175. }
  176. }
  177. const StyledButton = styled(Button)`
  178. color: ${p => p.theme.gray300};
  179. `;
  180. const IntegrationItemBox = styled('div')`
  181. flex: 1;
  182. `;
  183. function IntegrationStatus(
  184. props: React.HTMLAttributes<HTMLDivElement> & {
  185. status: ObjectStatus;
  186. hideTooltip?: boolean;
  187. }
  188. ) {
  189. const theme = useTheme();
  190. const {status, hideTooltip, ...p} = props;
  191. const color = status === 'active' ? theme.success : theme.gray300;
  192. const inner = (
  193. <div {...p}>
  194. <CircleIndicator size={6} color={color} />
  195. <IntegrationStatusText data-test-id="integration-status">{`${
  196. status === 'active'
  197. ? t('enabled')
  198. : status === 'pending_deletion'
  199. ? t('pending deletion')
  200. : status === 'disabled'
  201. ? t('disabled')
  202. : t('unknown')
  203. }`}</IntegrationStatusText>
  204. </div>
  205. );
  206. return hideTooltip ? (
  207. inner
  208. ) : (
  209. <Tooltip
  210. title={
  211. status === 'active'
  212. ? t('This integration can be disabled by clicking the Uninstall button')
  213. : status === 'disabled'
  214. ? t('This integration has been disconnected from the external provider')
  215. : t('This integration is pending deletion.')
  216. }
  217. >
  218. {inner}
  219. </Tooltip>
  220. );
  221. }
  222. const StyledIntegrationStatus = styled(IntegrationStatus)`
  223. display: flex;
  224. align-items: center;
  225. color: ${p => p.theme.gray300};
  226. font-weight: light;
  227. text-transform: capitalize;
  228. &:before {
  229. content: '|';
  230. color: ${p => p.theme.gray200};
  231. margin-right: ${space(1)};
  232. font-weight: normal;
  233. }
  234. `;
  235. const IntegrationStatusText = styled('div')`
  236. margin: 0 ${space(0.75)} 0 ${space(0.5)};
  237. `;