index.tsx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 useApi from 'sentry/utils/useApi';
  18. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  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 = createdOrg?.features.includes('customer-domains');
  79. let nextUrl = normalizeUrl(
  80. `/organizations/${createdOrg.slug}/projects/new/`,
  81. {forceCustomerDomain: hasCustomerDomain}
  82. );
  83. if (hasCustomerDomain) {
  84. nextUrl = `${createdOrg.links.organizationUrl}${nextUrl}`;
  85. }
  86. // redirect to project creation *(BYPASS REACT ROUTER AND FORCE PAGE REFRESH TO GRAB CSRF TOKEN)*
  87. // browserHistory.pushState(null, `/organizations/${data.slug}/projects/new/`);
  88. window.location.assign(nextUrl);
  89. }}
  90. onSubmitError={error => {
  91. addErrorMessage(
  92. error.responseJSON?.detail ?? t('Unable to create organization.')
  93. );
  94. }}
  95. requireChanges
  96. >
  97. <TextField
  98. id="organization-name"
  99. name="name"
  100. label={t('Organization Name')}
  101. placeholder={t('e.g. My Company')}
  102. inline={false}
  103. flexibleControlStateSize
  104. stacked
  105. required
  106. />
  107. {shouldDisplayRegions() && (
  108. <SelectField
  109. name="dataStorageLocation"
  110. label={t('Data Storage Location')}
  111. help={tct(
  112. "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]",
  113. {learnMore: <a href={DATA_STORAGE_DOCS_LINK} />}
  114. )}
  115. choices={regionChoices}
  116. inline={false}
  117. stacked
  118. required
  119. />
  120. )}
  121. {termsUrl && privacyUrl && (
  122. <TermsWrapper hasDataConsent={hasDataConsent}>
  123. <CheckboxField
  124. name="agreeTerms"
  125. label={tct(
  126. 'I agree to the [termsLink:Terms of Service] and the [privacyLink:Privacy Policy]',
  127. {
  128. termsLink: <a href={termsUrl} />,
  129. privacyLink: <a href={privacyUrl} />,
  130. }
  131. )}
  132. inline={false}
  133. stacked
  134. required
  135. />
  136. </TermsWrapper>
  137. )}
  138. <DataConsentCheck />
  139. {!isSelfHosted && ConfigStore.get('features').has('relocation:enabled') && (
  140. <div>
  141. {tct('[relocationLink:Relocating from self-hosted?]', {
  142. relocationLink: <a href={relocationUrl} />,
  143. })}
  144. </div>
  145. )}
  146. </Form>
  147. </NarrowLayout>
  148. </SentryDocumentTitle>
  149. );
  150. }
  151. export default OrganizationCreate;
  152. const TermsWrapper = styled('div')<{hasDataConsent?: boolean}>`
  153. margin-bottom: ${p => (p.hasDataConsent ? '0' : '16px')};
  154. `;