addIntegration.tsx 3.7 KB

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