discourse/app/assets/javascripts/admin/addon/services/admin-sidebar-state-manager.js
Kris c293fe02c3
FIX: check if sidebar hidden and remove scrollLock when hiding hamburger menu (#32775)
Reported here:
https://meta.discourse.org/t/dashboard-freezes-until-sidebar-is-opened/366402

This is a follow-up to
https://github.com/discourse/discourse/pull/32651, when navigating from
the homepage to admin the hamburger gets force-hidden because we switch
from the dropdown to the sidebar nav but the scrollLock was
unintentionally left behind.

This makes sure we're only messing with hamburger state when the sidebar
is hidden (with ` {{hideApplicationSidebar}}`) and also removes the
scroll lock when the hamburger is force-hidden.
2025-05-16 15:30:24 -04:00

82 lines
2.1 KiB
JavaScript
Vendored

import Service, { service } from "@ember/service";
import KeyValueStore from "discourse/lib/key-value-store";
import scrollLock from "discourse/lib/scroll-lock";
import { ADMIN_PANEL, MAIN_PANEL } from "discourse/lib/sidebar/panels";
import AdminSearchModal from "admin/components/modal/admin-search";
export default class AdminSidebarStateManager extends Service {
@service sidebarState;
@service header;
STORE_NAMESPACE = "discourse_admin_sidebar_experiment_";
keywords = {};
store = new KeyValueStore(this.STORE_NAMESPACE);
get navConfig() {
return this.store.getObject("navConfig");
}
set navConfig(value) {
this.store.setObject({ key: "navConfig", value });
}
setLinkKeywords(link_name, keywords) {
if (!this.keywords[link_name]) {
this.keywords[link_name] = {
navigation: keywords.map((keyword) => keyword.toLowerCase()),
};
return;
}
this.keywords[link_name].navigation = [
...new Set(
this.keywords[link_name].navigation.concat(
keywords.map((keyword) => keyword.toLowerCase())
)
),
];
}
maybeForceAdminSidebar(opts = {}) {
opts.onlyIfAlreadyActive ??= true;
const isAdminSidebarActive =
this.sidebarState.currentPanel?.key === ADMIN_PANEL;
if (!opts.onlyIfAlreadyActive) {
return this.#forceAdminSidebar();
}
if (isAdminSidebarActive) {
return this.#forceAdminSidebar();
} else {
this.sidebarState.isForcingSidebar = false;
return false;
}
}
stopForcingAdminSidebar() {
this.sidebarState.setPanel(MAIN_PANEL);
this.sidebarState.isForcingSidebar = false;
}
get modals() {
return { adminSearch: AdminSearchModal };
}
#forceAdminSidebar() {
this.sidebarState.setPanel(ADMIN_PANEL);
this.sidebarState.setSeparatedMode();
this.sidebarState.hideSwitchPanelButtons();
this.sidebarState.isForcingSidebar = true;
// we may navigate to admin from the header dropdown
// and when we do, we have to close it
if (this.sidebarState.sidebarHidden) {
this.header.hamburgerVisible = false;
scrollLock(false);
}
return true;
}
}