details.tsx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. import type {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 type {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?.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 = plugins?.plugins?.find(
  132. ({slug}) => slug === this.props.params.pluginId
  133. );
  134. return plugin ? plugin.enabled : 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 {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. project={project}
  175. plugin={pluginDetails}
  176. enabled={this.getEnabled()}
  177. onDisablePlugin={this.handleDisable}
  178. />
  179. </div>
  180. <div className="col-md-4 col-md-offset-1">
  181. <div className="pluginDetails-meta">
  182. <h4>{t('Plugin Information')}</h4>
  183. <dl className="flat">
  184. <dt>{t('Name')}</dt>
  185. <dd>{pluginDetails.name}</dd>
  186. <dt>{t('Author')}</dt>
  187. <dd>{pluginDetails.author?.name}</dd>
  188. {pluginDetails.author?.url && (
  189. <div>
  190. <dt>{t('URL')}</dt>
  191. <dd>
  192. <ExternalLink href={pluginDetails.author.url}>
  193. {this.trimSchema(pluginDetails.author.url)}
  194. </ExternalLink>
  195. </dd>
  196. </div>
  197. )}
  198. <dt>{t('Version')}</dt>
  199. <dd>
  200. {getDynamicText({
  201. value: pluginDetails.version,
  202. fixed: '1.0.0',
  203. })}
  204. </dd>
  205. </dl>
  206. {pluginDetails.description && (
  207. <div>
  208. <h4>{t('Description')}</h4>
  209. <p className="description">{pluginDetails.description}</p>
  210. </div>
  211. )}
  212. {pluginDetails.resourceLinks && (
  213. <div>
  214. <h4>{t('Resources')}</h4>
  215. <dl className="flat">
  216. {pluginDetails.resourceLinks.map(({title, url}) => (
  217. <dd key={url}>
  218. <ExternalLink href={url}>{title}</ExternalLink>
  219. </dd>
  220. ))}
  221. </dl>
  222. </div>
  223. )}
  224. </div>
  225. </div>
  226. </div>
  227. </div>
  228. );
  229. }
  230. }
  231. export {ProjectPluginDetails};
  232. export default withPlugins(ProjectPluginDetails);
  233. const StyledButton = styled(Button)`
  234. margin-right: ${space(0.75)};
  235. `;