details.tsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 AsyncView from 'sentry/views/asyncView';
  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. } & AsyncView['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 AsyncView<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. this.recordDetailsViewed();
  48. }
  49. recordDetailsViewed() {
  50. const {pluginId} = this.props.params;
  51. trackIntegrationAnalytics('integrations.details_viewed', {
  52. integration: pluginId,
  53. integration_type: 'plugin',
  54. view: 'plugin_details',
  55. organization: this.props.organization,
  56. });
  57. }
  58. getTitle() {
  59. const {plugin} = this.state;
  60. if (plugin && plugin.name) {
  61. return plugin.name;
  62. }
  63. return 'Sentry';
  64. }
  65. getEndpoints(): ReturnType<AsyncView['getEndpoints']> {
  66. const {organization} = this.props;
  67. const {projectId, pluginId} = this.props.params;
  68. return [
  69. [
  70. 'pluginDetails',
  71. `/projects/${organization.slug}/${projectId}/plugins/${pluginId}/`,
  72. ],
  73. ];
  74. }
  75. trimSchema(value) {
  76. return value.split('//')[1];
  77. }
  78. handleReset = () => {
  79. const {organization} = this.props;
  80. const {projectId, pluginId} = this.props.params;
  81. addLoadingMessage(t('Saving changes\u2026'));
  82. trackIntegrationAnalytics('integrations.uninstall_clicked', {
  83. integration: pluginId,
  84. integration_type: 'plugin',
  85. view: 'plugin_details',
  86. organization: this.props.organization,
  87. });
  88. this.api.request(`/projects/${organization.slug}/${projectId}/plugins/${pluginId}/`, {
  89. method: 'POST',
  90. data: {reset: true},
  91. success: pluginDetails => {
  92. this.setState({pluginDetails});
  93. addSuccessMessage(t('Plugin was reset'));
  94. trackIntegrationAnalytics('integrations.uninstall_completed', {
  95. integration: pluginId,
  96. integration_type: 'plugin',
  97. view: 'plugin_details',
  98. organization: this.props.organization,
  99. });
  100. },
  101. error: () => {
  102. addErrorMessage(t('An error occurred'));
  103. },
  104. });
  105. };
  106. handleEnable = () => {
  107. const {organization, params} = this.props;
  108. enablePlugin({...params, orgId: organization.slug});
  109. this.analyticsChangeEnableStatus(true);
  110. };
  111. handleDisable = () => {
  112. const {organization, params} = this.props;
  113. disablePlugin({...params, orgId: organization.slug});
  114. this.analyticsChangeEnableStatus(false);
  115. };
  116. analyticsChangeEnableStatus = (enabled: boolean) => {
  117. const {pluginId} = this.props.params;
  118. const eventKey = enabled ? 'integrations.enabled' : 'integrations.disabled';
  119. trackIntegrationAnalytics(eventKey, {
  120. integration: pluginId,
  121. integration_type: 'plugin',
  122. view: 'plugin_details',
  123. organization: this.props.organization,
  124. });
  125. };
  126. // Enabled state is handled via PluginsStore and not via plugins detail
  127. getEnabled() {
  128. const {pluginDetails} = this.state;
  129. const {plugins} = this.props;
  130. const plugin =
  131. plugins &&
  132. plugins.plugins &&
  133. plugins.plugins.find(({slug}) => slug === this.props.params.pluginId);
  134. return plugin ? plugin.enabled : pluginDetails && pluginDetails.enabled;
  135. }
  136. renderActions() {
  137. const {pluginDetails} = this.state;
  138. if (!pluginDetails) {
  139. return null;
  140. }
  141. const enabled = this.getEnabled();
  142. const enable = (
  143. <StyledButton size="sm" onClick={this.handleEnable}>
  144. {t('Enable Plugin')}
  145. </StyledButton>
  146. );
  147. const disable = (
  148. <StyledButton size="sm" priority="danger" onClick={this.handleDisable}>
  149. {t('Disable Plugin')}
  150. </StyledButton>
  151. );
  152. const toggleEnable = enabled ? disable : enable;
  153. return (
  154. <div className="pull-right">
  155. {pluginDetails.canDisable && toggleEnable}
  156. <Button size="sm" onClick={this.handleReset}>
  157. {t('Reset Configuration')}
  158. </Button>
  159. </div>
  160. );
  161. }
  162. renderBody() {
  163. const {organization, project} = this.props;
  164. const {pluginDetails} = this.state;
  165. if (!pluginDetails) {
  166. return null;
  167. }
  168. return (
  169. <div>
  170. <SettingsPageHeader title={pluginDetails.name} action={this.renderActions()} />
  171. <div className="row">
  172. <div className="col-md-7">
  173. <PluginConfig
  174. organization={organization}
  175. project={project}
  176. data={pluginDetails}
  177. enabled={this.getEnabled()}
  178. onDisablePlugin={this.handleDisable}
  179. />
  180. </div>
  181. <div className="col-md-4 col-md-offset-1">
  182. <div className="pluginDetails-meta">
  183. <h4>{t('Plugin Information')}</h4>
  184. <dl className="flat">
  185. <dt>{t('Name')}</dt>
  186. <dd>{pluginDetails.name}</dd>
  187. <dt>{t('Author')}</dt>
  188. <dd>{pluginDetails.author?.name}</dd>
  189. {pluginDetails.author?.url && (
  190. <div>
  191. <dt>{t('URL')}</dt>
  192. <dd>
  193. <ExternalLink href={pluginDetails.author.url}>
  194. {this.trimSchema(pluginDetails.author.url)}
  195. </ExternalLink>
  196. </dd>
  197. </div>
  198. )}
  199. <dt>{t('Version')}</dt>
  200. <dd>
  201. {getDynamicText({
  202. value: pluginDetails.version,
  203. fixed: '1.0.0',
  204. })}
  205. </dd>
  206. </dl>
  207. {pluginDetails.description && (
  208. <div>
  209. <h4>{t('Description')}</h4>
  210. <p className="description">{pluginDetails.description}</p>
  211. </div>
  212. )}
  213. {pluginDetails.resourceLinks && (
  214. <div>
  215. <h4>{t('Resources')}</h4>
  216. <dl className="flat">
  217. {pluginDetails.resourceLinks.map(({title, url}) => (
  218. <dd key={url}>
  219. <ExternalLink href={url}>{title}</ExternalLink>
  220. </dd>
  221. ))}
  222. </dl>
  223. </div>
  224. )}
  225. </div>
  226. </div>
  227. </div>
  228. </div>
  229. );
  230. }
  231. }
  232. export {ProjectPluginDetails};
  233. export default withPlugins(ProjectPluginDetails);
  234. const StyledButton = styled(Button)`
  235. margin-right: ${space(0.75)};
  236. `;