withOrganizations.tsx 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import {Component} from 'react';
  2. import OrganizationsStore from 'sentry/stores/organizationsStore';
  3. import {OrganizationSummary} from 'sentry/types';
  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: organizationsLoading ?? !OrganizationsStore.loaded,
  34. organizations: organizations ?? this.state.organizations,
  35. ...props,
  36. } as P)}
  37. />
  38. );
  39. }
  40. }
  41. return WithOrganizations;
  42. }
  43. export default withOrganizations;