sentryAppComponentsStore.tsx 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import {createStore, StoreDefinition} from 'reflux';
  2. import {SentryAppComponent} from 'sentry/types';
  3. import {makeSafeRefluxStore} from 'sentry/utils/makeSafeRefluxStore';
  4. export interface SentryAppComponentsStoreDefinition extends StoreDefinition {
  5. get: (uuid: string) => SentryAppComponent | undefined;
  6. getAll: () => SentryAppComponent[];
  7. getComponentByType: (type: string | undefined) => SentryAppComponent[];
  8. getInitialState: () => SentryAppComponent[];
  9. loadComponents: (items: SentryAppComponent[]) => void;
  10. }
  11. const storeConfig: SentryAppComponentsStoreDefinition = {
  12. unsubscribeListeners: [],
  13. items: [],
  14. init() {
  15. this.items = [];
  16. },
  17. getInitialState() {
  18. return this.items;
  19. },
  20. loadComponents(items: SentryAppComponent[]) {
  21. this.items = items;
  22. this.trigger(items);
  23. },
  24. get(uuid: string) {
  25. const items: SentryAppComponent[] = this.items;
  26. return items.find(item => item.uuid === uuid);
  27. },
  28. getAll() {
  29. return this.items;
  30. },
  31. getComponentByType(type: string | undefined) {
  32. if (!type) {
  33. return this.getAll();
  34. }
  35. const items: SentryAppComponent[] = this.items;
  36. return items.filter(item => item.type === type);
  37. },
  38. };
  39. const SentryAppComponentsStore = createStore(makeSafeRefluxStore(storeConfig));
  40. export default SentryAppComponentsStore;