import {Component} from 'react';
import SentryAppComponentsStore from 'sentry/stores/sentryAppComponentsStore';
import {SentryAppComponent} from 'sentry/types';
import getDisplayName from 'sentry/utils/getDisplayName';
type InjectedAppComponentsProps = {
components: SentryAppComponent[];
};
type State = {
components: SentryAppComponent[];
};
type Options = {
componentType?: SentryAppComponent['type'];
};
function withSentryAppComponents
(
WrappedComponent: React.ComponentType
,
{componentType}: Options = {}
) {
class WithSentryAppComponents extends Component<
Omit
& Partial,
State
> {
static displayName = `withSentryAppComponents(${getDisplayName(WrappedComponent)})`;
state = {components: SentryAppComponentsStore.getAll()};
componentWillUnmount() {
this.unsubscribe();
}
unsubscribe = SentryAppComponentsStore.listen(
() => this.setState({components: SentryAppComponentsStore.getAll()}),
undefined
);
render() {
const {components: propComponents, ...props} = this.props as P;
const storeComponents = componentType
? this.state.components.filter(item => item.type === componentType)
: this.state.components;
const components = propComponents ?? storeComponents;
return ;
}
}
return WithSentryAppComponents;
}
export default withSentryAppComponents;