123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- import * as React from 'react';
- import {getProjectRelease, getReleaseDeploys} from 'sentry/actionCreators/release';
- import {Client} from 'sentry/api';
- import ReleaseStore from 'sentry/stores/releaseStore';
- import {Deploy, Organization, Release} from 'sentry/types';
- import getDisplayName from 'sentry/utils/getDisplayName';
- type DependentProps = {
- api: Client;
- organization: Organization;
- projectSlug: string;
- releaseVersion: string;
- };
- type InjectedProps = {
- deploys?: Array<Deploy>;
- deploysError?: Error;
- deploysLoading?: boolean;
- release?: Release;
- releaseError?: Error;
- releaseLoading?: boolean;
- };
- function withRelease<P extends DependentProps>(WrappedComponent: React.ComponentType<P>) {
- class WithRelease extends React.Component<
- Omit<P, keyof InjectedProps> & Partial<InjectedProps> & DependentProps,
- InjectedProps
- > {
- static displayName = `withRelease(${getDisplayName(WrappedComponent)})`;
- constructor(props, context) {
- super(props, context);
- const {projectSlug, releaseVersion} = this.props;
- const releaseData = ReleaseStore.get(projectSlug, releaseVersion);
- this.state = {...releaseData};
- }
- componentDidMount() {
- this.fetchRelease();
- this.fetchDeploys();
- }
- componentWillUnmount() {
- this.unsubscribe();
- }
- unsubscribe = ReleaseStore.listen(() => this.onStoreUpdate(), undefined);
- fetchRelease() {
- const {api, organization, projectSlug, releaseVersion} = this.props;
- const releaseData = ReleaseStore.get(projectSlug, releaseVersion);
- const orgSlug = organization.slug;
- if (
- (!releaseData.release && !releaseData.releaseLoading) ||
- releaseData.releaseError
- ) {
- getProjectRelease(api, {orgSlug, projectSlug, releaseVersion});
- }
- }
- fetchDeploys() {
- const {api, organization, projectSlug, releaseVersion} = this.props;
- const releaseData = ReleaseStore.get(projectSlug, releaseVersion);
- const orgSlug = organization.slug;
- if (
- (!releaseData.deploys && !releaseData.deploysLoading) ||
- releaseData.deploysError
- ) {
- getReleaseDeploys(api, {orgSlug, projectSlug, releaseVersion});
- }
- }
- onStoreUpdate() {
- const {projectSlug, releaseVersion} = this.props;
- const releaseData = ReleaseStore.get(projectSlug, releaseVersion);
- this.setState({...releaseData});
- }
- render() {
- return (
- <WrappedComponent
- {...(this.props as P & DependentProps)}
- {...(this.state as InjectedProps)}
- />
- );
- }
- }
- return WithRelease;
- }
- export default withRelease;
|