latestContextStore.tsx 3.9 KB

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