Files
sentinel/static/app/utils/withOrganizations.tsx
2024-02-15 17:01:50 +00:00

54 lines
1.4 KiB
TypeScript

import {Component} from 'react';
import OrganizationsStore from 'sentry/stores/organizationsStore';
import type {OrganizationSummary} from 'sentry/types';
import getDisplayName from 'sentry/utils/getDisplayName';
type InjectedOrganizationsProps = {
organizations: OrganizationSummary[];
organizationsLoading?: boolean;
};
type State = {
organizations: OrganizationSummary[];
};
function withOrganizations<P extends InjectedOrganizationsProps>(
WrappedComponent: React.ComponentType<P>
) {
class WithOrganizations extends Component<
Omit<P, keyof InjectedOrganizationsProps> & Partial<InjectedOrganizationsProps>,
State
> {
static displayName = `withOrganizations(${getDisplayName(WrappedComponent)})`;
state: State = {organizations: OrganizationsStore.getAll()};
componentWillUnmount() {
this.unsubscribe();
}
unsubscribe = OrganizationsStore.listen(
(organizations: OrganizationSummary[]) => this.setState({organizations}),
undefined
);
render() {
const {organizationsLoading, organizations, ...props} = this.props as P;
return (
<WrappedComponent
{...({
organizationsLoading: organizationsLoading ?? !OrganizationsStore.loaded,
organizations: organizations ?? this.state.organizations,
...props,
} as P)}
/>
);
}
}
return WithOrganizations;
}
export default withOrganizations;