pluginConfig.tsx 5.0 KB

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