withOrganization.tsx 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import * as React from 'react';
  2. import SentryTypes from 'app/sentryTypes';
  3. import {LightWeightOrganization, Organization} from 'app/types';
  4. import getDisplayName from 'app/utils/getDisplayName';
  5. type InjectedOrganizationProps = {
  6. organization?: Organization | LightWeightOrganization;
  7. };
  8. const withOrganization = <P extends InjectedOrganizationProps>(
  9. WrappedComponent: React.ComponentType<P>
  10. ) =>
  11. class extends React.Component<
  12. Omit<P, keyof InjectedOrganizationProps> & InjectedOrganizationProps
  13. > {
  14. static displayName = `withOrganization(${getDisplayName(WrappedComponent)})`;
  15. static contextTypes = {
  16. organization: SentryTypes.Organization,
  17. };
  18. render() {
  19. const {organization, ...props} = this.props;
  20. return (
  21. <WrappedComponent
  22. {...({
  23. organization: organization ?? this.context.organization,
  24. ...props,
  25. } as P)}
  26. />
  27. );
  28. }
  29. };
  30. export function isLightweightOrganization(
  31. organization: Organization | LightWeightOrganization
  32. ): organization is LightWeightOrganization {
  33. const castedOrg = organization as Organization;
  34. return !(castedOrg.projects && castedOrg.teams);
  35. }
  36. export default withOrganization;