latestContextStore.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import {createStore} from 'reflux';
  2. import type {StrictStoreDefinition} from 'sentry/stores/types';
  3. import type {Organization} from 'sentry/types/organization';
  4. import type {Project} from 'sentry/types/project';
  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 StrictStoreDefinition<State> {
  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. }
  19. /**
  20. * Keeps track of last usable project/org this currently won't track when users
  21. * navigate out of a org/project completely, it tracks only if a user switches
  22. * into a new org/project.
  23. *
  24. * Only keep slug so that people don't get the idea to access org/project data
  25. * here Org/project data is currently in organizationsStore/projectsStore
  26. */
  27. const storeConfig: LatestContextStoreDefinition = {
  28. state: {
  29. project: null,
  30. lastProject: null,
  31. organization: null,
  32. environment: null,
  33. },
  34. get() {
  35. return this.state;
  36. },
  37. init() {
  38. // XXX: Do not use `this.listenTo` in this store. We avoid usage of reflux
  39. // listeners due to their leaky nature in tests.
  40. this.reset();
  41. },
  42. reset() {
  43. this.state = {
  44. project: null,
  45. lastProject: null,
  46. organization: null,
  47. environment: null,
  48. };
  49. return this.state;
  50. },
  51. onUpdateOrganization(org) {
  52. // Don't do anything if base/target orgs are falsey
  53. if (!this.state.organization) {
  54. return;
  55. }
  56. if (!org) {
  57. return;
  58. }
  59. // Check to make sure current active org is what has been updated
  60. if (org.slug !== this.state.organization.slug) {
  61. return;
  62. }
  63. this.state = {
  64. ...this.state,
  65. organization: {...this.state.organization, ...org},
  66. };
  67. this.trigger(this.state);
  68. },
  69. onSetActiveOrganization(org) {
  70. if (!org) {
  71. this.state = {
  72. ...this.state,
  73. organization: null,
  74. project: null,
  75. };
  76. } else if (!this.state.organization || this.state.organization.slug !== org.slug) {
  77. // Update only if different
  78. this.state = {
  79. ...this.state,
  80. organization: org,
  81. project: null,
  82. };
  83. }
  84. this.trigger(this.state);
  85. },
  86. onSetActiveProject(project) {
  87. if (!project) {
  88. this.state = {
  89. ...this.state,
  90. lastProject: this.state.project,
  91. project: null,
  92. };
  93. } else if (!this.state.project || this.state.project.slug !== project.slug) {
  94. // Update only if different
  95. this.state = {
  96. ...this.state,
  97. lastProject: this.state.project,
  98. project,
  99. };
  100. }
  101. this.trigger(this.state);
  102. },
  103. onUpdateProject(project) {
  104. this.state = {
  105. ...this.state,
  106. project,
  107. };
  108. this.trigger(this.state);
  109. },
  110. getState() {
  111. return this.state;
  112. },
  113. };
  114. const LatestContextStore = createStore(storeConfig);
  115. export default LatestContextStore;