withDomainRequired.tsx 4.1 KB

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