withProject.tsx 977 B

123456789101112131415161718192021222324252627282930313233343536
  1. import {Component} from 'react';
  2. import {SentryPropTypeValidators} from 'sentry/sentryPropTypeValidators';
  3. import type {Project} from 'sentry/types';
  4. import getDisplayName from 'sentry/utils/getDisplayName';
  5. type InjectedProjectProps = {
  6. project?: Project;
  7. };
  8. /**
  9. * Currently wraps component with project from context
  10. */
  11. const withProject = <P extends InjectedProjectProps>(
  12. WrappedComponent: React.ComponentType<P>
  13. ) =>
  14. class extends Component<
  15. Omit<P, keyof InjectedProjectProps> & Partial<InjectedProjectProps>
  16. > {
  17. static displayName = `withProject(${getDisplayName(WrappedComponent)})`;
  18. static contextTypes = {
  19. project: SentryPropTypeValidators.isProject,
  20. };
  21. declare context: {project: Project};
  22. render() {
  23. const {project, ...props} = this.props;
  24. return (
  25. <WrappedComponent
  26. {...({project: project ?? this.context.project, ...props} as P)}
  27. />
  28. );
  29. }
  30. };
  31. export default withProject;