releaseStore.tsx 6.2 KB

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