pluginConfig.tsx 4.7 KB

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