latestContextStore.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import type {StoreDefinition} from 'reflux';
  2. import {createStore} from 'reflux';
  3. import type {Organization, Project} from 'sentry/types';
  4. type State = {
  5. environment: string | string[] | null;
  6. lastProject: Project | null;
  7. organization: Organization | null;
  8. project: Project | null;
  9. };
  10. interface LatestContextStoreDefinition extends StoreDefinition {
  11. get(): State;
  12. onSetActiveOrganization(organization: Organization): void;
  13. onSetActiveProject(project: Project | null): void;
  14. onUpdateOrganization(organization: Partial<Organization>): void;
  15. onUpdateProject(project: Project | null): void;
  16. reset(): void;
  17. state: State;
  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. };
  111. const LatestContextStore = createStore(storeConfig);
  112. export default LatestContextStore;