A platform for community discussion. Free, open, simple.
DEV: Simplify sidebar's `collapsedSections` (#26972)
Jarek Radosz4c47f2553007
diff --git a/app/assets/javascripts/discourse/app/components/sidebar/section.js b/app/assets/javascripts/discourse/app/components/sidebar/section.jsindex 62477baa7d0..a0ffbcda87c 100644--- a/app/assets/javascripts/discourse/app/components/sidebar/section.js+++ b/app/assets/javascripts/discourse/app/components/sidebar/section.js@@ -1,5 +1,4 @@import Component from "@glimmer/component";-import { tracked } from "@glimmer/tracking";import { action } from "@ember/object";import { service } from "@ember/service";import { bind } from "discourse-common/utils/decorators";@@ -8,7 +7,6 @@ export default class SidebarSection extends Component {@service keyValueStore;@service sidebarState;- @tracked collapsedSections = this.sidebarState.collapsedSections;sidebarSectionContentID = `sidebar-section-content-${this.args.sectionName}`;collapsedSidebarSectionKey = `sidebar-section-${this.args.sectionName}-collapsed`;@@ -36,7 +34,9 @@ export default class SidebarSection extends Component {}get displaySectionContent() {- return !this.collapsedSections.includes(this.collapsedSidebarSectionKey);+ return !this.sidebarState.collapsedSections.has(+ this.collapsedSidebarSectionKey+ );}willDestroy() {diff --git a/app/assets/javascripts/discourse/app/components/sidebar/toggle-all-sections.gjs b/app/assets/javascripts/discourse/app/components/sidebar/toggle-all-sections.gjsindex 6de06f07da8..b83c0092071 100644--- a/app/assets/javascripts/discourse/app/components/sidebar/toggle-all-sections.gjs+++ b/app/assets/javascripts/discourse/app/components/sidebar/toggle-all-sections.gjs@@ -1,5 +1,4 @@import Component from "@glimmer/component";-import { tracked } from "@glimmer/tracking";import { action } from "@ember/object";import { service } from "@ember/service";import DButton from "discourse/components/d-button";@@ -8,11 +7,10 @@ import { ADMIN_NAV_MAP } from "discourse/lib/sidebar/admin-nav-map";export default class ToggleAllSections extends Component {@service sidebarState;@service keyValueStore;- @tracked collapsedSections = this.sidebarState.collapsedSections;get allSectionsExpanded() {return ADMIN_NAV_MAP.every((adminNav) => {- return !this.collapsedSections.includes(+ return !this.sidebarState.collapsedSections.has(`sidebar-section-${this.sidebarState.currentPanel.key}-${adminNav.name}-collapsed`);});@@ -32,13 +30,15 @@ export default class ToggleAllSections extends Component {@actiontoggleAllSections() {- const collapseOrExpand = this.allSectionsExpanded- ? this.sidebarState.collapseSection.bind(this)- : this.sidebarState.expandSection.bind(this);+ const collapse = this.allSectionsExpanded;+ADMIN_NAV_MAP.forEach((adminNav) => {- collapseOrExpand(- `${this.sidebarState.currentPanel.key}-${adminNav.name}`- );+ const key = `${this.sidebarState.currentPanel.key}-${adminNav.name}`;+ if (collapse) {+ this.sidebarState.collapseSection(key);+ } else {+ this.sidebarState.expandSection(key);+ }});}diff --git a/app/assets/javascripts/discourse/app/services/sidebar-state.js b/app/assets/javascripts/discourse/app/services/sidebar-state.jsindex 8127435b29b..0157bd4677d 100644--- a/app/assets/javascripts/discourse/app/services/sidebar-state.js+++ b/app/assets/javascripts/discourse/app/services/sidebar-state.js@@ -1,5 +1,4 @@import { tracked } from "@glimmer/tracking";-import { A } from "@ember/array";import { registerDestructor } from "@ember/destroyable";import Service, { service } from "@ember/service";import { TrackedSet } from "@ember-compat/tracked-built-ins";@@ -17,21 +16,16 @@ import {@disableImplicitInjectionsexport default class SidebarState extends Service {@service keyValueStore;+@tracked currentPanelKey = currentPanelKey;- @tracked panels = panels;@tracked mode = COMBINED_MODE;@tracked displaySwitchPanelButtons = false;@tracked filter = "";- @tracked collapsedSections = A([]);-+ panels = panels;+ collapsedSections = new TrackedSet();previousState = {};#hiders = new TrackedSet();- constructor() {- super(...arguments);- this.#reset();- }-get sidebarHidden() {return this.#hiders.size > 0;}@@ -85,13 +79,13 @@ export default class SidebarState extends Service {collapseSection(sectionKey) {const collapsedSidebarSectionKey = `sidebar-section-${sectionKey}-collapsed`;this.keyValueStore.setItem(collapsedSidebarSectionKey, true);- this.collapsedSections.pushObject(collapsedSidebarSectionKey);+ this.collapsedSections.add(collapsedSidebarSectionKey);}expandSection(sectionKey) {const collapsedSidebarSectionKey = `sidebar-section-${sectionKey}-collapsed`;this.keyValueStore.setItem(collapsedSidebarSectionKey, false);- this.collapsedSections.removeObject(collapsedSidebarSectionKey);+ this.collapsedSections.delete(collapsedSidebarSectionKey);}isCurrentPanel(panel) {@@ -125,12 +119,6 @@ export default class SidebarState extends Service {return this.currentPanelKey === MAIN_PANEL;}- #reset() {- this.currentPanelKey = currentPanelKey;- this.panels = panels;- this.mode = COMBINED_MODE;- }-clearFilter() {this.filter = "";}