installedIntegration.tsx 8.3 KB

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