useResolveRoute.tsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import ConfigStore from 'sentry/stores/configStore';
  2. import {OrganizationSummary} from 'sentry/types';
  3. import {extractSlug} from 'sentry/utils/extractSlug';
  4. import shouldUseLegacyRoute from './shouldUseLegacyRoute';
  5. import useOrganization from './useOrganization';
  6. import {normalizeUrl} from './withDomainRequired';
  7. /**
  8. * In yarn dev-ui mode we proxy API calls to sentry.io.
  9. * However, all of the browser URLs are either acme.localhost,
  10. * or acme.dev.getsentry.net so we need to hack up the server provided
  11. * domain values.
  12. */
  13. function localizeDomain(domain?: string) {
  14. if (!window.__SENTRY_DEV_UI || !domain) {
  15. return domain;
  16. }
  17. const slugDomain = extractSlug(window.location.host);
  18. if (!slugDomain) {
  19. return domain;
  20. }
  21. return domain.replace('sentry.io', slugDomain.domain);
  22. }
  23. /**
  24. * If organization is passed, then a URL with the route will be returned with the customer domain prefix attached if the
  25. * organization has customer domain feature enabled.
  26. * Otherwise, if the organization is not given, then if the current organization has customer domain enabled, then we
  27. * use the sentry URL as the prefix.
  28. */
  29. function useResolveRoute(route: string, organization?: OrganizationSummary) {
  30. const currentOrganization = useOrganization({allowNull: true});
  31. const hasCustomerDomain = currentOrganization?.features.includes('customer-domains');
  32. const sentryUrl = localizeDomain(ConfigStore.get('links').sentryUrl);
  33. if (!organization) {
  34. if (hasCustomerDomain) {
  35. return `${sentryUrl}${normalizeUrl(route)}`;
  36. }
  37. return route;
  38. }
  39. const organizationUrl = localizeDomain(organization.links.organizationUrl);
  40. const useLegacyRoute = shouldUseLegacyRoute(organization);
  41. if (useLegacyRoute) {
  42. if (hasCustomerDomain) {
  43. // If the current org is a customer domain, then we need to change the hostname in addition to
  44. // updating the path.
  45. return `${sentryUrl}${route}`;
  46. }
  47. return route;
  48. }
  49. return `${organizationUrl}${normalizeUrl(route)}`;
  50. }
  51. export default useResolveRoute;