resolveRoute.tsx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import {DEPLOY_PREVIEW_CONFIG} from 'sentry/constants';
  2. import ConfigStore from 'sentry/stores/configStore';
  3. import type {OrganizationSummary} from 'sentry/types';
  4. import {extractSlug} from 'sentry/utils/extractSlug';
  5. import {normalizeUrl} from './withDomainRequired';
  6. /**
  7. * In yarn dev-ui mode we proxy API calls to sentry.io.
  8. * However, browser URLs are in the form of acme.dev.getsentry.net.
  9. * In order to not redirect to production we swap domains.
  10. */
  11. function localizeDomain(domain?: string) {
  12. if (!window.__SENTRY_DEV_UI || !domain) {
  13. return domain;
  14. }
  15. // Vercel doesn't support subdomains, stay on the current host.
  16. if (DEPLOY_PREVIEW_CONFIG) {
  17. return `https://${window.location.host}`;
  18. }
  19. const slugDomain = extractSlug(window.location.host);
  20. if (!slugDomain) {
  21. return domain;
  22. }
  23. return domain.replace('sentry.io', slugDomain.domain);
  24. }
  25. /**
  26. * Decide if an organization uses slug based paths.
  27. */
  28. function shouldUseSlugPath(organization: OrganizationSummary): boolean {
  29. const {organizationUrl} = organization.links;
  30. return !organizationUrl || !organization.features.includes('customer-domains');
  31. }
  32. /**
  33. * If organization is passed, then a URL with the route will be returned with the customer domain prefix attached if the
  34. * organization has customer domain feature enabled.
  35. *
  36. * Otherwise, if the organization is not given, then if the current organization has customer domain enabled, then we
  37. * use the sentry URL as the prefix.
  38. */
  39. function resolveRoute(
  40. route: string,
  41. currentOrganization: OrganizationSummary | null,
  42. organization?: OrganizationSummary
  43. ) {
  44. const hasCustomerDomain = currentOrganization?.features.includes('customer-domains');
  45. const sentryUrl = localizeDomain(ConfigStore.get('links').sentryUrl);
  46. // If only one organization was provided we're not switching orgs,
  47. // and thus not switching domains.
  48. if (!organization) {
  49. return normalizeUrl(route);
  50. }
  51. const organizationUrl = localizeDomain(organization.links.organizationUrl);
  52. const useSlugPath = shouldUseSlugPath(organization);
  53. if (useSlugPath) {
  54. if (hasCustomerDomain) {
  55. // If the current org is a customer domain, then we need to change the hostname in addition to
  56. // updating the path.
  57. return `${sentryUrl}${route}`;
  58. }
  59. return route;
  60. }
  61. return `${organizationUrl}${normalizeUrl(route)}`;
  62. }
  63. export {localizeDomain, resolveRoute};