withPromotions.tsx 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import type LoadingIndicator from 'sentry/components/loadingIndicator';
  2. import type {QueryClient, QueryObserverResult} from 'sentry/utils/queryClient';
  3. import {useQueryClient} from 'sentry/utils/queryClient';
  4. import useOrganization from 'sentry/utils/useOrganization';
  5. import type {PromotionData} from 'getsentry/types';
  6. import usePromotionTriggerCheck from 'getsentry/utils/usePromotionTriggerCheck';
  7. type InjectedPromotionProps = {
  8. isError?: boolean;
  9. isLoading?: boolean;
  10. promotionData?: PromotionData;
  11. queryClient?: QueryClient;
  12. refetch?: () => Promise<QueryObserverResult<PromotionData, unknown>>;
  13. };
  14. const withPromotions = <P extends InjectedPromotionProps>(
  15. WrappedComponent: React.ComponentType<P> | typeof LoadingIndicator
  16. ) => {
  17. function WithPromotions(props: Omit<P, keyof InjectedPromotionProps>) {
  18. const organization = useOrganization();
  19. const {isPending, isError, data, refetch} = usePromotionTriggerCheck(organization);
  20. const queryClient = useQueryClient();
  21. const innerProps = {
  22. promotionData: data,
  23. isLoading: isPending,
  24. isError,
  25. refetch,
  26. queryClient,
  27. ...props,
  28. } as P;
  29. // TODO(any): HoC prop types not working w/ emotion https://github.com/emotion-js/emotion/issues/3261
  30. return <WrappedComponent {...(innerProps as any)} />;
  31. }
  32. return WithPromotions;
  33. };
  34. export default withPromotions;