latestContextStore.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import Reflux 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. type OrgTypes = Organization | null;
  7. type State = {
  8. project: Project | null;
  9. lastProject: Project | null;
  10. organization: OrgTypes;
  11. environment: string | string[] | null;
  12. };
  13. type LatestContextStoreInterface = {
  14. state: State;
  15. reset(): void;
  16. get(): State;
  17. onUpdateOrganization(organization: OrgTypes): void;
  18. onSetActiveOrganization(organization: OrgTypes): void;
  19. onSetActiveProject(project: Project | null): void;
  20. onUpdateProject(project: Project | null): void;
  21. };
  22. /**
  23. * Keeps track of last usable project/org this currently won't track when users
  24. * navigate out of a org/project completely, it tracks only if a user switches
  25. * into a new org/project.
  26. *
  27. * Only keep slug so that people don't get the idea to access org/project data
  28. * here Org/project data is currently in organizationsStore/projectsStore
  29. */
  30. const storeConfig: Reflux.StoreDefinition & LatestContextStoreInterface = {
  31. state: {
  32. project: null,
  33. lastProject: null,
  34. organization: null,
  35. environment: null,
  36. },
  37. get() {
  38. return this.state;
  39. },
  40. init() {
  41. this.reset();
  42. this.listenTo(ProjectActions.setActive, this.onSetActiveProject);
  43. this.listenTo(ProjectActions.updateSuccess, this.onUpdateProject);
  44. this.listenTo(OrganizationsActions.setActive, this.onSetActiveOrganization);
  45. this.listenTo(OrganizationsActions.update, this.onUpdateOrganization);
  46. this.listenTo(OrganizationActions.update, this.onUpdateOrganization);
  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: 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 = Reflux.createStore(storeConfig) as Reflux.Store &
  118. LatestContextStoreInterface;
  119. export default LatestContextStore;