withConfig.tsx 853 B

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