installedPlugin.tsx 6.2 KB

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