releaseStore.tsx 6.2 KB

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