details.tsx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 ExternalLink from 'sentry/components/links/externalLink';
  10. import PluginConfig from 'sentry/components/pluginConfig';
  11. import {t} from 'sentry/locale';
  12. import {space} from 'sentry/styles/space';
  13. import type {Plugin} from 'sentry/types/integrations';
  14. import type {RouteComponentProps} from 'sentry/types/legacyReactRouter';
  15. import type {Organization} from 'sentry/types/organization';
  16. import type {Project} from 'sentry/types/project';
  17. import getDynamicText from 'sentry/utils/getDynamicText';
  18. import {trackIntegrationAnalytics} from 'sentry/utils/integrationUtil';
  19. import withPlugins from 'sentry/utils/withPlugins';
  20. import DeprecatedAsyncView from 'sentry/views/deprecatedAsyncView';
  21. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  22. type Props = {
  23. organization: Organization;
  24. plugins: {
  25. plugins: Plugin[];
  26. };
  27. project: Project;
  28. } & RouteComponentProps<{pluginId: string; projectId: string}, {}>;
  29. type State = {
  30. pluginDetails?: Plugin;
  31. } & DeprecatedAsyncView['state'];
  32. /**
  33. * There are currently two sources of truths for plugin details:
  34. *
  35. * 1) PluginsStore has a list of plugins, and this is where ENABLED state lives
  36. * 2) We fetch "plugin details" via API and save it to local state as `pluginDetails`.
  37. * This is because "details" call contains form `config` and the "list" endpoint does not.
  38. * The more correct way would be to pass `config` to PluginConfig and use plugin from
  39. * PluginsStore
  40. */
  41. class ProjectPluginDetails extends DeprecatedAsyncView<Props, State> {
  42. componentDidUpdate(prevProps: Props, prevState: State) {
  43. super.componentDidUpdate(prevProps, prevState);
  44. if (prevProps.params.pluginId !== this.props.params.pluginId) {
  45. this.recordDetailsViewed();
  46. }
  47. }
  48. componentDidMount() {
  49. super.componentDidMount();
  50. this.recordDetailsViewed();
  51. }
  52. recordDetailsViewed() {
  53. const {pluginId} = this.props.params;
  54. trackIntegrationAnalytics('integrations.details_viewed', {
  55. integration: pluginId,
  56. integration_type: 'plugin',
  57. view: 'plugin_details',
  58. organization: this.props.organization,
  59. });
  60. }
  61. getTitle() {
  62. const {plugin} = this.state;
  63. if (plugin?.name) {
  64. return plugin.name;
  65. }
  66. return 'Sentry';
  67. }
  68. getEndpoints(): ReturnType<DeprecatedAsyncView['getEndpoints']> {
  69. const {organization} = this.props;
  70. const {projectId, pluginId} = this.props.params;
  71. return [
  72. [
  73. 'pluginDetails',
  74. `/projects/${organization.slug}/${projectId}/plugins/${pluginId}/`,
  75. ],
  76. ];
  77. }
  78. trimSchema(value) {
  79. return value.split('//')[1];
  80. }
  81. handleReset = () => {
  82. const {organization} = this.props;
  83. const {projectId, pluginId} = this.props.params;
  84. addLoadingMessage(t('Saving changes\u2026'));
  85. trackIntegrationAnalytics('integrations.uninstall_clicked', {
  86. integration: pluginId,
  87. integration_type: 'plugin',
  88. view: 'plugin_details',
  89. organization: this.props.organization,
  90. });
  91. this.api.request(`/projects/${organization.slug}/${projectId}/plugins/${pluginId}/`, {
  92. method: 'POST',
  93. data: {reset: true},
  94. success: pluginDetails => {
  95. this.setState({pluginDetails});
  96. addSuccessMessage(t('Plugin was reset'));
  97. trackIntegrationAnalytics('integrations.uninstall_completed', {
  98. integration: pluginId,
  99. integration_type: 'plugin',
  100. view: 'plugin_details',
  101. organization: this.props.organization,
  102. });
  103. },
  104. error: () => {
  105. addErrorMessage(t('An error occurred'));
  106. },
  107. });
  108. };
  109. handleEnable = () => {
  110. const {organization, params} = this.props;
  111. enablePlugin({...params, orgId: organization.slug});
  112. this.analyticsChangeEnableStatus(true);
  113. };
  114. handleDisable = () => {
  115. const {organization, params} = this.props;
  116. disablePlugin({...params, orgId: organization.slug});
  117. this.analyticsChangeEnableStatus(false);
  118. };
  119. analyticsChangeEnableStatus = (enabled: boolean) => {
  120. const {pluginId} = this.props.params;
  121. const eventKey = enabled ? 'integrations.enabled' : 'integrations.disabled';
  122. trackIntegrationAnalytics(eventKey, {
  123. integration: pluginId,
  124. integration_type: 'plugin',
  125. view: 'plugin_details',
  126. organization: this.props.organization,
  127. });
  128. };
  129. // Enabled state is handled via PluginsStore and not via plugins detail
  130. getEnabled() {
  131. const {pluginDetails} = this.state;
  132. const {plugins} = this.props;
  133. const plugin = plugins?.plugins?.find(
  134. ({slug}) => slug === this.props.params.pluginId
  135. );
  136. return plugin ? plugin.enabled : pluginDetails?.enabled;
  137. }
  138. renderActions() {
  139. const {pluginDetails} = this.state;
  140. if (!pluginDetails) {
  141. return null;
  142. }
  143. const enabled = this.getEnabled();
  144. const enable = (
  145. <StyledButton size="sm" onClick={this.handleEnable}>
  146. {t('Enable Plugin')}
  147. </StyledButton>
  148. );
  149. const disable = (
  150. <StyledButton size="sm" priority="danger" onClick={this.handleDisable}>
  151. {t('Disable Plugin')}
  152. </StyledButton>
  153. );
  154. const toggleEnable = enabled ? disable : enable;
  155. return (
  156. <div className="pull-right">
  157. {pluginDetails.canDisable && toggleEnable}
  158. <Button size="sm" onClick={this.handleReset}>
  159. {t('Reset Configuration')}
  160. </Button>
  161. </div>
  162. );
  163. }
  164. renderBody() {
  165. const {project} = this.props;
  166. const {pluginDetails} = this.state;
  167. if (!pluginDetails) {
  168. return null;
  169. }
  170. return (
  171. <div>
  172. <SettingsPageHeader title={pluginDetails.name} action={this.renderActions()} />
  173. <div className="row">
  174. <div className="col-md-7">
  175. <PluginConfig
  176. project={project}
  177. plugin={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. `;