sidebarPanelStore.tsx 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import {createStore} from 'reflux';
  2. import type {SidebarPanelKey} from 'sentry/components/sidebar/types';
  3. import type {CommonStoreDefinition} from './types';
  4. type ActivePanelType = SidebarPanelKey | '';
  5. interface SidebarPanelStoreDefinition extends CommonStoreDefinition<ActivePanelType> {
  6. activatePanel(panel: SidebarPanelKey): void;
  7. activePanel: ActivePanelType;
  8. hidePanel(hash?: string): void;
  9. togglePanel(panel: SidebarPanelKey): void;
  10. }
  11. const storeConfig: SidebarPanelStoreDefinition = {
  12. activePanel: '',
  13. init() {
  14. // XXX: Do not use `this.listenTo` in this store. We avoid usage of reflux
  15. // listeners due to their leaky nature in tests.
  16. },
  17. activatePanel(panel: SidebarPanelKey) {
  18. this.activePanel = panel;
  19. this.trigger(this.activePanel);
  20. },
  21. togglePanel(panel: SidebarPanelKey) {
  22. if (this.activePanel === panel) {
  23. this.hidePanel();
  24. } else {
  25. this.activatePanel(panel);
  26. }
  27. },
  28. hidePanel(hash?: string) {
  29. this.activePanel = '';
  30. if (hash) {
  31. window.location.hash = window.location.hash.replace(`#${hash}`, '');
  32. }
  33. this.trigger(this.activePanel);
  34. },
  35. getState() {
  36. return this.activePanel;
  37. },
  38. };
  39. /**
  40. * This store is used to hold local user preferences
  41. * Side-effects (like reading/writing to cookies) are done in associated actionCreators
  42. */
  43. const SidebarPanelStore = createStore(storeConfig);
  44. export default SidebarPanelStore;