sidebarPanelStore.tsx 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import {createStore, Store, StoreDefinition} from 'reflux';
  2. import SidebarPanelActions from 'sentry/actions/sidebarPanelActions';
  3. import {SidebarPanelKey} from 'sentry/components/sidebar/types';
  4. import {makeSafeRefluxStore, SafeStoreDefinition} from 'sentry/utils/makeSafeRefluxStore';
  5. import {CommonStoreInterface} from './types';
  6. type ActivePanelType = SidebarPanelKey | '';
  7. type SidebarPanelStoreInterface = CommonStoreInterface<ActivePanelType> & {
  8. activePanel: ActivePanelType;
  9. onActivatePanel(panel: SidebarPanelKey): void;
  10. onHidePanel(): void;
  11. onTogglePanel(panel: SidebarPanelKey): void;
  12. };
  13. const storeConfig: StoreDefinition & SidebarPanelStoreInterface & SafeStoreDefinition = {
  14. activePanel: '',
  15. unsubscribeListeners: [],
  16. init() {
  17. this.unsubscribeListeners.push(
  18. this.listenTo(SidebarPanelActions.activatePanel, this.onActivatePanel)
  19. );
  20. this.unsubscribeListeners.push(
  21. this.listenTo(SidebarPanelActions.hidePanel, this.onHidePanel)
  22. );
  23. this.unsubscribeListeners.push(
  24. this.listenTo(SidebarPanelActions.togglePanel, this.onTogglePanel)
  25. );
  26. },
  27. onActivatePanel(panel: SidebarPanelKey) {
  28. this.activePanel = panel;
  29. this.trigger(this.activePanel);
  30. },
  31. onTogglePanel(panel: SidebarPanelKey) {
  32. if (this.activePanel === panel) {
  33. this.onHidePanel();
  34. } else {
  35. this.onActivatePanel(panel);
  36. }
  37. },
  38. onHidePanel() {
  39. this.activePanel = '';
  40. this.trigger(this.activePanel);
  41. },
  42. getState() {
  43. return this.activePanel;
  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 SidebarPanelStore = createStore(makeSafeRefluxStore(storeConfig)) as Store &
  51. SidebarPanelStoreInterface;
  52. export default SidebarPanelStore;