123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- import type {StoreDefinition} from 'reflux';
- import {createStore} from 'reflux';
- import type {Organization} from 'sentry/types/organization';
- import type {Project} from 'sentry/types/project';
- type State = {
- environment: string | string[] | null;
- lastProject: Project | null;
- organization: Organization | null;
- project: Project | null;
- };
- interface LatestContextStoreDefinition extends StoreDefinition {
- get(): State;
- onSetActiveOrganization(organization: Organization): void;
- onSetActiveProject(project: Project | null): void;
- onUpdateOrganization(organization: Partial<Organization>): void;
- onUpdateProject(project: Project | null): void;
- reset(): void;
- state: State;
- }
- /**
- * Keeps track of last usable project/org this currently won't track when users
- * navigate out of a org/project completely, it tracks only if a user switches
- * into a new org/project.
- *
- * Only keep slug so that people don't get the idea to access org/project data
- * here Org/project data is currently in organizationsStore/projectsStore
- */
- const storeConfig: LatestContextStoreDefinition = {
- state: {
- project: null,
- lastProject: null,
- organization: null,
- environment: null,
- },
- get() {
- return this.state;
- },
- init() {
- // XXX: Do not use `this.listenTo` in this store. We avoid usage of reflux
- // listeners due to their leaky nature in tests.
- this.reset();
- },
- reset() {
- this.state = {
- project: null,
- lastProject: null,
- organization: null,
- environment: null,
- };
- return this.state;
- },
- onUpdateOrganization(org) {
- // Don't do anything if base/target orgs are falsey
- if (!this.state.organization) {
- return;
- }
- if (!org) {
- return;
- }
- // Check to make sure current active org is what has been updated
- if (org.slug !== this.state.organization.slug) {
- return;
- }
- this.state = {
- ...this.state,
- organization: {...this.state.organization, ...org},
- };
- this.trigger(this.state);
- },
- onSetActiveOrganization(org) {
- if (!org) {
- this.state = {
- ...this.state,
- organization: null,
- project: null,
- };
- } else if (!this.state.organization || this.state.organization.slug !== org.slug) {
- // Update only if different
- this.state = {
- ...this.state,
- organization: org,
- project: null,
- };
- }
- this.trigger(this.state);
- },
- onSetActiveProject(project) {
- if (!project) {
- this.state = {
- ...this.state,
- lastProject: this.state.project,
- project: null,
- };
- } else if (!this.state.project || this.state.project.slug !== project.slug) {
- // Update only if different
- this.state = {
- ...this.state,
- lastProject: this.state.project,
- project,
- };
- }
- this.trigger(this.state);
- },
- onUpdateProject(project) {
- this.state = {
- ...this.state,
- project,
- };
- this.trigger(this.state);
- },
- };
- const LatestContextStore = createStore(storeConfig);
- export default LatestContextStore;
|