withOrganizations.tsx 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import {Component} from 'react';
  2. import OrganizationsStore from 'sentry/stores/organizationsStore';
  3. import type {OrganizationSummary} from 'sentry/types/organization';
  4. import getDisplayName from 'sentry/utils/getDisplayName';
  5. type InjectedOrganizationsProps = {
  6. organizations: OrganizationSummary[];
  7. organizationsLoading?: boolean;
  8. };
  9. type State = {
  10. organizations: OrganizationSummary[];
  11. };
  12. function withOrganizations<P extends InjectedOrganizationsProps>(
  13. WrappedComponent: React.ComponentType<P>
  14. ) {
  15. class WithOrganizations extends Component<
  16. Omit<P, keyof InjectedOrganizationsProps> & Partial<InjectedOrganizationsProps>,
  17. State
  18. > {
  19. static displayName = `withOrganizations(${getDisplayName(WrappedComponent)})`;
  20. state: State = {organizations: OrganizationsStore.getAll()};
  21. componentWillUnmount() {
  22. this.unsubscribe();
  23. }
  24. unsubscribe = OrganizationsStore.listen(
  25. (organizations: OrganizationSummary[]) => this.setState({organizations}),
  26. undefined
  27. );
  28. render() {
  29. const {organizationsLoading, organizations, ...props} = this.props as P;
  30. return (
  31. <WrappedComponent
  32. {...({
  33. organizationsLoading:
  34. organizationsLoading ?? !OrganizationsStore.getState().loaded,
  35. organizations: organizations ?? this.state.organizations,
  36. ...props,
  37. } as P)}
  38. />
  39. );
  40. }
  41. }
  42. return WithOrganizations;
  43. }
  44. export default withOrganizations;