latestContextStore.tsx 2.8 KB

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