releaseStore.tsx 6.3 KB

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