diff --git a/app/assets/javascripts/discourse/app/components/sidebar/switch-panel-buttons.gjs b/app/assets/javascripts/discourse/app/components/sidebar/switch-panel-buttons.gjs index 2ae2a8428fd..3d1ef6d9153 100644 --- a/app/assets/javascripts/discourse/app/components/sidebar/switch-panel-buttons.gjs +++ b/app/assets/javascripts/discourse/app/components/sidebar/switch-panel-buttons.gjs @@ -4,43 +4,32 @@ import { fn } from "@ember/helper"; import { action } from "@ember/object"; import { service } from "@ember/service"; import DButton from "discourse/components/d-button"; -import { defaultHomepage } from "discourse/lib/utilities"; -import getURL from "discourse-common/lib/get-url"; export default class SwitchPanelButtons extends Component { @service router; @service sidebarState; - @tracked currentPanel; @tracked isSwitching = false; - get destination() { - if (this.currentPanel) { - const url = - this.currentPanel.switchButtonDefaultUrl || - this.currentPanel.lastKnownURL; - return url === "/" ? `discovery.${defaultHomepage()}` : getURL(url); - } - return null; - } - @action async switchPanel(panel) { this.isSwitching = true; - this.currentPanel = panel; this.sidebarState.currentPanel.lastKnownURL = this.router.currentURL; - if (this.destination) { - try { - await this.router.transitionTo(this.destination).followRedirects(); - this.sidebarState.setPanel(this.currentPanel.key); - } catch (e) { - if (e.name !== "TransitionAborted") { - throw e; - } - } finally { - this.isSwitching = false; + const destination = panel?.switchButtonDefaultUrl || panel?.lastKnownURL; + if (!destination) { + return; + } + + try { + await this.router.transitionTo(destination).followRedirects(); + this.sidebarState.setPanel(panel.key); + } catch (e) { + if (e.name !== "TransitionAborted") { + throw e; } + } finally { + this.isSwitching = false; } } diff --git a/plugins/chat/assets/javascripts/discourse/initializers/chat-sidebar.js b/plugins/chat/assets/javascripts/discourse/initializers/chat-sidebar.js index 4f35299c3ac..5cc0967cb43 100644 --- a/plugins/chat/assets/javascripts/discourse/initializers/chat-sidebar.js +++ b/plugins/chat/assets/javascripts/discourse/initializers/chat-sidebar.js @@ -8,7 +8,6 @@ import { withPluginApi } from "discourse/lib/plugin-api"; import { emojiUnescape } from "discourse/lib/text"; import { escapeExpression } from "discourse/lib/utilities"; import { avatarUrl } from "discourse-common/lib/avatar-utils"; -import getURL from "discourse-common/lib/get-url"; import { bind } from "discourse-common/utils/decorators"; import I18n from "discourse-i18n"; import ChatModalNewMessage from "discourse/plugins/chat/discourse/components/chat/modal/new-message"; @@ -40,7 +39,7 @@ export default { switchButtonIcon = "d-chat"; get switchButtonDefaultUrl() { - return getURL(chatStateManager.lastKnownChatURL || "/chat"); + return chatStateManager.lastKnownChatURL || "/chat"; } } ); diff --git a/plugins/chat/assets/javascripts/discourse/routes/chat.js b/plugins/chat/assets/javascripts/discourse/routes/chat.js index 94c4d2db983..579fdde8a9b 100644 --- a/plugins/chat/assets/javascripts/discourse/routes/chat.js +++ b/plugins/chat/assets/javascripts/discourse/routes/chat.js @@ -98,7 +98,12 @@ export default class ChatRoute extends DiscourseRoute { }); if (transition) { - const url = this.router.urlFor(transition.from.name); + let url = this.router.urlFor(transition.from.name); + + if (this.router.rootURL !== "/") { + url = url.replace(new RegExp(`^${this.router.rootURL}`), "/"); + } + this.chatStateManager.storeChatURL(url); } diff --git a/plugins/chat/assets/javascripts/discourse/services/chat-state-manager.js b/plugins/chat/assets/javascripts/discourse/services/chat-state-manager.js index d21e5b89df0..9a48135a243 100644 --- a/plugins/chat/assets/javascripts/discourse/services/chat-state-manager.js +++ b/plugins/chat/assets/javascripts/discourse/services/chat-state-manager.js @@ -4,7 +4,6 @@ import KeyValueStore from "discourse/lib/key-value-store"; import { withPluginApi } from "discourse/lib/plugin-api"; import { MAIN_PANEL } from "discourse/lib/sidebar/panels"; import { defaultHomepage } from "discourse/lib/utilities"; -import getURL from "discourse-common/lib/get-url"; import { getUserChatSeparateSidebarMode } from "discourse/plugins/chat/discourse/lib/get-user-chat-separate-sidebar-mode"; const PREFERRED_MODE_KEY = "preferred_mode"; @@ -182,16 +181,17 @@ export default class ChatStateManager extends Service { } get lastKnownAppURL() { - let url = this._appURL; - if (!url || url === "/") { - url = this.router.urlFor(`discovery.${defaultHomepage()}`); + const url = this._appURL; + + if (url && url !== "/") { + return url; } - return getURL(url); + return this.router.urlFor(`discovery.${defaultHomepage()}`); } get lastKnownChatURL() { - return getURL(this._chatURL || "/chat"); + return this._chatURL || "/chat"; } #publishStateChange() { diff --git a/plugins/chat/spec/system/separate_sidebar_mode_spec.rb b/plugins/chat/spec/system/separate_sidebar_mode_spec.rb index 325b3e5cc83..21fde310ee3 100644 --- a/plugins/chat/spec/system/separate_sidebar_mode_spec.rb +++ b/plugins/chat/spec/system/separate_sidebar_mode_spec.rb @@ -205,6 +205,25 @@ RSpec.describe "Separate sidebar mode", type: :system do expect(sidebar_component).to have_section_link(channel_2.name, active: true) end end + + context "with subfolder" do + let!(:channel_browse_page) { PageObjects::Pages::ChatBrowse.new } + + before do + set_subfolder "/discuss" + chat_page.prefers_full_page + end + + it "has the expected behavior" do + visit("/discuss/about") + + sidebar_component.switch_to_chat + expect(channel_browse_page.component).to be_present + + sidebar_component.switch_to_main + expect(page).to have_current_path("/discuss/") + end + end end describe "when separate sidebar mode is fullscreen" do