preferencesStore.tsx 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import Reflux from 'reflux';
  2. import PreferencesActions from 'sentry/actions/preferencesActions';
  3. import {CommonStoreInterface} from './types';
  4. type Preferences = {
  5. /**
  6. * Is the sidebar collapsed to the side
  7. */
  8. collapsed?: boolean;
  9. };
  10. type PreferenceStoreInterface = CommonStoreInterface<Preferences> & {
  11. getInitialState(): Preferences;
  12. loadInitialState(prefs: Preferences): void;
  13. prefs: Preferences;
  14. reset(): void;
  15. };
  16. const storeConfig: Reflux.StoreDefinition & PreferenceStoreInterface = {
  17. prefs: {},
  18. init() {
  19. this.reset();
  20. this.listenTo(PreferencesActions.hideSidebar, this.onHideSidebar);
  21. this.listenTo(PreferencesActions.showSidebar, this.onShowSidebar);
  22. this.listenTo(PreferencesActions.loadInitialState, this.loadInitialState);
  23. },
  24. getInitialState() {
  25. return this.prefs;
  26. },
  27. reset() {
  28. this.prefs = {collapsed: false};
  29. },
  30. loadInitialState(prefs: Preferences) {
  31. this.prefs = {...prefs};
  32. this.trigger(this.prefs);
  33. },
  34. onHideSidebar() {
  35. this.prefs = {...this.prefs, collapsed: true};
  36. this.trigger(this.prefs);
  37. },
  38. onShowSidebar() {
  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 = Reflux.createStore(storeConfig) as Reflux.Store &
  51. PreferenceStoreInterface;
  52. export default PreferenceStore;