withRouteAnalytics.tsx 922 B

12345678910111213141516171819202122232425
  1. import {useContext} from 'react';
  2. import getDisplayName from 'sentry/utils/getDisplayName';
  3. import {RouteAnalyticsContext} from 'sentry/views/routeAnalyticsContextProvider';
  4. type WithRouteAnalyticsProps = React.ContextType<typeof RouteAnalyticsContext>;
  5. type WrappedProps<P> = Omit<P, keyof WithRouteAnalyticsProps> &
  6. Partial<WithRouteAnalyticsProps>;
  7. const withRouteAnalytics = <P extends WithRouteAnalyticsProps>(
  8. WrappedComponent: React.ComponentType<P>
  9. ) => {
  10. const ComponentWithRouteAnalytics: React.FC<WrappedProps<P>> = props => {
  11. const routeAnalyticsContext = useContext(RouteAnalyticsContext);
  12. return <WrappedComponent {...(props as P)} {...routeAnalyticsContext} />;
  13. };
  14. ComponentWithRouteAnalytics.displayName = `withRouteAnalytics(${getDisplayName(
  15. WrappedComponent
  16. )})`;
  17. return ComponentWithRouteAnalytics;
  18. };
  19. export default withRouteAnalytics;
  20. export {WithRouteAnalyticsProps};