index.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import {useState} from 'react';
  2. import {addErrorMessage} from 'sentry/actionCreators/indicator';
  3. import ApiForm from 'sentry/components/forms/apiForm';
  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 NarrowLayout from 'sentry/components/narrowLayout';
  8. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  9. import {t, tct} from 'sentry/locale';
  10. import ConfigStore from 'sentry/stores/configStore';
  11. import {OrganizationSummary} from 'sentry/types';
  12. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  13. enum RegionDisplayName {
  14. US = '🇺🇸 United States of America (US)',
  15. DE = '🇪🇺 European Union (EU)',
  16. }
  17. function getRegionChoices(): [string, string][] {
  18. const regions = ConfigStore.get('regions') ?? [];
  19. return regions.map(({name, url}) => {
  20. const regionName = name.toUpperCase();
  21. if (RegionDisplayName[regionName]) {
  22. return [url, RegionDisplayName[regionName]];
  23. }
  24. return [url, name];
  25. });
  26. }
  27. function getDefaultRegionChoice(
  28. regionChoices: [string, string][]
  29. ): [string, string] | undefined {
  30. if (!shouldDisplayRegions()) {
  31. return undefined;
  32. }
  33. const usRegion = regionChoices.find(
  34. ([_, regionName]) => regionName === RegionDisplayName.US
  35. );
  36. if (usRegion) {
  37. return usRegion;
  38. }
  39. return regionChoices[0];
  40. }
  41. function shouldDisplayRegions(): boolean {
  42. const regionCount = (ConfigStore.get('regions') ?? []).length;
  43. return (
  44. ConfigStore.get('features').has('organizations:multi-region-selector') &&
  45. regionCount > 1
  46. );
  47. }
  48. function removeRegionFromRequestForm(formData: Record<string, any>) {
  49. const shallowFormDataCopy = {...formData};
  50. delete shallowFormDataCopy.region;
  51. return shallowFormDataCopy;
  52. }
  53. function OrganizationCreate() {
  54. const termsUrl = ConfigStore.get('termsUrl');
  55. const privacyUrl = ConfigStore.get('privacyUrl');
  56. const regionChoices = getRegionChoices();
  57. const [regionUrl, setRegion] = useState<string | undefined>(
  58. getDefaultRegionChoice(regionChoices)?.[0]
  59. );
  60. return (
  61. <SentryDocumentTitle title={t('Create Organization')}>
  62. <NarrowLayout showLogout>
  63. <h3>{t('Create a New Organization')}</h3>
  64. <p>
  65. {t(
  66. "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."
  67. )}
  68. </p>
  69. <ApiForm
  70. initialData={{defaultTeam: true}}
  71. submitLabel={t('Create Organization')}
  72. apiEndpoint="/organizations/"
  73. apiMethod="POST"
  74. hostOverride={regionUrl}
  75. onSubmit={removeRegionFromRequestForm}
  76. onSubmitSuccess={(createdOrg: OrganizationSummary) => {
  77. const hasCustomerDomain = createdOrg?.features.includes('customer-domains');
  78. let nextUrl = normalizeUrl(
  79. `/organizations/${createdOrg.slug}/projects/new/`,
  80. {forceCustomerDomain: hasCustomerDomain}
  81. );
  82. if (hasCustomerDomain) {
  83. nextUrl = `${createdOrg.links.organizationUrl}${nextUrl}`;
  84. }
  85. // redirect to project creation *(BYPASS REACT ROUTER AND FORCE PAGE REFRESH TO GRAB CSRF TOKEN)*
  86. // browserHistory.pushState(null, `/organizations/${data.slug}/projects/new/`);
  87. window.location.assign(nextUrl);
  88. }}
  89. onSubmitError={error => {
  90. addErrorMessage(
  91. error.responseJSON?.detail ?? t('Unable to create organization.')
  92. );
  93. }}
  94. requireChanges
  95. >
  96. <TextField
  97. id="organization-name"
  98. name="name"
  99. label={t('Organization Name')}
  100. placeholder={t('e.g. My Company')}
  101. inline={false}
  102. flexibleControlStateSize
  103. stacked
  104. required
  105. />
  106. {shouldDisplayRegions() && (
  107. <SelectField
  108. name="region"
  109. label="Data Storage"
  110. help="Where will this organization reside?"
  111. defaultValue={getDefaultRegionChoice(regionChoices)?.[0]}
  112. choices={regionChoices}
  113. onChange={setRegion}
  114. inline={false}
  115. stacked
  116. required
  117. />
  118. )}
  119. {termsUrl && privacyUrl && (
  120. <CheckboxField
  121. name="agreeTerms"
  122. label={tct(
  123. 'I agree to the [termsLink:Terms of Service] and the [privacyLink:Privacy Policy]',
  124. {
  125. termsLink: <a href={termsUrl} />,
  126. privacyLink: <a href={privacyUrl} />,
  127. }
  128. )}
  129. inline={false}
  130. stacked
  131. required
  132. />
  133. )}
  134. </ApiForm>
  135. </NarrowLayout>
  136. </SentryDocumentTitle>
  137. );
  138. }
  139. export default OrganizationCreate;