latestContextStore.tsx 3.8 KB

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