releaseStore.tsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. 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) {
  72. this.reset();
  73. this.state.orgSlug = org.slug;
  74. this.trigger(this.state);
  75. },
  76. loadRelease(orgSlug, projectSlug, releaseVersion) {
  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, releaseVersion, 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, releaseVersion, data) {
  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, projectSlug, releaseVersion) {
  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, releaseVersion, 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, releaseVersion, data) {
  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, releaseVersion) {
  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(storeConfig);
  203. export default ReleaseStore;