latestContextStore.tsx 3.7 KB

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