withConfig.tsx 886 B

123456789101112131415161718192021222324252627282930313233
  1. import * as React from 'react';
  2. import ConfigStore from 'sentry/stores/configStore';
  3. import {useLegacyStore} from 'sentry/stores/useLegacyStore';
  4. import {Config} from 'sentry/types';
  5. import getDisplayName from 'sentry/utils/getDisplayName';
  6. type InjectedConfigProps = {
  7. config: Config;
  8. };
  9. /**
  10. * Higher order component that passes the config object to the wrapped
  11. * component
  12. */
  13. function withConfig<P extends InjectedConfigProps>(
  14. WrappedComponent: React.ComponentType<P>
  15. ) {
  16. type Props = Omit<P, keyof InjectedConfigProps> & Partial<InjectedConfigProps>;
  17. const Wrapper: React.FC<Props> = props => {
  18. const config = useLegacyStore(ConfigStore);
  19. const allProps = {config, ...props} as P;
  20. return <WrappedComponent {...allProps} />;
  21. };
  22. Wrapper.displayName = `withConfig(${getDisplayName(WrappedComponent)})`;
  23. return Wrapper;
  24. }
  25. export default withConfig;