useResolveRoute.tsx 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import {useContext} from 'react';
  2. import ConfigStore from 'sentry/stores/configStore';
  3. import {OrganizationSummary} from 'sentry/types';
  4. import {OrganizationContext} from 'sentry/views/organizationContext';
  5. import shouldUseLegacyRoute from './shouldUseLegacyRoute';
  6. import {normalizeUrl} from './withDomainRequired';
  7. /**
  8. * If organization is passed, then a URL with the route will be returned with the customer domain prefix attached if the
  9. * organization has customer domain feature enabled.
  10. * Otherwise, if the organization is not given, then if the current organization has customer domain enabled, then we
  11. * use the sentry URL as the prefix.
  12. */
  13. function useResolveRoute(route: string, organization?: OrganizationSummary) {
  14. const {sentryUrl} = ConfigStore.get('links');
  15. const currentOrganization = useContext(OrganizationContext);
  16. const hasCustomerDomain = currentOrganization?.features.includes('customer-domains');
  17. if (!organization) {
  18. if (hasCustomerDomain) {
  19. return `${sentryUrl}${normalizeUrl(route)}`;
  20. }
  21. return route;
  22. }
  23. const {organizationUrl} = organization.links;
  24. const useLegacyRoute = shouldUseLegacyRoute(organization);
  25. if (useLegacyRoute) {
  26. if (hasCustomerDomain) {
  27. // If the current org is a customer domain, then we need to change the hostname in addition to
  28. // updating the path.
  29. return `${sentryUrl}${route}`;
  30. }
  31. return route;
  32. }
  33. return `${organizationUrl}${normalizeUrl(route)}`;
  34. }
  35. export default useResolveRoute;