installedIntegration.tsx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 type {
  14. Integration,
  15. IntegrationProvider,
  16. ObjectStatus,
  17. Organization,
  18. } from 'sentry/types';
  19. import type {IntegrationAnalyticsKey} from 'sentry/utils/analytics/integrations';
  20. import {getIntegrationStatus} from 'sentry/utils/integrationUtil';
  21. import {AddIntegrationButton} from './addIntegrationButton';
  22. import IntegrationItem from './integrationItem';
  23. type Props = {
  24. integration: Integration;
  25. onDisable: (integration: Integration) => void;
  26. onRemove: (integration: Integration) => void;
  27. organization: Organization;
  28. provider: IntegrationProvider;
  29. trackIntegrationAnalytics: (eventKey: IntegrationAnalyticsKey) => void; // analytics callback
  30. requiresUpgrade?: boolean;
  31. };
  32. export default class InstalledIntegration extends Component<Props> {
  33. handleUninstallClick = () => {
  34. this.props.trackIntegrationAnalytics('integrations.uninstall_clicked');
  35. };
  36. getRemovalBodyAndText(aspects: Integration['provider']['aspects']) {
  37. if (aspects?.removal_dialog) {
  38. return {
  39. body: aspects.removal_dialog.body,
  40. actionText: aspects.removal_dialog.actionText,
  41. };
  42. }
  43. return {
  44. body: t(
  45. 'Deleting this integration will remove any project associated data. This action cannot be undone. Are you sure you want to delete this integration?'
  46. ),
  47. actionText: t('Delete'),
  48. };
  49. }
  50. handleRemove(integration: Integration) {
  51. this.props.onRemove(integration);
  52. this.props.trackIntegrationAnalytics('integrations.uninstall_completed');
  53. }
  54. get integrationStatus() {
  55. const {integration} = this.props;
  56. return getIntegrationStatus(integration);
  57. }
  58. get removeConfirmProps() {
  59. const {integration} = this.props;
  60. const {body, actionText} = this.getRemovalBodyAndText(integration.provider.aspects);
  61. const message = (
  62. <Fragment>
  63. <Alert type="error" showIcon>
  64. {t('Deleting this integration has consequences!')}
  65. </Alert>
  66. {body}
  67. </Fragment>
  68. );
  69. return {
  70. message,
  71. confirmText: actionText,
  72. onConfirm: () => this.handleRemove(integration),
  73. };
  74. }
  75. get disableConfirmProps() {
  76. const {integration} = this.props;
  77. const {body, actionText} = integration.provider.aspects.disable_dialog || {};
  78. const message = (
  79. <Fragment>
  80. <Alert type="error" showIcon>
  81. {t('This integration cannot be removed in Sentry')}
  82. </Alert>
  83. {body}
  84. </Fragment>
  85. );
  86. return {
  87. message,
  88. confirmText: actionText,
  89. onConfirm: () => this.props.onDisable(integration),
  90. };
  91. }
  92. render() {
  93. const {integration, organization, provider, requiresUpgrade} = this.props;
  94. const removeConfirmProps =
  95. this.integrationStatus === 'active' && integration.provider.canDisable
  96. ? this.disableConfirmProps
  97. : this.removeConfirmProps;
  98. const allowMemberConfiguration = ['github', 'gitlab'].includes(
  99. this.props.provider.key
  100. );
  101. return (
  102. <Access access={['org:integrations']}>
  103. {({hasAccess}) => {
  104. const disableAction = !(hasAccess && this.integrationStatus === 'active');
  105. return (
  106. <Fragment>
  107. <IntegrationItemBox>
  108. <IntegrationItem integration={integration} />
  109. </IntegrationItemBox>
  110. <div>
  111. <Tooltip
  112. disabled={allowMemberConfiguration || hasAccess}
  113. position="left"
  114. title={t(
  115. 'You must be an organization owner, manager or admin to configure'
  116. )}
  117. >
  118. {requiresUpgrade && (
  119. <AddIntegrationButton
  120. analyticsParams={{
  121. view: 'integrations_directory_integration_detail',
  122. already_installed: true,
  123. }}
  124. buttonText={t('Update Now')}
  125. data-test-id="integration-upgrade-button"
  126. disabled={disableAction}
  127. icon={<IconWarning />}
  128. onAddIntegration={() => {}}
  129. organization={organization}
  130. provider={provider}
  131. priority="primary"
  132. size="sm"
  133. />
  134. )}
  135. <StyledButton
  136. borderless
  137. icon={<IconSettings />}
  138. disabled={!allowMemberConfiguration && disableAction}
  139. to={`/settings/${organization.slug}/integrations/${provider.key}/${integration.id}/`}
  140. data-test-id="integration-configure-button"
  141. >
  142. {t('Configure')}
  143. </StyledButton>
  144. </Tooltip>
  145. </div>
  146. <div>
  147. <Tooltip
  148. disabled={hasAccess}
  149. title={t(
  150. 'You must be an organization owner, manager or admin to uninstall'
  151. )}
  152. >
  153. <Confirm
  154. priority="danger"
  155. onConfirming={this.handleUninstallClick}
  156. disabled={!hasAccess}
  157. {...removeConfirmProps}
  158. >
  159. <StyledButton
  160. disabled={!hasAccess}
  161. borderless
  162. icon={<IconDelete />}
  163. data-test-id="integration-remove-button"
  164. >
  165. {t('Uninstall')}
  166. </StyledButton>
  167. </Confirm>
  168. </Tooltip>
  169. </div>
  170. <StyledIntegrationStatus
  171. status={this.integrationStatus}
  172. // Let the hook handle the alert for disabled org integrations
  173. hideTooltip={integration.organizationIntegrationStatus === 'disabled'}
  174. />
  175. </Fragment>
  176. );
  177. }}
  178. </Access>
  179. );
  180. }
  181. }
  182. const StyledButton = styled(Button)`
  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. `;