index.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import {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 {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. getEndpoints(): ReturnType<DeprecatedAsyncView['getEndpoints']> {
  22. const query = this.props.location.query;
  23. // Because this route happens outside of OrganizationContext we
  24. // need to use initial data to decide which host to send the request to
  25. // as `/accept-transfer/` cannot be resolved to a region.
  26. const initialData = window.__initialData;
  27. let host: string | undefined = undefined;
  28. if (initialData && initialData.links?.regionUrl !== initialData.links?.sentryUrl) {
  29. host = initialData.links.regionUrl;
  30. }
  31. return [['transferDetails', '/accept-transfer/', {query, host}]];
  32. }
  33. getTitle() {
  34. return t('Accept Project Transfer');
  35. }
  36. handleSubmit = formData => {
  37. this.api.request('/accept-transfer/', {
  38. method: 'POST',
  39. data: {
  40. data: this.props.location.query.data,
  41. organization: formData.organization,
  42. },
  43. success: () => {
  44. const orgSlug = formData.organization;
  45. const projectSlug = this.state?.transferDetails?.project.slug;
  46. const sentryUrl = ConfigStore.get('links').sentryUrl;
  47. if (!projectSlug) {
  48. window.location.href = `${sentryUrl}/organizations/${orgSlug}/projects/`;
  49. } else {
  50. window.location.href = `${sentryUrl}/organizations/${orgSlug}/settings/projects/${projectSlug}/teams/`;
  51. // done this way since we need to change subdomains
  52. }
  53. },
  54. error: error => {
  55. const errorMsg =
  56. error && error.responseJSON && typeof error.responseJSON.detail === 'string'
  57. ? error.responseJSON.detail
  58. : '';
  59. addErrorMessage(
  60. t('Unable to transfer project') + errorMsg ? `: ${errorMsg}` : ''
  61. );
  62. },
  63. });
  64. };
  65. renderError(error) {
  66. let disableLog = false;
  67. // Check if there is an error message with `transferDetails` endpoint
  68. // If so, show as toast and ignore, otherwise log to sentry
  69. if (error && error.responseJSON && typeof error.responseJSON.detail === 'string') {
  70. addErrorMessage(error.responseJSON.detail);
  71. disableLog = true;
  72. }
  73. return super.renderError(error, disableLog);
  74. }
  75. renderBody() {
  76. const {transferDetails} = this.state;
  77. const options = transferDetails?.organizations.map(org => ({
  78. label: org.slug,
  79. value: org.slug,
  80. }));
  81. const organization = options?.[0]?.value;
  82. return (
  83. <NarrowLayout>
  84. <SettingsPageHeader title={t('Approve Transfer Project Request')} />
  85. <p>
  86. {tct(
  87. '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.)',
  88. {
  89. organization: <strong>{t('Organization')}</strong>,
  90. projectSettings: <strong>{t('Project Settings')}</strong>,
  91. }
  92. )}
  93. </p>
  94. {transferDetails && (
  95. <p>
  96. {tct(
  97. 'Please select which [organization] you want for the project [project].',
  98. {
  99. organization: <strong>{t('Organization')}</strong>,
  100. project: transferDetails.project.slug,
  101. }
  102. )}
  103. </p>
  104. )}
  105. <Form
  106. onSubmit={this.handleSubmit}
  107. submitLabel={t('Transfer Project')}
  108. submitPriority="danger"
  109. initialData={organization ? {organization} : undefined}
  110. >
  111. <SelectField
  112. options={options}
  113. label={t('Organization')}
  114. name="organization"
  115. style={{borderBottom: 'none'}}
  116. />
  117. </Form>
  118. </NarrowLayout>
  119. );
  120. }
  121. }
  122. export default AcceptProjectTransfer;