releaseStore.tsx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import {createStore, StoreDefinition} from 'reflux';
  2. import OrganizationActions from 'sentry/actions/organizationActions';
  3. import ReleaseActions from 'sentry/actions/releaseActions';
  4. import {Deploy, Organization, Release} from 'sentry/types';
  5. import {makeSafeRefluxStore} from 'sentry/utils/makeSafeRefluxStore';
  6. type StoreRelease = Map<string, Release>;
  7. type StoreDeploys = Map<string, Array<Deploy>>;
  8. type StoreLoading = Map<string, boolean>;
  9. type StoreError = Map<string, Error>;
  10. interface ReleaseStoreDefinition extends StoreDefinition {
  11. get(
  12. projectSlug: string,
  13. releaseVersion: string
  14. ): {
  15. deploys: Array<Deploy> | undefined;
  16. deploysError: Error | undefined;
  17. deploysLoading: boolean | undefined;
  18. release: Release | undefined;
  19. releaseError: Error | undefined;
  20. releaseLoading: boolean | undefined;
  21. };
  22. loadDeploys(orgSlug: string, projectSlug: string, releaseVersion: string): void;
  23. loadDeploysError(projectSlug: string, releaseVersion: string, error: Error): void;
  24. loadDeploysSuccess(projectSlug: string, releaseVersion: string, data: Release): void;
  25. loadRelease(orgSlug: string, projectSlug: string, releaseVersion: string): void;
  26. loadReleaseError(projectSlug: string, releaseVersion: string, error: Error): void;
  27. loadReleaseSuccess(projectSlug: string, releaseVersion: string, data: Release): void;
  28. state: {
  29. deploys: StoreDeploys;
  30. deploysError: StoreError;
  31. deploysLoading: StoreLoading;
  32. orgSlug: string | undefined;
  33. release: StoreRelease;
  34. releaseError: StoreError;
  35. releaseLoading: StoreLoading;
  36. };
  37. updateOrganization(org: Organization): void;
  38. }
  39. export const getReleaseStoreKey = (projectSlug: string, releaseVersion: string) =>
  40. `${projectSlug}${releaseVersion}`;
  41. const storeConfig: ReleaseStoreDefinition = {
  42. state: {
  43. orgSlug: undefined,
  44. release: new Map() as StoreRelease,
  45. releaseLoading: new Map() as StoreLoading,
  46. releaseError: new Map() as StoreError,
  47. deploys: new Map() as StoreDeploys,
  48. deploysLoading: new Map() as StoreLoading,
  49. deploysError: new Map() as StoreError,
  50. },
  51. listenables: ReleaseActions,
  52. unsubscribeListeners: [],
  53. init() {
  54. this.unsubscribeListeners.push(
  55. this.listenTo(OrganizationActions.update, this.updateOrganization)
  56. );
  57. this.reset();
  58. },
  59. reset() {
  60. this.state = {
  61. orgSlug: undefined,
  62. release: new Map() as StoreRelease,
  63. releaseLoading: new Map() as StoreLoading,
  64. releaseError: new Map() as StoreError,
  65. deploys: new Map() as StoreDeploys,
  66. deploysLoading: new Map() as StoreLoading,
  67. deploysError: new Map() as StoreError,
  68. };
  69. this.trigger(this.state);
  70. },
  71. updateOrganization(org: Organization) {
  72. this.reset();
  73. this.state.orgSlug = org.slug;
  74. this.trigger(this.state);
  75. },
  76. loadRelease(orgSlug: string, projectSlug: string, releaseVersion: string) {
  77. // Wipe entire store if the user switched organizations
  78. if (!this.orgSlug || this.orgSlug !== orgSlug) {
  79. this.reset();
  80. this.orgSlug = orgSlug;
  81. }
  82. const releaseKey = getReleaseStoreKey(projectSlug, releaseVersion);
  83. const {releaseLoading, releaseError, ...state} = this.state;
  84. this.state = {
  85. ...state,
  86. releaseLoading: {
  87. ...releaseLoading,
  88. [releaseKey]: true,
  89. },
  90. releaseError: {
  91. ...releaseError,
  92. [releaseKey]: undefined,
  93. },
  94. };
  95. this.trigger(this.state);
  96. },
  97. loadReleaseError(projectSlug: string, releaseVersion: string, error: Error) {
  98. const releaseKey = getReleaseStoreKey(projectSlug, releaseVersion);
  99. const {releaseLoading, releaseError, ...state} = this.state;
  100. this.state = {
  101. ...state,
  102. releaseLoading: {
  103. ...releaseLoading,
  104. [releaseKey]: false,
  105. },
  106. releaseError: {
  107. ...releaseError,
  108. [releaseKey]: error,
  109. },
  110. };
  111. this.trigger(this.state);
  112. },
  113. loadReleaseSuccess(projectSlug: string, releaseVersion: string, data: Release) {
  114. const releaseKey = getReleaseStoreKey(projectSlug, releaseVersion);
  115. const {release, releaseLoading, releaseError, ...state} = this.state;
  116. this.state = {
  117. ...state,
  118. release: {
  119. ...release,
  120. [releaseKey]: data,
  121. },
  122. releaseLoading: {
  123. ...releaseLoading,
  124. [releaseKey]: false,
  125. },
  126. releaseError: {
  127. ...releaseError,
  128. [releaseKey]: undefined,
  129. },
  130. };
  131. this.trigger(this.state);
  132. },
  133. loadDeploys(orgSlug: string, projectSlug: string, releaseVersion: string) {
  134. // Wipe entire store if the user switched organizations
  135. if (!this.orgSlug || this.orgSlug !== orgSlug) {
  136. this.reset();
  137. this.orgSlug = orgSlug;
  138. }
  139. const releaseKey = getReleaseStoreKey(projectSlug, releaseVersion);
  140. const {deploysLoading, deploysError, ...state} = this.state;
  141. this.state = {
  142. ...state,
  143. deploysLoading: {
  144. ...deploysLoading,
  145. [releaseKey]: true,
  146. },
  147. deploysError: {
  148. ...deploysError,
  149. [releaseKey]: undefined,
  150. },
  151. };
  152. this.trigger(this.state);
  153. },
  154. loadDeploysError(projectSlug: string, releaseVersion: string, error: Error) {
  155. const releaseKey = getReleaseStoreKey(projectSlug, releaseVersion);
  156. const {deploysLoading, deploysError, ...state} = this.state;
  157. this.state = {
  158. ...state,
  159. deploysLoading: {
  160. ...deploysLoading,
  161. [releaseKey]: false,
  162. },
  163. deploysError: {
  164. ...deploysError,
  165. [releaseKey]: error,
  166. },
  167. };
  168. this.trigger(this.state);
  169. },
  170. loadDeploysSuccess(projectSlug: string, releaseVersion: string, data: Release) {
  171. const releaseKey = getReleaseStoreKey(projectSlug, releaseVersion);
  172. const {deploys, deploysLoading, deploysError, ...state} = this.state;
  173. this.state = {
  174. ...state,
  175. deploys: {
  176. ...deploys,
  177. [releaseKey]: data,
  178. },
  179. deploysLoading: {
  180. ...deploysLoading,
  181. [releaseKey]: false,
  182. },
  183. deploysError: {
  184. ...deploysError,
  185. [releaseKey]: undefined,
  186. },
  187. };
  188. this.trigger(this.state);
  189. },
  190. get(projectSlug: string, releaseVersion: string) {
  191. const releaseKey = getReleaseStoreKey(projectSlug, releaseVersion);
  192. return {
  193. release: this.state.release[releaseKey],
  194. releaseLoading: this.state.releaseLoading[releaseKey],
  195. releaseError: this.state.releaseError[releaseKey],
  196. deploys: this.state.deploys[releaseKey],
  197. deploysLoading: this.state.deploysLoading[releaseKey],
  198. deploysError: this.state.deploysError[releaseKey],
  199. };
  200. },
  201. };
  202. const ReleaseStore = createStore(makeSafeRefluxStore(storeConfig));
  203. export default ReleaseStore;