addIntegration.tsx 4.0 KB

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