latestContextStore.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import type {StoreDefinition} from 'reflux';
  2. import {createStore} from 'reflux';
  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 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. state: {
  30. project: null,
  31. lastProject: null,
  32. organization: null,
  33. environment: null,
  34. },
  35. get() {
  36. return this.state;
  37. },
  38. init() {
  39. // XXX: Do not use `this.listenTo` in this store. We avoid usage of reflux
  40. // listeners due to their leaky nature in tests.
  41. this.reset();
  42. },
  43. reset() {
  44. this.state = {
  45. project: null,
  46. lastProject: null,
  47. organization: null,
  48. environment: null,
  49. };
  50. return this.state;
  51. },
  52. onUpdateOrganization(org) {
  53. // Don't do anything if base/target orgs are falsey
  54. if (!this.state.organization) {
  55. return;
  56. }
  57. if (!org) {
  58. return;
  59. }
  60. // Check to make sure current active org is what has been updated
  61. if (org.slug !== this.state.organization.slug) {
  62. return;
  63. }
  64. this.state = {
  65. ...this.state,
  66. organization: {...this.state.organization, ...org},
  67. };
  68. this.trigger(this.state);
  69. },
  70. onSetActiveOrganization(org) {
  71. if (!org) {
  72. this.state = {
  73. ...this.state,
  74. organization: null,
  75. project: null,
  76. };
  77. } else if (!this.state.organization || this.state.organization.slug !== org.slug) {
  78. // Update only if different
  79. this.state = {
  80. ...this.state,
  81. organization: org,
  82. project: null,
  83. };
  84. }
  85. this.trigger(this.state);
  86. },
  87. onSetActiveProject(project) {
  88. if (!project) {
  89. this.state = {
  90. ...this.state,
  91. lastProject: this.state.project,
  92. project: null,
  93. };
  94. } else if (!this.state.project || this.state.project.slug !== project.slug) {
  95. // Update only if different
  96. this.state = {
  97. ...this.state,
  98. lastProject: this.state.project,
  99. project,
  100. };
  101. }
  102. this.trigger(this.state);
  103. },
  104. onUpdateProject(project) {
  105. this.state = {
  106. ...this.state,
  107. project,
  108. };
  109. this.trigger(this.state);
  110. },
  111. };
  112. const LatestContextStore = createStore(storeConfig);
  113. export default LatestContextStore;