latestContextStore.tsx 3.2 KB

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