sidebarPanelStore.tsx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import {createStore} from 'reflux';
  2. import SidebarPanelActions from 'sentry/actions/sidebarPanelActions';
  3. import {SidebarPanelKey} from 'sentry/components/sidebar/types';
  4. import {makeSafeRefluxStore} from 'sentry/utils/makeSafeRefluxStore';
  5. import {CommonStoreDefinition} from './types';
  6. type ActivePanelType = SidebarPanelKey | '';
  7. interface SidebarPanelStoreDefinition extends CommonStoreDefinition<ActivePanelType> {
  8. activePanel: ActivePanelType;
  9. onActivatePanel(panel: SidebarPanelKey): void;
  10. onHidePanel(): void;
  11. onTogglePanel(panel: SidebarPanelKey): void;
  12. }
  13. const storeConfig: SidebarPanelStoreDefinition = {
  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));
  51. export default SidebarPanelStore;