integrationDetailedView.tsx 7.9 KB

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