Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FIX: Simplify sidebar custom link implementation (#26201)
All our link validation, and conversion from url -> route/model/query is expensive and prone to bugs. Instead, if people enter a link, we can just use it as-is. Originally all this extra logic was added to handle unusual situations like `/safe-mode`, `/my/...`, etc. However, all of these are now handled correctly by our Ember router, so there is no need for it. Now, we just pass the user-supplied `href` directly to the SectionLink component, and let Ember handle routing to it when clicked. The only functional change here is that we no longer validate internal links by parsing them with the Ember router. But I'd argue this is fine, because the previous logic would cause both false positives (e.g. `/t/123` would be valid, even if topic 123 doesn't exist), and false negatives (for routes which are server-side only, like the new AI share pages).
- Loading branch information
1 parent
a884842
commit e3cfb19
Showing
11 changed files
with
103 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 18 additions & 30 deletions
48
app/assets/javascripts/discourse/app/components/sidebar/more-section-link.hbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,18 @@ | ||
{{#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}} | ||
<Sidebar::SectionLink | ||
@shouldDisplay={{@sectionLink.shouldDisplay}} | ||
@href={{@sectionLink.href}} | ||
@title={{@sectionLink.title}} | ||
@linkName={{@sectionLink.name}} | ||
@route={{@sectionLink.route}} | ||
@model={{@sectionLink.model}} | ||
@models={{@sectionLink.models}} | ||
@query={{@sectionLink.query}} | ||
@content={{replace-emoji @sectionLink.text}} | ||
@badgeText={{@sectionLink.badgeText}} | ||
@prefixType="icon" | ||
@prefixValue={{@sectionLink.prefixValue}} | ||
@suffixCSSClass={{@sectionLink.suffixCSSClass}} | ||
@suffixValue={{@sectionLink.suffixValue}} | ||
@suffixType={{@sectionLink.suffixType}} | ||
@currentWhen={{@sectionLink.currentWhen}} | ||
/> | ||
{{/if}} | ||
<Sidebar::SectionLink | ||
@badgeText={{@sectionLink.badgeText}} | ||
@content={{replace-emoji @sectionLink.text}} | ||
@currentWhen={{@sectionLink.currentWhen}} | ||
@href={{or @sectionLink.href @sectionLink.value}} | ||
@linkName={{@sectionLink.name}} | ||
@model={{@sectionLink.model}} | ||
@models={{@sectionLink.models}} | ||
@prefixType="icon" | ||
@prefixValue={{@sectionLink.prefixValue}} | ||
@query={{@sectionLink.query}} | ||
@route={{@sectionLink.route}} | ||
@shouldDisplay={{@sectionLink.shouldDisplay}} | ||
@suffixCSSClass={{@sectionLink.suffixCSSClass}} | ||
@suffixType={{@sectionLink.suffixType}} | ||
@suffixValue={{@sectionLink.suffixValue}} | ||
@title={{@sectionLink.title}} | ||
/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 0 additions & 36 deletions
36
app/assets/javascripts/discourse/app/lib/sidebar/route-info-helper.js
This file was deleted.
Oops, something went wrong.
26 changes: 1 addition & 25 deletions
26
app/assets/javascripts/discourse/app/lib/sidebar/section-link.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,20 @@ | ||
import { tracked } from "@glimmer/tracking"; | ||
import RouteInfoHelper from "discourse/lib/sidebar/route-info-helper"; | ||
import { defaultHomepage } from "discourse/lib/utilities"; | ||
|
||
export default class SectionLink { | ||
@tracked linkDragCss; | ||
|
||
constructor( | ||
{ external, full_reload, icon, id, name, value }, | ||
section, | ||
router | ||
) { | ||
constructor({ external, icon, id, name, value }, section) { | ||
this.external = external; | ||
this.fullReload = full_reload; | ||
this.prefixValue = icon; | ||
this.id = id; | ||
this.name = name; | ||
this.text = name; | ||
this.value = value; | ||
this.section = section; | ||
this.withAnchor = value.match(/#\w+$/gi); | ||
|
||
if (!this.externalOrFullReload) { | ||
const routeInfoHelper = new RouteInfoHelper(router, value); | ||
|
||
if (routeInfoHelper.route === "discovery.index") { | ||
this.route = `discovery.${defaultHomepage()}`; | ||
} else { | ||
this.route = routeInfoHelper.route; | ||
} | ||
|
||
this.models = routeInfoHelper.models; | ||
this.query = routeInfoHelper.query; | ||
} | ||
} | ||
|
||
get shouldDisplay() { | ||
return true; | ||
} | ||
|
||
get externalOrFullReload() { | ||
return this.external || this.fullReload || this.withAnchor; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
class SidebarUrlSerializer < ApplicationSerializer | ||
attributes :id, :name, :value, :icon, :external, :full_reload, :segment | ||
attributes :id, :name, :value, :icon, :external, :segment | ||
|
||
def external | ||
object.external? | ||
end | ||
|
||
def full_reload | ||
object.full_reload? | ||
end | ||
end |
Oops, something went wrong.
e3cfb19
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This commit has been mentioned on Discourse Meta. There might be relevant details there:
https://meta.discourse.org/t/header-dropdown-navigation-menu-custom-link-removes-heading-anchor-from-url/275465/6
e3cfb19
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This commit has been mentioned on Discourse Meta. There might be relevant details there:
https://meta.discourse.org/t/custom-menu-links-are-modified-and-broken-by-discourse/299027/18
e3cfb19
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This commit has been mentioned on Discourse Meta. There might be relevant details there:
https://meta.discourse.org/t/preventing-broken-links-in-the-sidebar/307539/1