index.tsx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import {useCallback} from 'react';
  2. import styled from '@emotion/styled';
  3. import {addErrorMessage, addLoadingMessage} from 'sentry/actionCreators/indicator';
  4. import CheckboxField from 'sentry/components/forms/fields/checkboxField';
  5. import SelectField from 'sentry/components/forms/fields/selectField';
  6. import TextField from 'sentry/components/forms/fields/textField';
  7. import Form from 'sentry/components/forms/form';
  8. import type {OnSubmitCallback} from 'sentry/components/forms/types';
  9. import HookOrDefault from 'sentry/components/hookOrDefault';
  10. import NarrowLayout from 'sentry/components/narrowLayout';
  11. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  12. import {t, tct} from 'sentry/locale';
  13. import ConfigStore from 'sentry/stores/configStore';
  14. import HookStore from 'sentry/stores/hookStore';
  15. import type {OrganizationSummary} from 'sentry/types/organization';
  16. import {getRegionChoices, shouldDisplayRegions} from 'sentry/utils/regions';
  17. import normalizeUrl from 'sentry/utils/url/normalizeUrl';
  18. import useApi from 'sentry/utils/useApi';
  19. export const DATA_STORAGE_DOCS_LINK =
  20. 'https://docs.sentry.io/product/accounts/choose-your-data-center';
  21. function removeDataStorageLocationFromFormData(
  22. formData: Record<string, any>
  23. ): Record<string, any> {
  24. const shallowFormDataClone = {...formData};
  25. delete shallowFormDataClone.dataStorageLocation;
  26. return shallowFormDataClone;
  27. }
  28. function OrganizationCreate() {
  29. const termsUrl = ConfigStore.get('termsUrl');
  30. const privacyUrl = ConfigStore.get('privacyUrl');
  31. const isSelfHosted = ConfigStore.get('isSelfHosted');
  32. const relocationUrl = normalizeUrl(`/relocation/`);
  33. const regionChoices = getRegionChoices();
  34. const client = useApi();
  35. const DataConsentCheck = HookOrDefault({
  36. hookName: 'component:data-consent-org-creation-checkbox',
  37. defaultComponent: null,
  38. });
  39. const hasDataConsent =
  40. HookStore.get('component:data-consent-org-creation-checkbox').length !== 0;
  41. // This is a trimmed down version of the logic in ApiForm. It validates the
  42. // form data prior to submitting the request, and overrides the request host
  43. // with the selected region's URL if one is provided.
  44. const submitOrganizationCreate: OnSubmitCallback = useCallback(
  45. (data, onSubmitSuccess, onSubmitError, _event, formModel) => {
  46. if (!formModel.validateForm()) {
  47. return;
  48. }
  49. const regionUrl = data.dataStorageLocation;
  50. addLoadingMessage(t('Creating Organization\u2026'));
  51. formModel.setFormSaving();
  52. client.request('/organizations/', {
  53. method: 'POST',
  54. data: removeDataStorageLocationFromFormData(data),
  55. host: regionUrl,
  56. success: onSubmitSuccess,
  57. error: onSubmitError,
  58. });
  59. },
  60. [client]
  61. );
  62. return (
  63. <SentryDocumentTitle title={t('Create Organization')}>
  64. <NarrowLayout showLogout>
  65. <h3>{t('Create a New Organization')}</h3>
  66. <p>
  67. {t(
  68. "Organizations represent the top level in your hierarchy. You'll be able to bundle a collection of teams within an organization as well as give organization-wide permissions to users."
  69. )}
  70. </p>
  71. <Form
  72. initialData={{defaultTeam: true}}
  73. submitLabel={t('Create Organization')}
  74. apiEndpoint="/organizations/"
  75. apiMethod="POST"
  76. onSubmit={submitOrganizationCreate}
  77. onSubmitSuccess={(createdOrg: OrganizationSummary) => {
  78. const hasCustomerDomain =
  79. ConfigStore.get('features').has('system:multi-region');
  80. let nextUrl = normalizeUrl(
  81. `/organizations/${createdOrg.slug}/projects/new/`,
  82. {forceCustomerDomain: hasCustomerDomain}
  83. );
  84. if (hasCustomerDomain) {
  85. nextUrl = `${createdOrg.links.organizationUrl}${nextUrl}`;
  86. }
  87. // redirect to project creation *(BYPASS REACT ROUTER AND FORCE PAGE REFRESH TO GRAB CSRF TOKEN)*
  88. // browserHistory.pushState(null, `/organizations/${data.slug}/projects/new/`);
  89. window.location.assign(nextUrl);
  90. }}
  91. onSubmitError={error => {
  92. addErrorMessage(
  93. error.responseJSON?.detail ?? t('Unable to create organization.')
  94. );
  95. }}
  96. requireChanges
  97. >
  98. <TextField
  99. id="organization-name"
  100. name="name"
  101. label={t('Organization Name')}
  102. placeholder={t('e.g. My Company')}
  103. inline={false}
  104. flexibleControlStateSize
  105. stacked
  106. required
  107. />
  108. {shouldDisplayRegions() && (
  109. <SelectField
  110. name="dataStorageLocation"
  111. label={t('Data Storage Location')}
  112. help={tct(
  113. "Choose where to store your organization's data. Please note, you won't be able to change locations once your organization has been created. [learnMore:Learn More]",
  114. {learnMore: <a href={DATA_STORAGE_DOCS_LINK} />}
  115. )}
  116. choices={regionChoices}
  117. inline={false}
  118. stacked
  119. required
  120. />
  121. )}
  122. {termsUrl && privacyUrl && (
  123. <TermsWrapper hasDataConsent={hasDataConsent}>
  124. <CheckboxField
  125. name="agreeTerms"
  126. label={tct(
  127. 'I agree to the [termsLink:Terms of Service] and the [privacyLink:Privacy Policy]',
  128. {
  129. termsLink: <a href={termsUrl} />,
  130. privacyLink: <a href={privacyUrl} />,
  131. }
  132. )}
  133. inline={false}
  134. stacked
  135. required
  136. />
  137. </TermsWrapper>
  138. )}
  139. <DataConsentCheck />
  140. {!isSelfHosted && ConfigStore.get('features').has('relocation:enabled') && (
  141. <div>
  142. {tct('[relocationLink:Relocating from self-hosted?]', {
  143. relocationLink: <a href={relocationUrl} />,
  144. })}
  145. </div>
  146. )}
  147. </Form>
  148. </NarrowLayout>
  149. </SentryDocumentTitle>
  150. );
  151. }
  152. export default OrganizationCreate;
  153. const TermsWrapper = styled('div')<{hasDataConsent?: boolean}>`
  154. margin-bottom: ${p => (p.hasDataConsent ? '0' : '16px')};
  155. `;