integrationDetailedView.tsx 8.2 KB

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