Skip to content

Commit c546afb

Browse files
committed
DEV: Simplify CustomHeaderIcon view logic and improve maintainability
Refactor `shouldRender` and `isLastLink` logic by introducing a reusable `shouldRenderViewMode` function and encapsulating mobile/desktop view checks in dedicated sets. Extract `visibleLinks` computation into a separate getter for clarity and reuse.
1 parent e42dfd5 commit c546afb

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

javascripts/discourse/components/custom-header-icon.gjs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,26 @@ import dasherize from "discourse/helpers/dasherize";
88
import { escapeExpression } from "discourse/lib/utilities";
99
import isValidUrl from "../lib/isValidUrl";
1010

11+
const MOBILE_VIEWS = new Set(["vmo", "vdm"]);
12+
const DESKTOP_VIEWS = new Set(["vdo", "vdm"]);
13+
14+
function shouldRenderViewMode(view, isMobile) {
15+
return isMobile ? MOBILE_VIEWS.has(view) : DESKTOP_VIEWS.has(view);
16+
}
17+
1118
export default class CustomHeaderIcon extends Component {
1219
static shouldRender(args, context) {
13-
return context.site.mobileView
14-
? args.link.view === "vmo" || args.link.view === "vdm"
15-
: args.link.view === "vdo" || args.link.view === "vdm";
20+
return shouldRenderViewMode(args.link.view, context.site.mobileView);
1621
}
1722

1823
@service site;
1924

20-
get className() {
25+
get iconClassName() {
2126
return `header-icon-${dasherize(this.args.link.title)}`;
2227
}
2328

2429
get isLastLink() {
25-
const visibleLinks = this.args.links.filter((item) =>
26-
this.site.mobileView
27-
? item.view === "vmo" || item.view === "vdm"
28-
: item.view === "vdo" || item.view === "vdm"
29-
);
30-
31-
return this.args.link === visibleLinks.at(-1);
30+
return this.args.link === this.visibleLinks.at(-1);
3231
}
3332

3433
get style() {
@@ -38,11 +37,17 @@ export default class CustomHeaderIcon extends Component {
3837
: undefined;
3938
}
4039

40+
get visibleLinks() {
41+
return this.args.links.filter((link) =>
42+
shouldRenderViewMode(link.view, this.site.mobileView)
43+
);
44+
}
45+
4146
<template>
4247
<li
4348
class={{concatClass
4449
"custom-header-icon-link"
45-
this.className
50+
this.iconClassName
4651
@link.view
4752
(if this.isLastLink "last-custom-icon")
4853
}}

0 commit comments

Comments
 (0)