123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- import {createStore, StoreDefinition} from 'reflux';
- import OrganizationActions from 'sentry/actions/organizationActions';
- import {Deploy, Organization, Release} from 'sentry/types';
- import {makeSafeRefluxStore} from 'sentry/utils/makeSafeRefluxStore';
- type StoreRelease = Map<string, Release>;
- type StoreDeploys = Map<string, Array<Deploy>>;
- type StoreLoading = Map<string, boolean>;
- type StoreError = Map<string, Error>;
- interface ReleaseStoreDefinition extends StoreDefinition {
- get(
- projectSlug: string,
- releaseVersion: string
- ): {
- deploys: Array<Deploy> | undefined;
- deploysError: Error | undefined;
- deploysLoading: boolean | undefined;
- release: Release | undefined;
- releaseError: Error | undefined;
- releaseLoading: boolean | undefined;
- };
- loadDeploys(orgSlug: string, projectSlug: string, releaseVersion: string): void;
- loadDeploysError(projectSlug: string, releaseVersion: string, error: Error): void;
- loadDeploysSuccess(
- projectSlug: string,
- releaseVersion: string,
- data: Deploy[] | null
- ): void;
- loadRelease(orgSlug: string, projectSlug: string, releaseVersion: string): void;
- loadReleaseError(projectSlug: string, releaseVersion: string, error: Error): void;
- loadReleaseSuccess(
- projectSlug: string,
- releaseVersion: string,
- data: Release | null
- ): void;
- state: {
- deploys: StoreDeploys;
- deploysError: StoreError;
- deploysLoading: StoreLoading;
- orgSlug: string | undefined;
- release: StoreRelease;
- releaseError: StoreError;
- releaseLoading: StoreLoading;
- };
- updateOrganization(org: Organization): void;
- }
- export const getReleaseStoreKey = (projectSlug: string, releaseVersion: string) =>
- `${projectSlug}${releaseVersion}`;
- const storeConfig: ReleaseStoreDefinition = {
- state: {
- orgSlug: undefined,
- release: new Map() as StoreRelease,
- releaseLoading: new Map() as StoreLoading,
- releaseError: new Map() as StoreError,
- deploys: new Map() as StoreDeploys,
- deploysLoading: new Map() as StoreLoading,
- deploysError: new Map() as StoreError,
- },
- unsubscribeListeners: [],
- init() {
- this.unsubscribeListeners.push(
- this.listenTo(OrganizationActions.update, this.updateOrganization)
- );
- this.reset();
- },
- reset() {
- this.state = {
- orgSlug: undefined,
- release: new Map() as StoreRelease,
- releaseLoading: new Map() as StoreLoading,
- releaseError: new Map() as StoreError,
- deploys: new Map() as StoreDeploys,
- deploysLoading: new Map() as StoreLoading,
- deploysError: new Map() as StoreError,
- };
- this.trigger(this.state);
- },
- updateOrganization(org) {
- this.reset();
- this.state.orgSlug = org.slug;
- this.trigger(this.state);
- },
- loadRelease(orgSlug, projectSlug, releaseVersion) {
- // Wipe entire store if the user switched organizations
- if (!this.orgSlug || this.orgSlug !== orgSlug) {
- this.reset();
- this.orgSlug = orgSlug;
- }
- const releaseKey = getReleaseStoreKey(projectSlug, releaseVersion);
- const {releaseLoading, releaseError, ...state} = this.state;
- this.state = {
- ...state,
- releaseLoading: {
- ...releaseLoading,
- [releaseKey]: true,
- },
- releaseError: {
- ...releaseError,
- [releaseKey]: undefined,
- },
- };
- this.trigger(this.state);
- },
- loadReleaseError(projectSlug, releaseVersion, error) {
- const releaseKey = getReleaseStoreKey(projectSlug, releaseVersion);
- const {releaseLoading, releaseError, ...state} = this.state;
- this.state = {
- ...state,
- releaseLoading: {
- ...releaseLoading,
- [releaseKey]: false,
- },
- releaseError: {
- ...releaseError,
- [releaseKey]: error,
- },
- };
- this.trigger(this.state);
- },
- loadReleaseSuccess(projectSlug, releaseVersion, data) {
- const releaseKey = getReleaseStoreKey(projectSlug, releaseVersion);
- const {release, releaseLoading, releaseError, ...state} = this.state;
- this.state = {
- ...state,
- release: {
- ...release,
- [releaseKey]: data,
- },
- releaseLoading: {
- ...releaseLoading,
- [releaseKey]: false,
- },
- releaseError: {
- ...releaseError,
- [releaseKey]: undefined,
- },
- };
- this.trigger(this.state);
- },
- loadDeploys(orgSlug, projectSlug, releaseVersion) {
- // Wipe entire store if the user switched organizations
- if (!this.orgSlug || this.orgSlug !== orgSlug) {
- this.reset();
- this.orgSlug = orgSlug;
- }
- const releaseKey = getReleaseStoreKey(projectSlug, releaseVersion);
- const {deploysLoading, deploysError, ...state} = this.state;
- this.state = {
- ...state,
- deploysLoading: {
- ...deploysLoading,
- [releaseKey]: true,
- },
- deploysError: {
- ...deploysError,
- [releaseKey]: undefined,
- },
- };
- this.trigger(this.state);
- },
- loadDeploysError(projectSlug, releaseVersion, error) {
- const releaseKey = getReleaseStoreKey(projectSlug, releaseVersion);
- const {deploysLoading, deploysError, ...state} = this.state;
- this.state = {
- ...state,
- deploysLoading: {
- ...deploysLoading,
- [releaseKey]: false,
- },
- deploysError: {
- ...deploysError,
- [releaseKey]: error,
- },
- };
- this.trigger(this.state);
- },
- loadDeploysSuccess(projectSlug, releaseVersion, data) {
- const releaseKey = getReleaseStoreKey(projectSlug, releaseVersion);
- const {deploys, deploysLoading, deploysError, ...state} = this.state;
- this.state = {
- ...state,
- deploys: {
- ...deploys,
- [releaseKey]: data,
- },
- deploysLoading: {
- ...deploysLoading,
- [releaseKey]: false,
- },
- deploysError: {
- ...deploysError,
- [releaseKey]: undefined,
- },
- };
- this.trigger(this.state);
- },
- get(projectSlug, releaseVersion) {
- const releaseKey = getReleaseStoreKey(projectSlug, releaseVersion);
- return {
- release: this.state.release[releaseKey],
- releaseLoading: this.state.releaseLoading[releaseKey],
- releaseError: this.state.releaseError[releaseKey],
- deploys: this.state.deploys[releaseKey],
- deploysLoading: this.state.deploysLoading[releaseKey],
- deploysError: this.state.deploysError[releaseKey],
- };
- },
- };
- const ReleaseStore = createStore(makeSafeRefluxStore(storeConfig));
- export default ReleaseStore;
|