pluginsStore.tsx 2.8 KB

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