Skip to content

Commit 7ecf56e

Browse files
committed
DEV: Refactor header icon link rendering and width handling
Rename `buildIcon` to `buildIconTemplate` for clarity and refactor width parsing to use `Number.isFinite` for safer checks. Streamline conditional rendering logic for mobile and desktop views, improving code maintainability.
1 parent 5bfc2a3 commit 7ecf56e

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

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

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,44 +7,44 @@ import { withPluginApi } from "discourse/lib/plugin-api";
77
import { escapeExpression } from "discourse/lib/utilities";
88
import isValidUrl from "../lib/isValidUrl";
99

10-
function buildIcon(iconNameOrImageUrl, title) {
10+
const beforeIcon = ["chat", "search", "hamburger", "user-menu"];
11+
12+
function buildIconTemplate(iconNameOrImageUrl, title) {
1113
if (isValidUrl(iconNameOrImageUrl)) {
1214
return <template>
1315
<img src={{iconNameOrImageUrl}} aria-hidden="true" />
1416
<span class="sr-only">{{title}}</span>
1517
</template>;
16-
} else {
17-
return <template>{{icon iconNameOrImageUrl label=title}}</template>;
1818
}
19+
20+
return <template>{{icon iconNameOrImageUrl label=title}}</template>;
1921
}
2022

2123
export default {
2224
name: "header-icon-links",
2325
initialize() {
2426
withPluginApi((api) => {
2527
try {
26-
let links = settings.header_links;
28+
const links = settings.header_links || [];
2729

2830
links.forEach((link, index) => {
29-
const iconTemplate = buildIcon(link.icon, link.title);
31+
const iconTemplate = buildIconTemplate(link.icon, link.title);
3032
const className = `header-icon-${dasherize(link.title)}`;
3133
const target = link.target === "blank" ? "_blank" : "";
3234
const rel = link.target ? "noopener" : "";
3335
const isLastLink =
3436
index === links.length - 1 ? "last-custom-icon" : "";
3537

36-
let style;
37-
if (link.width && !isNaN(link.width)) {
38-
style = htmlSafe(`width: ${escapeExpression(link.width)}px`);
39-
}
38+
const numericWidth = Number(link.width);
39+
const style = Number.isFinite(numericWidth)
40+
? htmlSafe(`width: ${escapeExpression(numericWidth)}px`)
41+
: undefined;
4042

4143
const iconComponent = class extends Component {
4244
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-
}
45+
return context.site.mobileView
46+
? link.view === "vmo" || link.view === "vdm"
47+
: link.view === "vdo" || link.view === "vdm";
4848
}
4949

5050
<template>
@@ -70,8 +70,6 @@ export default {
7070
</template>
7171
};
7272

73-
const beforeIcon = ["chat", "search", "hamburger", "user-menu"];
74-
7573
api.headerIcons.add(link.title, iconComponent, {
7674
before: beforeIcon,
7775
});

0 commit comments

Comments
 (0)