latestContextStore.tsx 3.8 KB

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