integrationDetailedView.tsx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. import {Fragment} from 'react';
  2. import {addErrorMessage} from 'sentry/actionCreators/indicator';
  3. import {RequestOptions} from 'sentry/api';
  4. import Alert from 'sentry/components/alert';
  5. import AsyncComponent from 'sentry/components/asyncComponent';
  6. import Button from 'sentry/components/button';
  7. import HookOrDefault from 'sentry/components/hookOrDefault';
  8. import {Panel, PanelItem} from 'sentry/components/panels';
  9. import {IconOpen} from 'sentry/icons';
  10. import {t} from 'sentry/locale';
  11. import space from 'sentry/styles/space';
  12. import {Integration, IntegrationProvider, ObjectStatus} from 'sentry/types';
  13. import {getAlertText} from 'sentry/utils/integrationUtil';
  14. import withOrganization from 'sentry/utils/withOrganization';
  15. import AbstractIntegrationDetailedView from './abstractIntegrationDetailedView';
  16. import {AddIntegrationButton} from './addIntegrationButton';
  17. import InstalledIntegration from './installedIntegration';
  18. const FirstPartyIntegrationAlert = HookOrDefault({
  19. hookName: 'component:first-party-integration-alert',
  20. defaultComponent: () => null,
  21. });
  22. const FirstPartyIntegrationAdditionalCTA = HookOrDefault({
  23. hookName: 'component:first-party-integration-additional-cta',
  24. defaultComponent: () => null,
  25. });
  26. type State = {
  27. configurations: Integration[];
  28. information: {providers: IntegrationProvider[]};
  29. };
  30. class IntegrationDetailedView extends AbstractIntegrationDetailedView<
  31. AbstractIntegrationDetailedView['props'],
  32. State & AbstractIntegrationDetailedView['state']
  33. > {
  34. getEndpoints(): ReturnType<AsyncComponent['getEndpoints']> {
  35. const {orgId, integrationSlug} = this.props.params;
  36. return [
  37. [
  38. 'information',
  39. `/organizations/${orgId}/config/integrations/?provider_key=${integrationSlug}`,
  40. ],
  41. [
  42. 'configurations',
  43. `/organizations/${orgId}/integrations/?provider_key=${integrationSlug}&includeConfig=0`,
  44. ],
  45. ];
  46. }
  47. get integrationType() {
  48. return 'first_party' as const;
  49. }
  50. get provider() {
  51. return this.state.information.providers[0];
  52. }
  53. get description() {
  54. return this.metadata.description;
  55. }
  56. get author() {
  57. return this.metadata.author;
  58. }
  59. get alerts() {
  60. const provider = this.provider;
  61. const metadata = this.metadata;
  62. // The server response for integration installations includes old icon CSS classes
  63. // We map those to the currently in use values to their react equivalents
  64. // and fallback to IconFlag just in case.
  65. const alerts = (metadata.aspects.alerts || []).map(item => ({
  66. ...item,
  67. showIcon: true,
  68. }));
  69. if (!provider.canAdd && metadata.aspects.externalInstall) {
  70. alerts.push({
  71. type: 'warning',
  72. showIcon: true,
  73. text: metadata.aspects.externalInstall.noticeText,
  74. });
  75. }
  76. return alerts;
  77. }
  78. get resourceLinks() {
  79. const metadata = this.metadata;
  80. return [
  81. {url: metadata.source_url, title: 'View Source'},
  82. {url: metadata.issue_url, title: 'Report Issue'},
  83. ];
  84. }
  85. get metadata() {
  86. return this.provider.metadata;
  87. }
  88. get isEnabled() {
  89. return this.state.configurations.length > 0;
  90. }
  91. get installationStatus() {
  92. const {configurations} = this.state;
  93. if (
  94. configurations.filter(i => i.organizationIntegrationStatus === 'disabled').length
  95. ) {
  96. return 'Disabled';
  97. }
  98. return configurations.length ? 'Installed' : 'Not Installed';
  99. }
  100. get integrationName() {
  101. return this.provider.name;
  102. }
  103. get featureData() {
  104. return this.metadata.features;
  105. }
  106. onInstall = (integration: Integration) => {
  107. // send the user to the configure integration view for that integration
  108. const {orgId} = this.props.params;
  109. this.props.router.push(
  110. `/settings/${orgId}/integrations/${integration.provider.key}/${integration.id}/`
  111. );
  112. };
  113. onRemove = (integration: Integration) => {
  114. const {orgId} = this.props.params;
  115. const origIntegrations = [...this.state.configurations];
  116. const integrations = this.state.configurations.map(i =>
  117. i.id === integration.id
  118. ? {...i, organizationIntegrationStatus: 'pending_deletion' as ObjectStatus}
  119. : i
  120. );
  121. this.setState({configurations: integrations});
  122. const options: RequestOptions = {
  123. method: 'DELETE',
  124. error: () => {
  125. this.setState({configurations: origIntegrations});
  126. addErrorMessage(t('Failed to remove Integration'));
  127. },
  128. };
  129. this.api.request(`/organizations/${orgId}/integrations/${integration.id}/`, options);
  130. };
  131. onDisable = (integration: Integration) => {
  132. let url: string;
  133. const [domainName, orgName] = integration.domainName.split('/');
  134. if (integration.accountType === 'User') {
  135. url = `https://${domainName}/settings/installations/`;
  136. } else {
  137. url = `https://${domainName}/organizations/${orgName}/settings/installations/`;
  138. }
  139. window.open(url, '_blank');
  140. };
  141. handleExternalInstall = () => {
  142. this.trackIntegrationAnalytics('integrations.installation_start');
  143. };
  144. renderAlert() {
  145. return (
  146. <FirstPartyIntegrationAlert
  147. integrations={this.state.configurations ?? []}
  148. hideCTA
  149. />
  150. );
  151. }
  152. renderAdditionalCTA() {
  153. return (
  154. <FirstPartyIntegrationAdditionalCTA
  155. integrations={this.state.configurations ?? []}
  156. />
  157. );
  158. }
  159. renderTopButton(disabledFromFeatures: boolean, userHasAccess: boolean) {
  160. const {organization} = this.props;
  161. const provider = this.provider;
  162. const {metadata} = provider;
  163. const size = 'sm' as const;
  164. const priority = 'primary' as const;
  165. const buttonProps = {
  166. style: {marginBottom: space(1)},
  167. size,
  168. priority,
  169. 'data-test-id': 'install-button',
  170. disabled: disabledFromFeatures,
  171. organization,
  172. };
  173. if (!userHasAccess) {
  174. return this.renderRequestIntegrationButton();
  175. }
  176. if (provider.canAdd) {
  177. return (
  178. <AddIntegrationButton
  179. provider={provider}
  180. onAddIntegration={this.onInstall}
  181. analyticsParams={{
  182. view: 'integrations_directory_integration_detail',
  183. already_installed: this.installationStatus !== 'Not Installed',
  184. }}
  185. {...buttonProps}
  186. />
  187. );
  188. }
  189. if (metadata.aspects.externalInstall) {
  190. return (
  191. <Button
  192. icon={<IconOpen />}
  193. href={metadata.aspects.externalInstall.url}
  194. onClick={this.handleExternalInstall}
  195. external
  196. {...buttonProps}
  197. >
  198. {metadata.aspects.externalInstall.buttonText}
  199. </Button>
  200. );
  201. }
  202. // This should never happen but we can't return undefined without some refactoring.
  203. return <Fragment />;
  204. }
  205. renderConfigurations() {
  206. const {configurations} = this.state;
  207. const {organization} = this.props;
  208. const provider = this.provider;
  209. if (!configurations.length) {
  210. return this.renderEmptyConfigurations();
  211. }
  212. const alertText = getAlertText(configurations);
  213. return (
  214. <Fragment>
  215. {alertText && (
  216. <Alert type="warning" showIcon>
  217. {alertText}
  218. </Alert>
  219. )}
  220. <Panel>
  221. {configurations.map(integration => (
  222. <PanelItem key={integration.id}>
  223. <InstalledIntegration
  224. organization={organization}
  225. provider={provider}
  226. integration={integration}
  227. onRemove={this.onRemove}
  228. onDisable={this.onDisable}
  229. data-test-id={integration.id}
  230. trackIntegrationAnalytics={this.trackIntegrationAnalytics}
  231. requiresUpgrade={!!alertText}
  232. />
  233. </PanelItem>
  234. ))}
  235. </Panel>
  236. </Fragment>
  237. );
  238. }
  239. }
  240. export default withOrganization(IntegrationDetailedView);