installedIntegration.tsx 8.2 KB

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