addIntegration.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import {Component} from 'react';
  2. import * as qs from 'query-string';
  3. import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
  4. import {t} from 'sentry/locale';
  5. import ConfigStore from 'sentry/stores/configStore';
  6. import type {
  7. IntegrationProvider,
  8. IntegrationWithConfig,
  9. Organization,
  10. } from 'sentry/types';
  11. import {trackIntegrationAnalytics} from 'sentry/utils/integrationUtil';
  12. type Props = {
  13. children: (
  14. openDialog: (urlParams?: {[key: string]: string}) => void
  15. ) => React.ReactNode;
  16. onInstall: (data: IntegrationWithConfig) => void;
  17. organization: Organization;
  18. provider: IntegrationProvider;
  19. account?: string | null; // for analytics
  20. analyticsParams?: {
  21. already_installed: boolean;
  22. view:
  23. | 'integrations_directory_integration_detail'
  24. | 'integrations_directory'
  25. | 'messaging_integration_onboarding'
  26. | 'onboarding'
  27. | 'project_creation';
  28. };
  29. modalParams?: {[key: string]: string};
  30. };
  31. export default class AddIntegration extends Component<Props> {
  32. componentDidMount() {
  33. window.addEventListener('message', this.didReceiveMessage);
  34. }
  35. componentWillUnmount() {
  36. window.removeEventListener('message', this.didReceiveMessage);
  37. this.dialog?.close();
  38. }
  39. dialog: Window | null = null;
  40. computeCenteredWindow(width: number, height: number) {
  41. // Taken from: https://stackoverflow.com/questions/4068373/center-a-popup-window-on-screen
  42. const screenLeft =
  43. window.screenLeft !== undefined ? window.screenLeft : window.screenX;
  44. const screenTop = window.screenTop !== undefined ? window.screenTop : window.screenY;
  45. const innerWidth = window.innerWidth
  46. ? window.innerWidth
  47. : document.documentElement.clientWidth
  48. ? document.documentElement.clientWidth
  49. : screen.width;
  50. const innerHeight = window.innerHeight
  51. ? window.innerHeight
  52. : document.documentElement.clientHeight
  53. ? document.documentElement.clientHeight
  54. : screen.height;
  55. const left = innerWidth / 2 - width / 2 + screenLeft;
  56. const top = innerHeight / 2 - height / 2 + screenTop;
  57. return {left, top};
  58. }
  59. openDialog = (urlParams?: {[key: string]: string}) => {
  60. const {account, analyticsParams, modalParams, organization, provider} = this.props;
  61. trackIntegrationAnalytics('integrations.installation_start', {
  62. integration: provider.key,
  63. integration_type: 'first_party',
  64. organization,
  65. ...analyticsParams,
  66. });
  67. const name = 'sentryAddIntegration';
  68. const {url, width, height} = provider.setupDialog;
  69. const {left, top} = this.computeCenteredWindow(width, height);
  70. let query: {[key: string]: string} = {...urlParams};
  71. if (account) {
  72. query.account = account;
  73. }
  74. if (modalParams) {
  75. query = {...query, ...modalParams};
  76. }
  77. const installUrl = `${url}?${qs.stringify(query)}`;
  78. const opts = `scrollbars=yes,width=${width},height=${height},top=${top},left=${left}`;
  79. this.dialog = window.open(installUrl, name, opts);
  80. this.dialog?.focus();
  81. };
  82. didReceiveMessage = (message: MessageEvent) => {
  83. const {analyticsParams, onInstall, organization, provider} = this.props;
  84. const validOrigins = [
  85. ConfigStore.get('links').sentryUrl,
  86. ConfigStore.get('links').organizationUrl,
  87. document.location.origin,
  88. ];
  89. if (!validOrigins.includes(message.origin)) {
  90. return;
  91. }
  92. if (message.source !== this.dialog) {
  93. return;
  94. }
  95. const {success, data} = message.data;
  96. this.dialog = null;
  97. if (!success) {
  98. addErrorMessage(data?.error ?? t('An unknown error occurred'));
  99. return;
  100. }
  101. if (!data) {
  102. return;
  103. }
  104. trackIntegrationAnalytics('integrations.installation_complete', {
  105. integration: provider.key,
  106. integration_type: 'first_party',
  107. organization,
  108. ...analyticsParams,
  109. });
  110. addSuccessMessage(t('%s added', provider.name));
  111. onInstall(data);
  112. };
  113. render() {
  114. const {children} = this.props;
  115. return children(this.openDialog);
  116. }
  117. }