Skip to content

Commit 5bfc2a3

Browse files
committed
DEV: Prevent static viewport mode access during header link initialization
Refactor header icon links to dynamically determine `site.mobileView`. This ensures conditional rendering based on the current viewport state while avoiding premature static lookups. Improves robustness in customizing header icons for mobile and desktop layouts.
1 parent bff6d88 commit 5bfc2a3

File tree

1 file changed

+36
-34
lines changed

1 file changed

+36
-34
lines changed

javascripts/discourse/initializers/initialize-for-header-icon-links.gjs

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import Component from "@glimmer/component";
12
import { dasherize } from "@ember/string";
3+
import { htmlSafe } from "@ember/template";
24
import concatClass from "discourse/helpers/concat-class";
35
import icon from "discourse/helpers/d-icon";
46
import { withPluginApi } from "discourse/lib/plugin-api";
@@ -19,19 +21,9 @@ function buildIcon(iconNameOrImageUrl, title) {
1921
export default {
2022
name: "header-icon-links",
2123
initialize() {
22-
withPluginApi("0.8.41", (api) => {
24+
withPluginApi((api) => {
2325
try {
24-
const site = api.container.lookup("service:site");
2526
let links = settings.header_links;
26-
if (site.mobileView) {
27-
links = links.filter(
28-
(link) => link.view === "vmo" || link.view === "vdm"
29-
);
30-
} else {
31-
links = links.filter(
32-
(link) => link.view === "vdo" || link.view === "vdm"
33-
);
34-
}
3527

3628
links.forEach((link, index) => {
3729
const iconTemplate = buildIcon(link.icon, link.title);
@@ -41,32 +33,42 @@ export default {
4133
const isLastLink =
4234
index === links.length - 1 ? "last-custom-icon" : "";
4335

44-
let style = "";
45-
if (link.width) {
46-
style = `width: ${escapeExpression(link.width)}px`;
36+
let style;
37+
if (link.width && !isNaN(link.width)) {
38+
style = htmlSafe(`width: ${escapeExpression(link.width)}px`);
4739
}
4840

49-
const iconComponent = <template>
50-
<li
51-
class={{concatClass
52-
"custom-header-icon-link"
53-
className
54-
link.view
55-
isLastLink
56-
}}
57-
>
58-
<a
59-
class="btn no-text icon btn-flat"
60-
href={{link.url}}
61-
title={{link.title}}
62-
target={{target}}
63-
rel={{rel}}
64-
style={{style}}
41+
const iconComponent = class extends Component {
42+
static shouldRender(args, context) {
43+
if (context.site.mobileView) {
44+
return link.view === "vmo" || link.view === "vdm";
45+
} else {
46+
return link.view === "vdo" || link.view === "vdm";
47+
}
48+
}
49+
50+
<template>
51+
<li
52+
class={{concatClass
53+
"custom-header-icon-link"
54+
className
55+
link.view
56+
isLastLink
57+
}}
6558
>
66-
{{iconTemplate}}
67-
</a>
68-
</li>
69-
</template>;
59+
<a
60+
class="btn no-text icon btn-flat"
61+
href={{link.url}}
62+
title={{link.title}}
63+
target={{target}}
64+
rel={{rel}}
65+
style={{style}}
66+
>
67+
{{iconTemplate}}
68+
</a>
69+
</li>
70+
</template>
71+
};
7072

7173
const beforeIcon = ["chat", "search", "hamburger", "user-menu"];
7274

0 commit comments

Comments
 (0)