DEV: Simplify sidebar's collapsedSections (#26972)

This commit is contained in:
Jarek Radosz 2024-05-11 11:15:38 +02:00 committed by GitHub
parent 3e7601cada
commit 4c47f25530
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 17 additions and 29 deletions

View File

@ -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() {

View File

@ -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 {
@action
toggleAllSections() {
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);
}
});
}

View File

@ -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 {
@disableImplicitInjections
export 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 = "";
}