withSentryRouter.tsx 1.1 KB

123456789101112131415161718192021222324252627282930
  1. // eslint-disable-next-line no-restricted-imports
  2. import type {WithRouterProps} from 'react-router';
  3. // eslint-disable-next-line no-restricted-imports
  4. import {withRouter} from 'react-router';
  5. import {CUSTOMER_DOMAIN, USING_CUSTOMER_DOMAIN} from 'sentry/constants';
  6. /**
  7. * withSentryRouter is a higher-order component (HOC) that wraps withRouter, and implicitly injects the current customer
  8. * domain as the orgId parameter. This only happens if a customer domain is currently being used.
  9. *
  10. * Since withRouter() is discouraged from being used on new React components, we would use withSentryRouter() on
  11. * pre-existing React components.
  12. */
  13. function withSentryRouter<P extends WithRouterProps>(
  14. WrappedComponent: React.ComponentType<P>
  15. ): React.ComponentType<Omit<P, keyof WithRouterProps>> {
  16. function WithSentryRouterWrapper(props: P) {
  17. const {params} = props;
  18. if (USING_CUSTOMER_DOMAIN) {
  19. const newParams = {...params, orgId: CUSTOMER_DOMAIN};
  20. return <WrappedComponent {...props} params={newParams} />;
  21. }
  22. return <WrappedComponent {...props} />;
  23. }
  24. return withRouter(WithSentryRouterWrapper);
  25. }
  26. export default withSentryRouter;