withDomainRequired.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import type {RouteComponent, RouteComponentProps} from 'react-router';
  2. import type {Location, LocationDescriptor} from 'history';
  3. import trimEnd from 'lodash/trimEnd';
  4. import trimStart from 'lodash/trimStart';
  5. // If you change this also update the patterns in sentry.api.utils
  6. const NORMALIZE_PATTERNS: Array<[pattern: RegExp, replacement: string]> = [
  7. // /organizations/slug/section, but not /organizations/new
  8. [/\/organizations\/(?!new)[^\/]+\/(.*)/, '/$1'],
  9. // For /settings/:orgId/ -> /settings/organization/
  10. [
  11. /\/settings\/(?!account\/|billing\/|projects\/|teams\/)[^\/]+\/?$/,
  12. '/settings/organization/',
  13. ],
  14. // Move /settings/:orgId/:section -> /settings/:section
  15. // but not /settings/organization or /settings/projects which is a new URL
  16. [
  17. /^\/?settings\/(?!account\/|billing\/|projects\/|teams\/)[^\/]+\/(.*)/,
  18. '/settings/$1',
  19. ],
  20. [/^\/?join-request\/[^\/]+\/?.*/, '/join-request/'],
  21. [/^\/?onboarding\/[^\/]+\/(.*)/, '/onboarding/$1'],
  22. // Handles /org-slug/project-slug/getting-started/platform/ -> /getting-started/project-slug/platform/
  23. [/^\/?(?!settings)[^\/]+\/([^\/]+)\/getting-started\/(.*)/, '/getting-started/$1/$2'],
  24. [/^\/?accept-terms\/[^\/]*\/?$/, '/accept-terms/'],
  25. ];
  26. type NormalizeUrlOptions = {
  27. forceCustomerDomain: boolean;
  28. };
  29. /**
  30. * Normalize a URL for customer domains based on the organization that was
  31. * present in the initial page load.
  32. */
  33. export function normalizeUrl(path: string, options?: NormalizeUrlOptions): string;
  34. export function normalizeUrl(
  35. path: LocationDescriptor,
  36. options?: NormalizeUrlOptions
  37. ): LocationDescriptor;
  38. export function normalizeUrl(
  39. path: LocationDescriptor,
  40. location?: Location,
  41. options?: NormalizeUrlOptions
  42. ): LocationDescriptor;
  43. export function normalizeUrl(
  44. path: LocationDescriptor,
  45. location?: Location | NormalizeUrlOptions,
  46. options?: NormalizeUrlOptions
  47. ): LocationDescriptor {
  48. if (location && 'forceCustomerDomain' in location) {
  49. options = location;
  50. location = undefined;
  51. }
  52. if (!options?.forceCustomerDomain && !window.__initialData?.customerDomain) {
  53. return path;
  54. }
  55. let resolved = path;
  56. if (typeof resolved === 'string') {
  57. for (const patternData of NORMALIZE_PATTERNS) {
  58. resolved = resolved.replace(patternData[0], patternData[1]);
  59. if (resolved !== path) {
  60. return resolved;
  61. }
  62. }
  63. return resolved;
  64. }
  65. if (!resolved.pathname) {
  66. return resolved;
  67. }
  68. for (const patternData of NORMALIZE_PATTERNS) {
  69. const replacement = resolved.pathname.replace(patternData[0], patternData[1]);
  70. if (replacement !== resolved.pathname) {
  71. return {...resolved, pathname: replacement};
  72. }
  73. }
  74. return resolved;
  75. }
  76. /**
  77. * withDomainRequired is a higher-order component (HOC) meant to be used with <Route /> components within
  78. * static/app/routes.tsx whose route paths do not contain the :orgId parameter.
  79. * For example:
  80. *
  81. * <Route
  82. * path="/issues/(searches/:searchId/)"
  83. * component={withDomainRequired(errorHandler(IssueListContainer))}
  84. * / >
  85. *
  86. * withDomainRequired ensures that the route path is only accessed whenever a customer domain is used.
  87. * For example: orgslug.sentry.io
  88. *
  89. * The side-effect that this HOC provides is that it'll redirect the browser to sentryUrl
  90. * (from window.__initialData.links) whenever one of the following conditions are not satisfied:
  91. *
  92. * - window.__initialData.customerDomain is present.
  93. * - window.__initialData.features contains organizations:customer-domains feature.
  94. *
  95. * If both conditions above are satisfied, then WrappedComponent will be rendered with orgId included in the route
  96. * params prop.
  97. *
  98. * Whenever https://orgslug.sentry.io/ is accessed in the browser, then both conditions above will be satisfied.
  99. */
  100. function withDomainRequired<P extends RouteComponentProps<{}, {}>>(
  101. WrappedComponent: RouteComponent
  102. ) {
  103. return function withDomainRequiredWrapper(props: P) {
  104. const {params} = props;
  105. const {features, customerDomain} = window.__initialData;
  106. const {sentryUrl} = window.__initialData.links;
  107. const hasCustomerDomain = (features as unknown as string[]).includes(
  108. 'organizations:customer-domains'
  109. );
  110. if (!customerDomain || !hasCustomerDomain) {
  111. // This route should only be accessed if a customer domain is used.
  112. // We redirect the user to the sentryUrl.
  113. const redirectPath = `${window.location.pathname}${window.location.search}${window.location.hash}`;
  114. const redirectURL = `${trimEnd(sentryUrl, '/')}/${trimStart(redirectPath, '/')}`;
  115. window.location.replace(redirectURL);
  116. return null;
  117. }
  118. const newParams = {
  119. ...params,
  120. orgId: customerDomain.subdomain,
  121. };
  122. return <WrappedComponent {...props} params={newParams} />;
  123. };
  124. }
  125. export default withDomainRequired;