withSentryRouter.tsx 1.3 KB

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