preferencesStore.tsx 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import {createStore} from 'reflux';
  2. import {makeSafeRefluxStore} from 'sentry/utils/makeSafeRefluxStore';
  3. import {CommonStoreDefinition} from './types';
  4. type Preferences = {
  5. /**
  6. * Is the sidebar collapsed to the side
  7. */
  8. collapsed?: boolean;
  9. };
  10. interface PreferenceStoreDefinition extends CommonStoreDefinition<Preferences> {
  11. getInitialState(): Preferences;
  12. hideSidebar(): void;
  13. loadInitialState(prefs: Preferences): void;
  14. prefs: Preferences;
  15. reset(): void;
  16. showSidebar(): void;
  17. }
  18. const storeConfig: PreferenceStoreDefinition = {
  19. prefs: {},
  20. unsubscribeListeners: [],
  21. init() {
  22. this.reset();
  23. },
  24. getInitialState() {
  25. return this.prefs;
  26. },
  27. reset() {
  28. this.prefs = {collapsed: false};
  29. },
  30. loadInitialState(prefs) {
  31. this.prefs = {...prefs};
  32. this.trigger(this.prefs);
  33. },
  34. hideSidebar() {
  35. this.prefs = {...this.prefs, collapsed: true};
  36. this.trigger(this.prefs);
  37. },
  38. showSidebar() {
  39. this.prefs = {...this.prefs, collapsed: false};
  40. this.trigger(this.prefs);
  41. },
  42. getState() {
  43. return this.prefs;
  44. },
  45. };
  46. /**
  47. * This store is used to hold local user preferences
  48. * Side-effects (like reading/writing to cookies) are done in associated actionCreators
  49. */
  50. const PreferenceStore = createStore(makeSafeRefluxStore(storeConfig));
  51. export default PreferenceStore;