From 7a58bd8827a8b5c0232d15a8f7144742db00c5fe Mon Sep 17 00:00:00 2001
From: Krzysztof Kotlarek <kotlarek.krzysztof@gmail.com>
Date: Wed, 24 Aug 2022 14:09:40 +1000
Subject: [PATCH] FIX: hide sidebar toggle button when no sidebar (#18068)

When sidebar is not available (for example for anonymous user for sites which requires log in), toggle button should be hidden as well.
---
 .../discourse/app/controllers/application.js  | 21 +++++++++++++++----
 .../acceptance/sidebar-anonymous-user-test.js | 12 ++++++++++-
 2 files changed, 28 insertions(+), 5 deletions(-)

diff --git a/app/assets/javascripts/discourse/app/controllers/application.js b/app/assets/javascripts/discourse/app/controllers/application.js
index 45609c606f6..a7aede86aa2 100644
--- a/app/assets/javascripts/discourse/app/controllers/application.js
+++ b/app/assets/javascripts/discourse/app/controllers/application.js
@@ -18,8 +18,7 @@ export default Controller.extend({
   init() {
     this._super(...arguments);
     this.showSidebar =
-      (this.currentUser || !this.siteSettings.login_required) &&
-      !this.keyValueStore.getItem(HIDE_SIDEBAR_KEY);
+      this.canDisplaySidebar && !this.keyValueStore.getItem(HIDE_SIDEBAR_KEY);
   },
 
   @discourseComputed
@@ -31,6 +30,11 @@ export default Controller.extend({
     );
   },
 
+  @discourseComputed
+  canDisplaySidebar() {
+    return this.currentUser || !this.siteSettings.login_required;
+  },
+
   @discourseComputed
   loginRequired() {
     return this.siteSettings.login_required && !this.currentUser;
@@ -60,9 +64,18 @@ export default Controller.extend({
   @discourseComputed(
     "enable_sidebar",
     "siteSettings.enable_sidebar",
-    "router.currentRouteName"
+    "router.currentRouteName",
+    "canDisplaySidebar"
   )
-  sidebarEnabled(sidebarQueryParamOverride, enableSidebar, currentRouteName) {
+  sidebarEnabled(
+    sidebarQueryParamOverride,
+    enableSidebar,
+    currentRouteName,
+    canDisplaySidebar
+  ) {
+    if (!canDisplaySidebar) {
+      return false;
+    }
     if (sidebarQueryParamOverride === "1") {
       return true;
     }
diff --git a/app/assets/javascripts/discourse/tests/acceptance/sidebar-anonymous-user-test.js b/app/assets/javascripts/discourse/tests/acceptance/sidebar-anonymous-user-test.js
index f88ec26083f..e60767c174e 100644
--- a/app/assets/javascripts/discourse/tests/acceptance/sidebar-anonymous-user-test.js
+++ b/app/assets/javascripts/discourse/tests/acceptance/sidebar-anonymous-user-test.js
@@ -22,6 +22,11 @@ acceptance("Sidebar - Anonymous User", function (needs) {
       exists(".sidebar-container"),
       "sidebar exists for anonymous user"
     );
+
+    assert.ok(
+      exists(".header-sidebar-toggle"),
+      "toggle button for anonymous user"
+    );
   });
 });
 
@@ -32,12 +37,17 @@ acceptance("Sidebar - Anonymous User - Login Required", function (needs) {
     login_required: true,
   });
 
-  test("sidebar is hidden", async function (assert) {
+  test("sidebar and toggle button is hidden", async function (assert) {
     await visit("/");
 
     assert.ok(
       !exists(".sidebar-container"),
       "sidebar is hidden for anonymous user"
     );
+
+    assert.ok(
+      !exists(".header-sidebar-toggle"),
+      "toggle button is hidden for anonymous user"
+    );
   });
 });