installedIntegration.tsx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. 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={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={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. const 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 === 'disabled'
  199. ? t('disabled')
  200. : t('pending deletion')
  201. }`}</IntegrationStatusText>
  202. </div>
  203. );
  204. return hideTooltip ? (
  205. inner
  206. ) : (
  207. <Tooltip
  208. title={
  209. status === 'active'
  210. ? t('This integration can be disabled by clicking the Uninstall button')
  211. : status === 'disabled'
  212. ? t('This integration has been disconnected from the external provider')
  213. : t('This integration is pending deletion.')
  214. }
  215. >
  216. {inner}
  217. </Tooltip>
  218. );
  219. };
  220. const StyledIntegrationStatus = styled(IntegrationStatus)`
  221. display: flex;
  222. align-items: center;
  223. color: ${p => p.theme.gray300};
  224. font-weight: light;
  225. text-transform: capitalize;
  226. &:before {
  227. content: '|';
  228. color: ${p => p.theme.gray200};
  229. margin-right: ${space(1)};
  230. font-weight: normal;
  231. }
  232. `;
  233. const IntegrationStatusText = styled('div')`
  234. margin: 0 ${space(0.75)} 0 ${space(0.5)};
  235. `;