integrationDetailedView.tsx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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, getIntegrationStatus} 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. // TODO: add transations
  95. const {configurations} = this.state;
  96. const statusList = configurations.map(getIntegrationStatus);
  97. // if we have conflicting statuses, we have a priority order
  98. if (statusList.includes('active')) {
  99. return 'Installed';
  100. }
  101. if (statusList.includes('disabled')) {
  102. return 'Disabled';
  103. }
  104. if (statusList.includes('pending_deletion')) {
  105. return 'Pending Deletion';
  106. }
  107. return 'Not Installed';
  108. }
  109. get integrationName() {
  110. return this.provider.name;
  111. }
  112. get featureData() {
  113. return this.metadata.features;
  114. }
  115. onInstall = (integration: Integration) => {
  116. // send the user to the configure integration view for that integration
  117. const {organization} = this.props;
  118. this.props.router.push(
  119. normalizeUrl(
  120. `/settings/${organization.slug}/integrations/${integration.provider.key}/${integration.id}/`
  121. )
  122. );
  123. };
  124. onRemove = (integration: Integration) => {
  125. const {organization} = this.props;
  126. const origIntegrations = [...this.state.configurations];
  127. const integrations = this.state.configurations.map(i =>
  128. i.id === integration.id
  129. ? {...i, organizationIntegrationStatus: 'pending_deletion' as ObjectStatus}
  130. : i
  131. );
  132. this.setState({configurations: integrations});
  133. const options: RequestOptions = {
  134. method: 'DELETE',
  135. error: () => {
  136. this.setState({configurations: origIntegrations});
  137. addErrorMessage(t('Failed to remove Integration'));
  138. },
  139. };
  140. this.api.request(
  141. `/organizations/${organization.slug}/integrations/${integration.id}/`,
  142. options
  143. );
  144. };
  145. onDisable = (integration: Integration) => {
  146. let url: string;
  147. const [domainName, orgName] = integration.domainName.split('/');
  148. if (integration.accountType === 'User') {
  149. url = `https://${domainName}/settings/installations/`;
  150. } else {
  151. url = `https://${domainName}/organizations/${orgName}/settings/installations/`;
  152. }
  153. window.open(url, '_blank');
  154. };
  155. handleExternalInstall = () => {
  156. this.trackIntegrationAnalytics('integrations.installation_start');
  157. };
  158. renderAlert() {
  159. return (
  160. <FirstPartyIntegrationAlert
  161. integrations={this.state.configurations ?? []}
  162. hideCTA
  163. />
  164. );
  165. }
  166. renderAdditionalCTA() {
  167. return (
  168. <FirstPartyIntegrationAdditionalCTA
  169. integrations={this.state.configurations ?? []}
  170. />
  171. );
  172. }
  173. renderTopButton(disabledFromFeatures: boolean, userHasAccess: boolean) {
  174. const {organization} = this.props;
  175. const provider = this.provider;
  176. const {metadata} = provider;
  177. const size = 'sm' as const;
  178. const priority = 'primary' as const;
  179. const buttonProps = {
  180. style: {marginBottom: space(1)},
  181. size,
  182. priority,
  183. 'data-test-id': 'install-button',
  184. disabled: disabledFromFeatures,
  185. organization,
  186. };
  187. if (!userHasAccess) {
  188. return this.renderRequestIntegrationButton();
  189. }
  190. if (provider.canAdd) {
  191. return (
  192. <AddIntegrationButton
  193. provider={provider}
  194. onAddIntegration={this.onInstall}
  195. analyticsParams={{
  196. view: 'integrations_directory_integration_detail',
  197. already_installed: this.installationStatus !== 'Not Installed',
  198. }}
  199. {...buttonProps}
  200. />
  201. );
  202. }
  203. if (metadata.aspects.externalInstall) {
  204. return (
  205. <Button
  206. icon={<IconOpen />}
  207. href={metadata.aspects.externalInstall.url}
  208. onClick={this.handleExternalInstall}
  209. external
  210. {...buttonProps}
  211. >
  212. {metadata.aspects.externalInstall.buttonText}
  213. </Button>
  214. );
  215. }
  216. // This should never happen but we can't return undefined without some refactoring.
  217. return <Fragment />;
  218. }
  219. renderConfigurations() {
  220. const {configurations} = this.state;
  221. const {organization} = this.props;
  222. const provider = this.provider;
  223. if (!configurations.length) {
  224. return this.renderEmptyConfigurations();
  225. }
  226. const alertText = getAlertText(configurations);
  227. return (
  228. <Fragment>
  229. {alertText && (
  230. <Alert type="warning" showIcon>
  231. {alertText}
  232. </Alert>
  233. )}
  234. <Panel>
  235. {configurations.map(integration => (
  236. <PanelItem key={integration.id}>
  237. <InstalledIntegration
  238. organization={organization}
  239. provider={provider}
  240. integration={integration}
  241. onRemove={this.onRemove}
  242. onDisable={this.onDisable}
  243. data-test-id={integration.id}
  244. trackIntegrationAnalytics={this.trackIntegrationAnalytics}
  245. requiresUpgrade={!!alertText}
  246. />
  247. </PanelItem>
  248. ))}
  249. </Panel>
  250. </Fragment>
  251. );
  252. }
  253. }
  254. export default withOrganization(IntegrationDetailedView);