releaseStore.tsx 6.5 KB

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