A platform for community discussion. Free, open, simple.
FIX: Hide Horizon's new topic button when another panel owns the sidebar (#42069)
Sérgio Saquetim161a4716ff31
diff --git a/themes/horizon/javascripts/discourse/components/sidebar-new-topic-button.gjs b/themes/horizon/javascripts/discourse/components/sidebar-new-topic-button.gjsindex 8b92c634683..cfb7328f936 100644--- a/themes/horizon/javascripts/discourse/components/sidebar-new-topic-button.gjs+++ b/themes/horizon/javascripts/discourse/components/sidebar-new-topic-button.gjs@@ -19,15 +19,19 @@ export default class SidebarNewTopicButton extends Component {@service router;@service header;@service appEvents;+ @service sidebarState;@controller application;@tracked category;@tracked tag;+ // Keyed on the panel that owns the sidebar rather than on the path: every takeover displaces+ // the forum navigation this button is part of, and most have no distinguishing URL prefix.+ // `isForcingSidebar` is not equivalent, since a panel can take over without setting it.get shouldRender() {return (this.currentUser &&- !this.router.currentURL.startsWith("/admin") &&+ this.sidebarState.showMainPanel &&this.application.sidebarEnabled);}diff --git a/themes/horizon/spec/system/sidebar_topic_button_spec.rb b/themes/horizon/spec/system/sidebar_topic_button_spec.rbindex d816cc29ce4..6bf566c3412 100644--- a/themes/horizon/spec/system/sidebar_topic_button_spec.rb+++ b/themes/horizon/spec/system/sidebar_topic_button_spec.rb@@ -58,6 +58,20 @@ RSpec.describe "Sidebar New Topic Button" doendend+ context "when another panel has taken over the sidebar" do+ fab!(:admin)++ before { sign_in(admin) }++ it "hides the button in the admin sidebar and brings it back on the way out" do+ visit("/admin")+ expect(page).to have_no_css(".sidebar-new-topic-button__wrapper")++ visit("/latest")+ expect(page).to have_css(".sidebar-new-topic-button__wrapper")+ end+ end+context "for anon" doit "does not render the sidebar button for anons" dovisit("/latest")diff --git a/themes/horizon/test/acceptance/sidebar-new-topic-button-test.gjs b/themes/horizon/test/acceptance/sidebar-new-topic-button-test.gjsnew file mode 100644index 00000000000..4e223244739--- /dev/null+++ b/themes/horizon/test/acceptance/sidebar-new-topic-button-test.gjs@@ -0,0 +1,46 @@+import { render } from "@ember/test-helpers";+import { module, test } from "qunit";+import sinon from "sinon";+import { ADMIN_PANEL, MAIN_PANEL } from "discourse/lib/sidebar/panels";+import { setupRenderingTest } from "discourse/tests/helpers/component-test";+import SidebarNewTopicButton from "../../discourse/components/sidebar-new-topic-button";++module(+ "Horizon | Integration | Component | SidebarNewTopicButton",+ function (hooks) {+ // stubRouter supplies the currentRoute the button reads to pre-fill a category or tag.+ setupRenderingTest(hooks, { stubRouter: true });++ hooks.beforeEach(function () {+ // Both of these gate rendering on their own, so without them the negative assertion below+ // would pass no matter what the panel is. `sidebarEnabled` is a getter and has to be+ // stubbed rather than set.+ sinon+ .stub(this.owner.lookup("controller:application"), "sidebarEnabled")+ .get(() => true);+ this.currentUser.set("can_create_topic", true);++ // A plain forum path. The panel, not the path, is what should decide this, so the+ // assertions below have to hold on a URL that looks like ordinary browsing.+ this.owner.lookup("service:router").currentURL = "/latest";+ });++ test("renders while the forum panel owns the sidebar", async function (assert) {+ this.owner.lookup("service:sidebar-state").setPanel(MAIN_PANEL);++ await render(<template><SidebarNewTopicButton /></template>);++ assert.dom(".sidebar-new-topic-button__wrapper").exists();+ });++ // Rendering happens without a URL, so nothing here can be hidden by matching a path. Admin+ // stands in for every takeover, which all claim the sidebar the same way.+ test("is hidden while another panel owns the sidebar", async function (assert) {+ this.owner.lookup("service:sidebar-state").setPanel(ADMIN_PANEL);++ await render(<template><SidebarNewTopicButton /></template>);++ assert.dom(".sidebar-new-topic-button__wrapper").doesNotExist();+ });+ }+);