installedPlugin.tsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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} 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 {
  19. AvatarProject,
  20. Organization,
  21. PluginNoProject,
  22. PluginProjectItem,
  23. } from 'sentry/types';
  24. import type {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. <StyledButton
  122. borderless
  123. icon={<IconSettings />}
  124. disabled={!hasAccess}
  125. to={`/settings/${organization.slug}/projects/${projectItem.projectSlug}/plugins/${plugin.id}/`}
  126. data-test-id="integration-configure-button"
  127. >
  128. {t('Configure')}
  129. </StyledButton>
  130. </div>
  131. <div>
  132. <Confirm
  133. priority="danger"
  134. onConfirming={this.handleUninstallClick}
  135. disabled={!hasAccess}
  136. confirmText="Delete Installation"
  137. onConfirm={() => this.handleReset()}
  138. message={this.getConfirmMessage()}
  139. >
  140. <StyledButton
  141. disabled={!hasAccess}
  142. borderless
  143. icon={<IconDelete />}
  144. data-test-id="integration-remove-button"
  145. >
  146. {t('Uninstall')}
  147. </StyledButton>
  148. </Confirm>
  149. </div>
  150. <Switch
  151. isActive={projectItem.enabled}
  152. toggle={() =>
  153. this.toggleEnablePlugin(projectItem.projectId, !projectItem.enabled)
  154. }
  155. isDisabled={!hasAccess}
  156. />
  157. </IntegrationFlex>
  158. )}
  159. </Access>
  160. </Container>
  161. );
  162. }
  163. }
  164. export default withApi(InstalledPlugin);
  165. const Container = styled('div')`
  166. padding: ${space(2)};
  167. border: 1px solid ${p => p.theme.border};
  168. border-bottom: none;
  169. background-color: ${p => p.theme.background};
  170. &:last-child {
  171. border-bottom: 1px solid ${p => p.theme.border};
  172. }
  173. `;
  174. const StyledButton = styled(Button)`
  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. `;