sidebarPanelStore.tsx 1.3 KB

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