Files
sentinel/static/app/utils/withSentryRouter.tsx
2024-02-15 17:01:50 +00:00

31 lines
1.1 KiB
TypeScript

// eslint-disable-next-line no-restricted-imports
import type {WithRouterProps} from 'react-router';
// eslint-disable-next-line no-restricted-imports
import {withRouter} from 'react-router';
import {CUSTOMER_DOMAIN, USING_CUSTOMER_DOMAIN} from 'sentry/constants';
/**
* withSentryRouter is a higher-order component (HOC) that wraps withRouter, and implicitly injects the current customer
* domain as the orgId parameter. This only happens if a customer domain is currently being used.
*
* Since withRouter() is discouraged from being used on new React components, we would use withSentryRouter() on
* pre-existing React components.
*/
function withSentryRouter<P extends WithRouterProps>(
WrappedComponent: React.ComponentType<P>
) {
function WithSentryRouterWrapper(props: P) {
const {params} = props;
if (USING_CUSTOMER_DOMAIN) {
const newParams = {...params, orgId: CUSTOMER_DOMAIN};
return <WrappedComponent {...props} params={newParams} />;
}
return <WrappedComponent {...props} />;
}
return withRouter(WithSentryRouterWrapper);
}
export default withSentryRouter;