withRelease.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import {Component} from 'react';
  2. import {getProjectRelease, getReleaseDeploys} from 'sentry/actionCreators/release';
  3. import {Client} from 'sentry/api';
  4. import ReleaseStore from 'sentry/stores/releaseStore';
  5. import {Deploy, Organization, Release} from 'sentry/types';
  6. import getDisplayName from 'sentry/utils/getDisplayName';
  7. type DependentProps = {
  8. api: Client;
  9. organization: Organization;
  10. projectSlug: string;
  11. releaseVersion: string;
  12. };
  13. type InjectedProps = {
  14. deploys?: Array<Deploy>;
  15. deploysError?: Error;
  16. deploysLoading?: boolean;
  17. release?: Release;
  18. releaseError?: Error;
  19. releaseLoading?: boolean;
  20. };
  21. function withRelease<P extends DependentProps>(WrappedComponent: React.ComponentType<P>) {
  22. class WithRelease extends Component<
  23. Omit<P, keyof InjectedProps> & Partial<InjectedProps> & DependentProps,
  24. InjectedProps
  25. > {
  26. static displayName = `withRelease(${getDisplayName(WrappedComponent)})`;
  27. constructor(props, context) {
  28. super(props, context);
  29. const {projectSlug, releaseVersion} = this.props;
  30. const releaseData = ReleaseStore.get(projectSlug, releaseVersion);
  31. this.state = {...releaseData};
  32. }
  33. componentDidMount() {
  34. this.fetchRelease();
  35. this.fetchDeploys();
  36. }
  37. componentWillUnmount() {
  38. this.unsubscribe();
  39. }
  40. unsubscribe = ReleaseStore.listen(() => this.onStoreUpdate(), undefined);
  41. fetchRelease() {
  42. const {api, organization, projectSlug, releaseVersion} = this.props;
  43. const releaseData = ReleaseStore.get(projectSlug, releaseVersion);
  44. const orgSlug = organization.slug;
  45. if (
  46. (!releaseData.release && !releaseData.releaseLoading) ||
  47. releaseData.releaseError
  48. ) {
  49. getProjectRelease(api, {orgSlug, projectSlug, releaseVersion});
  50. }
  51. }
  52. fetchDeploys() {
  53. const {api, organization, projectSlug, releaseVersion} = this.props;
  54. const releaseData = ReleaseStore.get(projectSlug, releaseVersion);
  55. const orgSlug = organization.slug;
  56. if (
  57. (!releaseData.deploys && !releaseData.deploysLoading) ||
  58. releaseData.deploysError
  59. ) {
  60. getReleaseDeploys(api, {orgSlug, projectSlug, releaseVersion});
  61. }
  62. }
  63. onStoreUpdate() {
  64. const {projectSlug, releaseVersion} = this.props;
  65. const releaseData = ReleaseStore.get(projectSlug, releaseVersion);
  66. this.setState({...releaseData});
  67. }
  68. render() {
  69. return (
  70. <WrappedComponent
  71. {...(this.props as P & DependentProps)}
  72. {...(this.state as InjectedProps)}
  73. />
  74. );
  75. }
  76. }
  77. return WithRelease;
  78. }
  79. export default withRelease;