releaseStore.tsx 6.4 KB

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