latestContextStore.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import {createStore, StoreDefinition} from 'reflux';
  2. import OrganizationActions from 'sentry/actions/organizationActions';
  3. import ProjectActions from 'sentry/actions/projectActions';
  4. import {Organization, Project} from 'sentry/types';
  5. import {makeSafeRefluxStore} from 'sentry/utils/makeSafeRefluxStore';
  6. type State = {
  7. environment: string | string[] | null;
  8. lastProject: Project | null;
  9. organization: Organization | null;
  10. project: Project | null;
  11. };
  12. interface LatestContextStoreDefinition extends StoreDefinition {
  13. get(): State;
  14. onSetActiveOrganization(organization: Organization): void;
  15. onSetActiveProject(project: Project | null): void;
  16. onUpdateOrganization(organization: Partial<Organization>): void;
  17. onUpdateProject(project: Project | null): void;
  18. reset(): void;
  19. state: State;
  20. }
  21. /**
  22. * Keeps track of last usable project/org this currently won't track when users
  23. * navigate out of a org/project completely, it tracks only if a user switches
  24. * into a new org/project.
  25. *
  26. * Only keep slug so that people don't get the idea to access org/project data
  27. * here Org/project data is currently in organizationsStore/projectsStore
  28. */
  29. const storeConfig: LatestContextStoreDefinition = {
  30. unsubscribeListeners: [],
  31. state: {
  32. project: null,
  33. lastProject: null,
  34. organization: null,
  35. environment: null,
  36. },
  37. get() {
  38. return this.state;
  39. },
  40. init() {
  41. this.reset();
  42. this.unsubscribeListeners.push(
  43. this.listenTo(ProjectActions.setActive, this.onSetActiveProject)
  44. );
  45. this.unsubscribeListeners.push(
  46. this.listenTo(ProjectActions.updateSuccess, this.onUpdateProject)
  47. );
  48. this.unsubscribeListeners.push(
  49. this.listenTo(OrganizationActions.update, this.onUpdateOrganization)
  50. );
  51. },
  52. reset() {
  53. this.state = {
  54. project: null,
  55. lastProject: null,
  56. organization: null,
  57. environment: null,
  58. };
  59. return this.state;
  60. },
  61. onUpdateOrganization(org) {
  62. // Don't do anything if base/target orgs are falsey
  63. if (!this.state.organization) {
  64. return;
  65. }
  66. if (!org) {
  67. return;
  68. }
  69. // Check to make sure current active org is what has been updated
  70. if (org.slug !== this.state.organization.slug) {
  71. return;
  72. }
  73. this.state = {
  74. ...this.state,
  75. organization: {...this.state.organization, ...org},
  76. };
  77. this.trigger(this.state);
  78. },
  79. onSetActiveOrganization(org) {
  80. if (!org) {
  81. this.state = {
  82. ...this.state,
  83. organization: null,
  84. project: null,
  85. };
  86. } else if (!this.state.organization || this.state.organization.slug !== org.slug) {
  87. // Update only if different
  88. this.state = {
  89. ...this.state,
  90. organization: org,
  91. project: null,
  92. };
  93. }
  94. this.trigger(this.state);
  95. },
  96. onSetActiveProject(project) {
  97. if (!project) {
  98. this.state = {
  99. ...this.state,
  100. lastProject: this.state.project,
  101. project: null,
  102. };
  103. } else if (!this.state.project || this.state.project.slug !== project.slug) {
  104. // Update only if different
  105. this.state = {
  106. ...this.state,
  107. lastProject: this.state.project,
  108. project,
  109. };
  110. }
  111. this.trigger(this.state);
  112. },
  113. onUpdateProject(project) {
  114. this.state = {
  115. ...this.state,
  116. project,
  117. };
  118. this.trigger(this.state);
  119. },
  120. };
  121. const LatestContextStore = createStore(makeSafeRefluxStore(storeConfig));
  122. export default LatestContextStore;