pluginsStore.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import {createStore, StoreDefinition} from 'reflux';
  2. import PluginActions from 'sentry/actions/pluginActions';
  3. import {Plugin} from 'sentry/types';
  4. import {makeSafeRefluxStore} from 'sentry/utils/makeSafeRefluxStore';
  5. interface PluginStoreDefinition extends StoreDefinition {
  6. plugins: Map<string, Plugin> | null;
  7. state: {
  8. error: Error | null;
  9. loading: boolean;
  10. pageLinks: string | null;
  11. plugins: Plugin[];
  12. };
  13. updating: Map<string, Plugin>;
  14. }
  15. const defaultState = {
  16. loading: true,
  17. plugins: [],
  18. error: null,
  19. pageLinks: null,
  20. };
  21. const storeConfig: PluginStoreDefinition = {
  22. plugins: null,
  23. state: {...defaultState},
  24. updating: new Map(),
  25. unsubscribeListeners: [],
  26. reset() {
  27. // reset our state
  28. this.plugins = null;
  29. this.state = {...defaultState};
  30. this.updating = new Map();
  31. return this.state;
  32. },
  33. getInitialState() {
  34. return this.getState();
  35. },
  36. getState() {
  37. const {plugins: _plugins, ...state} = this.state;
  38. return {
  39. ...state,
  40. plugins: this.plugins ? Array.from(this.plugins.values()) : [],
  41. };
  42. },
  43. init() {
  44. this.reset();
  45. this.unsubscribeListeners.push(
  46. this.listenTo(PluginActions.fetchAll, this.onFetchAll)
  47. );
  48. this.unsubscribeListeners.push(
  49. this.listenTo(PluginActions.fetchAllSuccess, this.onFetchAllSuccess)
  50. );
  51. this.unsubscribeListeners.push(
  52. this.listenTo(PluginActions.fetchAllError, this.onFetchAllError)
  53. );
  54. this.unsubscribeListeners.push(this.listenTo(PluginActions.update, this.onUpdate));
  55. this.unsubscribeListeners.push(
  56. this.listenTo(PluginActions.updateSuccess, this.onUpdateSuccess)
  57. );
  58. this.unsubscribeListeners.push(
  59. this.listenTo(PluginActions.updateError, this.onUpdateError)
  60. );
  61. },
  62. triggerState() {
  63. this.trigger(this.getState());
  64. },
  65. onFetchAll({resetLoading}: {resetLoading?: boolean} = {}) {
  66. if (resetLoading) {
  67. this.state.loading = true;
  68. this.state.error = null;
  69. this.plugins = null;
  70. }
  71. this.triggerState();
  72. },
  73. onFetchAllSuccess(data: Plugin[], {pageLinks}: {pageLinks?: string}) {
  74. this.plugins = new Map(data.map(plugin => [plugin.id, plugin]));
  75. this.state.pageLinks = pageLinks || null;
  76. this.state.loading = false;
  77. this.triggerState();
  78. },
  79. onFetchAllError(err) {
  80. this.plugins = null;
  81. this.state.loading = false;
  82. this.state.error = err;
  83. this.triggerState();
  84. },
  85. onUpdate(id: string, updateObj: Partial<Plugin>) {
  86. if (!this.plugins) {
  87. return;
  88. }
  89. const plugin = this.plugins.get(id);
  90. if (!plugin) {
  91. return;
  92. }
  93. const newPlugin = {
  94. ...plugin,
  95. ...updateObj,
  96. };
  97. this.plugins.set(id, newPlugin);
  98. this.updating.set(id, plugin);
  99. this.triggerState();
  100. },
  101. onUpdateSuccess(id: string, _updateObj: Partial<Plugin>) {
  102. this.updating.delete(id);
  103. },
  104. onUpdateError(id: string, _updateObj: Partial<Plugin>, err) {
  105. const origPlugin = this.updating.get(id);
  106. if (!origPlugin || !this.plugins) {
  107. return;
  108. }
  109. this.plugins.set(id, origPlugin);
  110. this.updating.delete(id);
  111. this.state.error = err;
  112. this.triggerState();
  113. },
  114. };
  115. const PluginStore = createStore(makeSafeRefluxStore(storeConfig));
  116. export default PluginStore;