useResolveRoute.tsx 1.4 KB

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