123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- import Reflux from 'reflux';
- import NavigationActions from 'sentry/actions/navigationActions';
- import OrganizationActions from 'sentry/actions/organizationActions';
- import OrganizationsActions from 'sentry/actions/organizationsActions';
- import ProjectActions from 'sentry/actions/projectActions';
- import {Organization, Project} from 'sentry/types';
- type OrgTypes = Organization | null;
- type State = {
- project: Project | null;
- lastProject: Project | null;
- organization: OrgTypes;
- environment: string | string[] | null;
- lastRoute: string | null;
- };
- type LatestContextStoreInterface = {
- state: State;
- reset(): void;
- get(): State;
- onSetLastRoute(route: string): void;
- onUpdateOrganization(organization: OrgTypes): void;
- onSetActiveOrganization(organization: OrgTypes): void;
- onSetActiveProject(project: Project | null): void;
- onUpdateProject(project: Project | null): void;
- };
- /**
- * 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: Reflux.StoreDefinition & LatestContextStoreInterface = {
- state: {
- project: null,
- lastProject: null,
- organization: null,
- environment: null,
- lastRoute: null,
- },
- get() {
- return this.state;
- },
- init() {
- this.reset();
- this.listenTo(ProjectActions.setActive, this.onSetActiveProject);
- this.listenTo(ProjectActions.updateSuccess, this.onUpdateProject);
- this.listenTo(OrganizationsActions.setActive, this.onSetActiveOrganization);
- this.listenTo(OrganizationsActions.update, this.onUpdateOrganization);
- this.listenTo(OrganizationActions.update, this.onUpdateOrganization);
- this.listenTo(NavigationActions.setLastRoute, this.onSetLastRoute);
- },
- reset() {
- this.state = {
- project: null,
- lastProject: null,
- organization: null,
- environment: null,
- lastRoute: null,
- };
- return this.state;
- },
- onSetLastRoute(route) {
- this.state = {
- ...this.state,
- lastRoute: route,
- };
- this.trigger(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: 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 = Reflux.createStore(storeConfig) as Reflux.Store &
- LatestContextStoreInterface;
- export default LatestContextStore;
|