details.tsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. import {RouteComponentProps} from 'react-router';
  2. import styled from '@emotion/styled';
  3. import {
  4. addErrorMessage,
  5. addLoadingMessage,
  6. addSuccessMessage,
  7. } from 'sentry/actionCreators/indicator';
  8. import {disablePlugin, enablePlugin} from 'sentry/actionCreators/plugins';
  9. import {Button} from 'sentry/components/button';
  10. import ExternalLink from 'sentry/components/links/externalLink';
  11. import PluginConfig from 'sentry/components/pluginConfig';
  12. import {t} from 'sentry/locale';
  13. import {space} from 'sentry/styles/space';
  14. import {Organization, Plugin, Project} from 'sentry/types';
  15. import getDynamicText from 'sentry/utils/getDynamicText';
  16. import {trackIntegrationAnalytics} from 'sentry/utils/integrationUtil';
  17. import withPlugins from 'sentry/utils/withPlugins';
  18. import DeprecatedAsyncView from 'sentry/views/deprecatedAsyncView';
  19. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  20. type Props = {
  21. organization: Organization;
  22. plugins: {
  23. plugins: Plugin[];
  24. };
  25. project: Project;
  26. } & RouteComponentProps<{pluginId: string; projectId: string}, {}>;
  27. type State = {
  28. pluginDetails?: Plugin;
  29. } & DeprecatedAsyncView['state'];
  30. /**
  31. * There are currently two sources of truths for plugin details:
  32. *
  33. * 1) PluginsStore has a list of plugins, and this is where ENABLED state lives
  34. * 2) We fetch "plugin details" via API and save it to local state as `pluginDetails`.
  35. * This is because "details" call contains form `config` and the "list" endpoint does not.
  36. * The more correct way would be to pass `config` to PluginConfig and use plugin from
  37. * PluginsStore
  38. */
  39. class ProjectPluginDetails extends DeprecatedAsyncView<Props, State> {
  40. componentDidUpdate(prevProps: Props, prevState: State) {
  41. super.componentDidUpdate(prevProps, prevState);
  42. if (prevProps.params.pluginId !== this.props.params.pluginId) {
  43. this.recordDetailsViewed();
  44. }
  45. }
  46. componentDidMount() {
  47. super.componentDidMount();
  48. this.recordDetailsViewed();
  49. }
  50. recordDetailsViewed() {
  51. const {pluginId} = this.props.params;
  52. trackIntegrationAnalytics('integrations.details_viewed', {
  53. integration: pluginId,
  54. integration_type: 'plugin',
  55. view: 'plugin_details',
  56. organization: this.props.organization,
  57. });
  58. }
  59. getTitle() {
  60. const {plugin} = this.state;
  61. if (plugin && plugin.name) {
  62. return plugin.name;
  63. }
  64. return 'Sentry';
  65. }
  66. getEndpoints(): ReturnType<DeprecatedAsyncView['getEndpoints']> {
  67. const {organization} = this.props;
  68. const {projectId, pluginId} = this.props.params;
  69. return [
  70. [
  71. 'pluginDetails',
  72. `/projects/${organization.slug}/${projectId}/plugins/${pluginId}/`,
  73. ],
  74. ];
  75. }
  76. trimSchema(value) {
  77. return value.split('//')[1];
  78. }
  79. handleReset = () => {
  80. const {organization} = this.props;
  81. const {projectId, pluginId} = this.props.params;
  82. addLoadingMessage(t('Saving changes\u2026'));
  83. trackIntegrationAnalytics('integrations.uninstall_clicked', {
  84. integration: pluginId,
  85. integration_type: 'plugin',
  86. view: 'plugin_details',
  87. organization: this.props.organization,
  88. });
  89. this.api.request(`/projects/${organization.slug}/${projectId}/plugins/${pluginId}/`, {
  90. method: 'POST',
  91. data: {reset: true},
  92. success: pluginDetails => {
  93. this.setState({pluginDetails});
  94. addSuccessMessage(t('Plugin was reset'));
  95. trackIntegrationAnalytics('integrations.uninstall_completed', {
  96. integration: pluginId,
  97. integration_type: 'plugin',
  98. view: 'plugin_details',
  99. organization: this.props.organization,
  100. });
  101. },
  102. error: () => {
  103. addErrorMessage(t('An error occurred'));
  104. },
  105. });
  106. };
  107. handleEnable = () => {
  108. const {organization, params} = this.props;
  109. enablePlugin({...params, orgId: organization.slug});
  110. this.analyticsChangeEnableStatus(true);
  111. };
  112. handleDisable = () => {
  113. const {organization, params} = this.props;
  114. disablePlugin({...params, orgId: organization.slug});
  115. this.analyticsChangeEnableStatus(false);
  116. };
  117. analyticsChangeEnableStatus = (enabled: boolean) => {
  118. const {pluginId} = this.props.params;
  119. const eventKey = enabled ? 'integrations.enabled' : 'integrations.disabled';
  120. trackIntegrationAnalytics(eventKey, {
  121. integration: pluginId,
  122. integration_type: 'plugin',
  123. view: 'plugin_details',
  124. organization: this.props.organization,
  125. });
  126. };
  127. // Enabled state is handled via PluginsStore and not via plugins detail
  128. getEnabled() {
  129. const {pluginDetails} = this.state;
  130. const {plugins} = this.props;
  131. const plugin =
  132. plugins &&
  133. plugins.plugins &&
  134. plugins.plugins.find(({slug}) => slug === this.props.params.pluginId);
  135. return plugin ? plugin.enabled : pluginDetails && pluginDetails.enabled;
  136. }
  137. renderActions() {
  138. const {pluginDetails} = this.state;
  139. if (!pluginDetails) {
  140. return null;
  141. }
  142. const enabled = this.getEnabled();
  143. const enable = (
  144. <StyledButton size="sm" onClick={this.handleEnable}>
  145. {t('Enable Plugin')}
  146. </StyledButton>
  147. );
  148. const disable = (
  149. <StyledButton size="sm" priority="danger" onClick={this.handleDisable}>
  150. {t('Disable Plugin')}
  151. </StyledButton>
  152. );
  153. const toggleEnable = enabled ? disable : enable;
  154. return (
  155. <div className="pull-right">
  156. {pluginDetails.canDisable && toggleEnable}
  157. <Button size="sm" onClick={this.handleReset}>
  158. {t('Reset Configuration')}
  159. </Button>
  160. </div>
  161. );
  162. }
  163. renderBody() {
  164. const {organization, project} = this.props;
  165. const {pluginDetails} = this.state;
  166. if (!pluginDetails) {
  167. return null;
  168. }
  169. return (
  170. <div>
  171. <SettingsPageHeader title={pluginDetails.name} action={this.renderActions()} />
  172. <div className="row">
  173. <div className="col-md-7">
  174. <PluginConfig
  175. organization={organization}
  176. project={project}
  177. data={pluginDetails}
  178. enabled={this.getEnabled()}
  179. onDisablePlugin={this.handleDisable}
  180. />
  181. </div>
  182. <div className="col-md-4 col-md-offset-1">
  183. <div className="pluginDetails-meta">
  184. <h4>{t('Plugin Information')}</h4>
  185. <dl className="flat">
  186. <dt>{t('Name')}</dt>
  187. <dd>{pluginDetails.name}</dd>
  188. <dt>{t('Author')}</dt>
  189. <dd>{pluginDetails.author?.name}</dd>
  190. {pluginDetails.author?.url && (
  191. <div>
  192. <dt>{t('URL')}</dt>
  193. <dd>
  194. <ExternalLink href={pluginDetails.author.url}>
  195. {this.trimSchema(pluginDetails.author.url)}
  196. </ExternalLink>
  197. </dd>
  198. </div>
  199. )}
  200. <dt>{t('Version')}</dt>
  201. <dd>
  202. {getDynamicText({
  203. value: pluginDetails.version,
  204. fixed: '1.0.0',
  205. })}
  206. </dd>
  207. </dl>
  208. {pluginDetails.description && (
  209. <div>
  210. <h4>{t('Description')}</h4>
  211. <p className="description">{pluginDetails.description}</p>
  212. </div>
  213. )}
  214. {pluginDetails.resourceLinks && (
  215. <div>
  216. <h4>{t('Resources')}</h4>
  217. <dl className="flat">
  218. {pluginDetails.resourceLinks.map(({title, url}) => (
  219. <dd key={url}>
  220. <ExternalLink href={url}>{title}</ExternalLink>
  221. </dd>
  222. ))}
  223. </dl>
  224. </div>
  225. )}
  226. </div>
  227. </div>
  228. </div>
  229. </div>
  230. );
  231. }
  232. }
  233. export {ProjectPluginDetails};
  234. export default withPlugins(ProjectPluginDetails);
  235. const StyledButton = styled(Button)`
  236. margin-right: ${space(0.75)};
  237. `;