installedPlugin.tsx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import {Component, Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import {
  4. addErrorMessage,
  5. addLoadingMessage,
  6. addSuccessMessage,
  7. } from 'sentry/actionCreators/indicator';
  8. import type {Client} from 'sentry/api';
  9. import Access from 'sentry/components/acl/access';
  10. import {Alert} from 'sentry/components/alert';
  11. import {Button, LinkButton} from 'sentry/components/button';
  12. import Confirm from 'sentry/components/confirm';
  13. import ProjectBadge from 'sentry/components/idBadge/projectBadge';
  14. import Switch from 'sentry/components/switchButton';
  15. import {IconDelete, IconSettings} from 'sentry/icons';
  16. import {t} from 'sentry/locale';
  17. import {space} from 'sentry/styles/space';
  18. import type {PluginNoProject, PluginProjectItem} from 'sentry/types/integrations';
  19. import type {Organization} from 'sentry/types/organization';
  20. import type {AvatarProject} from 'sentry/types/project';
  21. import type {IntegrationAnalyticsKey} from 'sentry/utils/analytics/integrations';
  22. import withApi from 'sentry/utils/withApi';
  23. type Props = {
  24. api: Client;
  25. onPluginEnableStatusChange: (projectId: string, status: boolean) => void;
  26. onResetConfiguration: (projectId: string) => void;
  27. organization: Organization;
  28. plugin: PluginNoProject;
  29. projectItem: PluginProjectItem;
  30. trackIntegrationAnalytics: (eventKey: IntegrationAnalyticsKey) => void; // analytics callback
  31. className?: string;
  32. };
  33. export class InstalledPlugin extends Component<Props> {
  34. get projectId() {
  35. return this.props.projectItem.projectId;
  36. }
  37. getConfirmMessage() {
  38. return (
  39. <Fragment>
  40. <Alert type="error" showIcon>
  41. {t(
  42. 'Deleting this installation will disable the integration for this project and remove any configurations.'
  43. )}
  44. </Alert>
  45. </Fragment>
  46. );
  47. }
  48. pluginUpdate = async (data: object, method: 'POST' | 'DELETE' = 'POST') => {
  49. const {organization, projectItem, plugin} = this.props;
  50. // no try/catch so the caller will have to have it
  51. await this.props.api.requestPromise(
  52. `/projects/${organization.slug}/${projectItem.projectSlug}/plugins/${plugin.id}/`,
  53. {
  54. method,
  55. data,
  56. }
  57. );
  58. };
  59. updatePluginEnableStatus = async (enabled: boolean) => {
  60. if (enabled) {
  61. await this.pluginUpdate({enabled});
  62. } else {
  63. await this.pluginUpdate({}, 'DELETE');
  64. }
  65. };
  66. handleReset = async () => {
  67. try {
  68. addLoadingMessage(t('Removing...'));
  69. await this.pluginUpdate({reset: true});
  70. addSuccessMessage(t('Configuration was removed'));
  71. this.props.onResetConfiguration(this.projectId);
  72. this.props.trackIntegrationAnalytics('integrations.uninstall_completed');
  73. } catch (_err) {
  74. addErrorMessage(t('Unable to remove configuration'));
  75. }
  76. };
  77. handleUninstallClick = () => {
  78. this.props.trackIntegrationAnalytics('integrations.uninstall_clicked');
  79. };
  80. toggleEnablePlugin = async (projectId: string, status: boolean = true) => {
  81. try {
  82. addLoadingMessage(t('Enabling...'));
  83. await this.updatePluginEnableStatus(status);
  84. addSuccessMessage(
  85. status ? t('Configuration was enabled.') : t('Configuration was disabled.')
  86. );
  87. this.props.onPluginEnableStatusChange(projectId, status);
  88. this.props.trackIntegrationAnalytics(
  89. status ? 'integrations.enabled' : 'integrations.disabled'
  90. );
  91. } catch (_err) {
  92. addErrorMessage(
  93. status
  94. ? t('Unable to enable configuration.')
  95. : t('Unable to disable configuration.')
  96. );
  97. }
  98. };
  99. get projectForBadge(): AvatarProject {
  100. // this function returns the project as needed for the ProjectBadge component
  101. const {projectItem} = this.props;
  102. return {
  103. slug: projectItem.projectSlug,
  104. platform: projectItem.projectPlatform ? projectItem.projectPlatform : undefined,
  105. };
  106. }
  107. render() {
  108. const {className, plugin, organization, projectItem} = this.props;
  109. return (
  110. <Container data-test-id="installed-plugin">
  111. <Access access={['org:integrations']}>
  112. {({hasAccess}) => (
  113. <IntegrationFlex className={className}>
  114. <IntegrationItemBox>
  115. <ProjectBadge project={this.projectForBadge} />
  116. </IntegrationItemBox>
  117. <div>
  118. <StyledLinkButton
  119. borderless
  120. icon={<IconSettings />}
  121. disabled={!hasAccess}
  122. to={`/settings/${organization.slug}/projects/${projectItem.projectSlug}/plugins/${plugin.id}/`}
  123. data-test-id="integration-configure-button"
  124. >
  125. {t('Configure')}
  126. </StyledLinkButton>
  127. </div>
  128. <div>
  129. <Confirm
  130. priority="danger"
  131. onConfirming={this.handleUninstallClick}
  132. disabled={!hasAccess}
  133. confirmText="Delete Installation"
  134. onConfirm={() => this.handleReset()}
  135. message={this.getConfirmMessage()}
  136. >
  137. <StyledButton
  138. disabled={!hasAccess}
  139. borderless
  140. icon={<IconDelete />}
  141. data-test-id="integration-remove-button"
  142. >
  143. {t('Uninstall')}
  144. </StyledButton>
  145. </Confirm>
  146. </div>
  147. <Switch
  148. isActive={projectItem.enabled}
  149. toggle={() =>
  150. this.toggleEnablePlugin(projectItem.projectId, !projectItem.enabled)
  151. }
  152. isDisabled={!hasAccess}
  153. />
  154. </IntegrationFlex>
  155. )}
  156. </Access>
  157. </Container>
  158. );
  159. }
  160. }
  161. export default withApi(InstalledPlugin);
  162. const Container = styled('div')`
  163. padding: ${space(2)};
  164. border: 1px solid ${p => p.theme.border};
  165. border-bottom: none;
  166. background-color: ${p => p.theme.background};
  167. &:last-child {
  168. border-bottom: 1px solid ${p => p.theme.border};
  169. }
  170. `;
  171. const StyledButton = styled(Button)`
  172. color: ${p => p.theme.gray300};
  173. `;
  174. const StyledLinkButton = styled(LinkButton)`
  175. color: ${p => p.theme.gray300};
  176. `;
  177. const IntegrationFlex = styled('div')`
  178. display: flex;
  179. align-items: center;
  180. `;
  181. const IntegrationItemBox = styled('div')`
  182. flex: 1 0 fit-content;
  183. box-sizing: border-box;
  184. display: flex;
  185. flex-direction: row;
  186. min-width: 0;
  187. `;