A platform for community discussion. Free, open, simple.
FIX: consistent sidebar section external links (#22343)
Krzysztof Kotlarek3c019b1c0f7d
diff --git a/app/assets/javascripts/discourse/app/components/sidebar/common/custom-section.hbs b/app/assets/javascripts/discourse/app/components/sidebar/common/custom-section.hbsindex a433af1e15d..cde9643f48f 100644--- a/app/assets/javascripts/discourse/app/components/sidebar/common/custom-section.hbs+++ b/app/assets/javascripts/discourse/app/components/sidebar/common/custom-section.hbs@@ -8,13 +8,14 @@@class={{this.section.dragCss}}>{{#each this.section.links as |link|}}- {{#if link.external}}+ {{#if link.externalOrFullReload}}<Sidebar::SectionLink@shouldDisplay={{link.shouldDisplay}}@linkName={{link.name}}@content={{replace-emoji link.text}}@prefixType="icon"@prefixValue={{link.prefixValue}}+ @fullReload={{link.fullReload}}@href={{link.value}}@class={{link.linkDragCss}}{{(ifdiff --git a/app/assets/javascripts/discourse/app/components/sidebar/more-section-link.hbs b/app/assets/javascripts/discourse/app/components/sidebar/more-section-link.hbsindex a3cf4c2ece1..134a1b0a5b7 100644--- a/app/assets/javascripts/discourse/app/components/sidebar/more-section-link.hbs+++ b/app/assets/javascripts/discourse/app/components/sidebar/more-section-link.hbs@@ -1,10 +1,11 @@-{{#if @sectionLink.external}}+{{#if @sectionLink.externalOrFullReload}}<Sidebar::SectionLink@shouldDisplay={{@sectionLink.shouldDisplay}}@linkName={{@sectionLink.name}}@content={{replace-emoji @sectionLink.text}}@prefixType="icon"@prefixValue={{@sectionLink.prefixValue}}+ @fullReload={{@sectionLink.fullReload}}@href={{@sectionLink.value}}/>{{else}}diff --git a/app/assets/javascripts/discourse/app/components/sidebar/section-link.js b/app/assets/javascripts/discourse/app/components/sidebar/section-link.jsindex 00e497d64e7..0c50537e832 100644--- a/app/assets/javascripts/discourse/app/components/sidebar/section-link.js+++ b/app/assets/javascripts/discourse/app/components/sidebar/section-link.js@@ -50,6 +50,9 @@ export default class SectionLink extends Component {}get target() {+ if (this.args.fullReload) {+ return "_self";+ }return this.currentUser?.user_option?.external_links_in_new_tab? "_blank": "_self";diff --git a/app/assets/javascripts/discourse/app/lib/sidebar/section-link.js b/app/assets/javascripts/discourse/app/lib/sidebar/section-link.jsindex 5846f319c5a..f08aff2fab0 100644--- a/app/assets/javascripts/discourse/app/lib/sidebar/section-link.js+++ b/app/assets/javascripts/discourse/app/lib/sidebar/section-link.js@@ -9,8 +9,13 @@ const MOUSE_DELAY = 250;export default class SectionLink {@tracked linkDragCss;- constructor({ external, icon, id, name, value }, section, router) {+ constructor(+ { external, full_reload, icon, id, name, value },+ section,+ router+ ) {this.external = external;+ this.fullReload = full_reload;this.prefixValue = icon;this.id = id;this.name = name;@@ -18,7 +23,7 @@ export default class SectionLink {this.value = value;this.section = section;- if (!this.external) {+ if (!this.externalOrFullReload) {const routeInfoHelper = new RouteInfoHelper(router, value);this.route = routeInfoHelper.route;this.models = routeInfoHelper.models;@@ -30,6 +35,10 @@ export default class SectionLink {return true;}+ get externalOrFullReload() {+ return this.external || this.fullReload;+ }+@binddidStartDrag(event) {// 0 represents left button of the mousediff --git a/app/serializers/sidebar_url_serializer.rb b/app/serializers/sidebar_url_serializer.rbindex 2425d0dda81..993aa92f6f2 100644--- a/app/serializers/sidebar_url_serializer.rb+++ b/app/serializers/sidebar_url_serializer.rb@@ -1,9 +1,13 @@# frozen_string_literal: trueclass SidebarUrlSerializer < ApplicationSerializer- attributes :id, :name, :value, :icon, :external, :segment+ attributes :id, :name, :value, :icon, :external, :full_reload, :segmentdef external- object.external? || object.full_reload?+ object.external?+ end++ def full_reload+ object.full_reload?endenddiff --git a/spec/system/custom_sidebar_sections_spec.rb b/spec/system/custom_sidebar_sections_spec.rbindex 18c49d14040..c92eaea28bf 100644--- a/spec/system/custom_sidebar_sections_spec.rb+++ b/spec/system/custom_sidebar_sections_spec.rb@@ -6,6 +6,8 @@ describe "Custom sidebar sections", type: :system dolet(:section_modal) { PageObjects::Modals::SidebarSectionForm.new }let(:sidebar) { PageObjects::Components::Sidebar.new }+ before { user.user_option.update!(external_links_in_new_tab: true) }+it "allows the user to create custom section" dovisit("/latest")@@ -40,7 +42,7 @@ describe "Custom sidebar sections", type: :system dosection_modal.saveexpect(sidebar).to have_section("My section")- expect(sidebar).to have_section_link("My preferences")+ expect(sidebar).to have_section_link("My preferences", target: "_self")endit "allows the user to create custom section with /pub link" do@@ -53,7 +55,7 @@ describe "Custom sidebar sections", type: :system dosection_modal.saveexpect(sidebar).to have_section("My section")- expect(sidebar).to have_section_link("Published Page")+ expect(sidebar).to have_section_link("Published Page", target: "_self")endit "allows the user to create custom section with external link" do@@ -76,7 +78,11 @@ describe "Custom sidebar sections", type: :system dosection_modal.saveexpect(sidebar).to have_section("My section")- expect(sidebar).to have_section_link("Discourse Homepage", href: "https://discourse.org")+ expect(sidebar).to have_section_link(+ "Discourse Homepage",+ href: "https://discourse.org",+ target: "_blank",+ )endit "allows the user to edit custom section" dodiff --git a/spec/system/page_objects/components/sidebar.rb b/spec/system/page_objects/components/sidebar.rbindex 7dbea8f9c07..ccd68a80f74 100644--- a/spec/system/page_objects/components/sidebar.rb+++ b/spec/system/page_objects/components/sidebar.rb@@ -57,8 +57,8 @@ module PageObjectshas_css?(".#{SIDEBAR_SECTION_LINK_SELECTOR}--active", count: 1)end- def has_section_link?(name, href: nil, active: false)- section_link_present?(name, href: href, active: active, present: true)+ def has_section_link?(name, href: nil, active: false, target: nil)+ section_link_present?(name, href: href, active: active, target: target, present: true)enddef has_no_section_link?(name, href: nil, active: false)@@ -119,11 +119,12 @@ module PageObjectsprivate- def section_link_present?(name, href: nil, active: false, present:)+ def section_link_present?(name, href: nil, active: false, target: nil, present:)attributes = { exact_text: name }attributes[:href] = href if hrefattributes[:class] = SIDEBAR_SECTION_LINK_SELECTORattributes[:class] += "--active" if active+ attributes[:target] = target if targetpage.public_send(present ? :has_link? : :has_no_link?, **attributes)end