index.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import type {RouteComponentProps} from 'react-router';
  2. import {addErrorMessage} from 'sentry/actionCreators/indicator';
  3. import SelectField from 'sentry/components/forms/fields/selectField';
  4. import Form from 'sentry/components/forms/form';
  5. import NarrowLayout from 'sentry/components/narrowLayout';
  6. import {t, tct} from 'sentry/locale';
  7. import ConfigStore from 'sentry/stores/configStore';
  8. import type {Organization, Project} from 'sentry/types';
  9. import DeprecatedAsyncView from 'sentry/views/deprecatedAsyncView';
  10. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  11. type Props = RouteComponentProps<{}, {}>;
  12. type TransferDetails = {
  13. organizations: Organization[];
  14. project: Project;
  15. };
  16. type State = {
  17. transferDetails: TransferDetails | null;
  18. } & DeprecatedAsyncView['state'];
  19. class AcceptProjectTransfer extends DeprecatedAsyncView<Props, State> {
  20. disableErrorReport = false;
  21. get regionHost(): string | undefined {
  22. // Because this route happens outside of OrganizationContext we
  23. // need to use initial data to decide which host to send the request to
  24. // as `/accept-transfer/` cannot be resolved to a region.
  25. const initialData = window.__initialData;
  26. let host: string | undefined = undefined;
  27. if (initialData && initialData.links?.regionUrl !== initialData.links?.sentryUrl) {
  28. host = initialData.links.regionUrl;
  29. }
  30. return host;
  31. }
  32. getEndpoints(): ReturnType<DeprecatedAsyncView['getEndpoints']> {
  33. const query = this.props.location.query;
  34. const host = this.regionHost;
  35. return [['transferDetails', '/accept-transfer/', {query, host}]];
  36. }
  37. getTitle() {
  38. return t('Accept Project Transfer');
  39. }
  40. handleSubmit = formData => {
  41. this.api.request('/accept-transfer/', {
  42. method: 'POST',
  43. host: this.regionHost,
  44. data: {
  45. data: this.props.location.query.data,
  46. organization: formData.organization,
  47. },
  48. success: () => {
  49. const orgSlug = formData.organization;
  50. const projectSlug = this.state?.transferDetails?.project.slug;
  51. const sentryUrl = ConfigStore.get('links').sentryUrl;
  52. if (!projectSlug) {
  53. window.location.href = `${sentryUrl}/organizations/${orgSlug}/projects/`;
  54. } else {
  55. window.location.href = `${sentryUrl}/organizations/${orgSlug}/settings/projects/${projectSlug}/teams/`;
  56. // done this way since we need to change subdomains
  57. }
  58. },
  59. error: error => {
  60. const errorMsg =
  61. error?.responseJSON && typeof error.responseJSON.detail === 'string'
  62. ? error.responseJSON.detail
  63. : '';
  64. addErrorMessage(
  65. t('Unable to transfer project') + errorMsg ? `: ${errorMsg}` : ''
  66. );
  67. },
  68. });
  69. };
  70. renderError(error) {
  71. let disableLog = false;
  72. // Check if there is an error message with `transferDetails` endpoint
  73. // If so, show as toast and ignore, otherwise log to sentry
  74. if (error?.responseJSON && typeof error.responseJSON.detail === 'string') {
  75. addErrorMessage(error.responseJSON.detail);
  76. disableLog = true;
  77. }
  78. return super.renderError(error, disableLog);
  79. }
  80. renderBody() {
  81. const {transferDetails} = this.state;
  82. const options = transferDetails?.organizations.map(org => ({
  83. label: org.slug,
  84. value: org.slug,
  85. }));
  86. const organization = options?.[0]?.value;
  87. return (
  88. <NarrowLayout>
  89. <SettingsPageHeader title={t('Approve Transfer Project Request')} />
  90. <p>
  91. {tct(
  92. 'Projects must be transferred to a specific [organization]. You can grant specific teams access to the project later under the [projectSettings]. (Note that granting access to at least one team is necessary for the project to appear in all parts of the UI.)',
  93. {
  94. organization: <strong>{t('Organization')}</strong>,
  95. projectSettings: <strong>{t('Project Settings')}</strong>,
  96. }
  97. )}
  98. </p>
  99. {transferDetails && (
  100. <p>
  101. {tct(
  102. 'Please select which [organization] you want for the project [project].',
  103. {
  104. organization: <strong>{t('Organization')}</strong>,
  105. project: transferDetails.project.slug,
  106. }
  107. )}
  108. </p>
  109. )}
  110. <Form
  111. onSubmit={this.handleSubmit}
  112. submitLabel={t('Transfer Project')}
  113. submitPriority="danger"
  114. initialData={organization ? {organization} : undefined}
  115. >
  116. <SelectField
  117. options={options}
  118. label={t('Organization')}
  119. name="organization"
  120. style={{borderBottom: 'none'}}
  121. />
  122. </Form>
  123. </NarrowLayout>
  124. );
  125. }
  126. }
  127. export default AcceptProjectTransfer;