pluginConfig.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import {Component} from 'react';
  2. import styled from '@emotion/styled';
  3. import isEqual from 'lodash/isEqual';
  4. import {
  5. addErrorMessage,
  6. addLoadingMessage,
  7. addSuccessMessage,
  8. } from 'sentry/actionCreators/indicator';
  9. import {Client} from 'sentry/api';
  10. import {hasEveryAccess} from 'sentry/components/acl/access';
  11. import {Button} from 'sentry/components/button';
  12. import LoadingIndicator from 'sentry/components/loadingIndicator';
  13. import {Panel, PanelAlert, PanelBody, PanelHeader} from 'sentry/components/panels';
  14. import {t} from 'sentry/locale';
  15. import plugins from 'sentry/plugins';
  16. import PluginIcon from 'sentry/plugins/components/pluginIcon';
  17. import {space} from 'sentry/styles/space';
  18. import {Organization, Plugin, Project} from 'sentry/types';
  19. import withApi from 'sentry/utils/withApi';
  20. type Props = {
  21. api: Client;
  22. data: Plugin;
  23. onDisablePlugin: (data: Plugin) => void;
  24. organization: Organization;
  25. project: Project;
  26. enabled?: boolean;
  27. };
  28. type State = {
  29. testResults: string;
  30. loading?: boolean;
  31. };
  32. class PluginConfig extends Component<Props, State> {
  33. static defaultProps = {
  34. onDisablePlugin: () => {},
  35. };
  36. state: State = {
  37. loading: !plugins.isLoaded(this.props.data),
  38. testResults: '',
  39. };
  40. componentDidMount() {
  41. this.loadPlugin(this.props.data);
  42. }
  43. UNSAFE_componentWillReceiveProps(nextProps: Props) {
  44. this.loadPlugin(nextProps.data);
  45. }
  46. shouldComponentUpdate(nextProps: Props, nextState: State) {
  47. return !isEqual(nextState, this.state) || !isEqual(nextProps.data, this.props.data);
  48. }
  49. loadPlugin(data: Plugin) {
  50. this.setState(
  51. {
  52. loading: true,
  53. },
  54. () => {
  55. plugins.load(data, () => {
  56. this.setState({loading: false});
  57. });
  58. }
  59. );
  60. }
  61. getPluginEndpoint() {
  62. const {organization, project, data} = this.props;
  63. return `/projects/${organization.slug}/${project.slug}/plugins/${data.id}/`;
  64. }
  65. handleDisablePlugin = () => {
  66. this.props.onDisablePlugin(this.props.data);
  67. };
  68. handleTestPlugin = async () => {
  69. this.setState({testResults: ''});
  70. addLoadingMessage(t('Sending test...'));
  71. try {
  72. const data = await this.props.api.requestPromise(this.getPluginEndpoint(), {
  73. method: 'POST',
  74. data: {
  75. test: true,
  76. },
  77. });
  78. this.setState({testResults: JSON.stringify(data.detail)});
  79. addSuccessMessage(t('Test Complete!'));
  80. } catch (_err) {
  81. addErrorMessage(
  82. t('An unexpected error occurred while testing your plugin. Please try again.')
  83. );
  84. }
  85. };
  86. createMarkup() {
  87. return {__html: this.props.data.doc};
  88. }
  89. render() {
  90. const {data, organization, project} = this.props;
  91. // If passed via props, use that value instead of from `data`
  92. const enabled =
  93. typeof this.props.enabled !== 'undefined' ? this.props.enabled : data.enabled;
  94. const hasWriteAccess = hasEveryAccess(['project:write'], {organization, project});
  95. return (
  96. <Panel
  97. className={`plugin-config ref-plugin-config-${data.id}`}
  98. data-test-id="plugin-config"
  99. >
  100. <PanelHeader hasButtons>
  101. <PluginName>
  102. <StyledPluginIcon pluginId={data.id} />
  103. <span>{data.name}</span>
  104. </PluginName>
  105. {data.canDisable && enabled && (
  106. <Actions>
  107. {data.isTestable && (
  108. <TestPluginButton onClick={this.handleTestPlugin} size="sm">
  109. {t('Test Plugin')}
  110. </TestPluginButton>
  111. )}
  112. <Button
  113. size="sm"
  114. onClick={this.handleDisablePlugin}
  115. disabled={!hasWriteAccess}
  116. >
  117. {t('Disable')}
  118. </Button>
  119. </Actions>
  120. )}
  121. </PanelHeader>
  122. {data.status === 'beta' && (
  123. <PanelAlert type="warning">
  124. {t('This plugin is considered beta and may change in the future.')}
  125. </PanelAlert>
  126. )}
  127. {this.state.testResults !== '' && (
  128. <PanelAlert type="info">
  129. <strong>Test Results</strong>
  130. <div>{this.state.testResults}</div>
  131. </PanelAlert>
  132. )}
  133. <StyledPanelBody>
  134. <div dangerouslySetInnerHTML={this.createMarkup()} />
  135. {this.state.loading ? (
  136. <LoadingIndicator />
  137. ) : (
  138. plugins.get(data).renderSettings({
  139. organization: this.props.organization,
  140. project: this.props.project,
  141. })
  142. )}
  143. </StyledPanelBody>
  144. </Panel>
  145. );
  146. }
  147. }
  148. export {PluginConfig};
  149. export default withApi(PluginConfig);
  150. const PluginName = styled('div')`
  151. display: flex;
  152. align-items: center;
  153. flex: 1;
  154. `;
  155. const StyledPluginIcon = styled(PluginIcon)`
  156. margin-right: ${space(1)};
  157. `;
  158. const Actions = styled('div')`
  159. display: flex;
  160. `;
  161. const TestPluginButton = styled(Button)`
  162. margin-right: ${space(1)};
  163. `;
  164. const StyledPanelBody = styled(PanelBody)`
  165. padding: ${space(2)};
  166. padding-bottom: 0;
  167. `;