details.tsx 8.0 KB

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