Skip to content

Commit 270f8b2

Browse files
committed
DEV: Refactor isLastLink logic for header icon links
Move `isLastLink` logic to a method using `site.mobileView` service for dynamic checks of visible links. This replaces static index comparisons with runtime filtering, ensuring accurate detection in both mobile and desktop views. Improves maintainability and correctness of conditional styling for the last custom header icon link.
1 parent 7ecf56e commit 270f8b2

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Component from "@glimmer/component";
2+
import { service } from "@ember/service";
23
import { dasherize } from "@ember/string";
34
import { htmlSafe } from "@ember/template";
45
import concatClass from "discourse/helpers/concat-class";
@@ -27,13 +28,11 @@ export default {
2728
try {
2829
const links = settings.header_links || [];
2930

30-
links.forEach((link, index) => {
31+
links.forEach((link) => {
3132
const iconTemplate = buildIconTemplate(link.icon, link.title);
3233
const className = `header-icon-${dasherize(link.title)}`;
3334
const target = link.target === "blank" ? "_blank" : "";
3435
const rel = link.target ? "noopener" : "";
35-
const isLastLink =
36-
index === links.length - 1 ? "last-custom-icon" : "";
3736

3837
const numericWidth = Number(link.width);
3938
const style = Number.isFinite(numericWidth)
@@ -47,13 +46,25 @@ export default {
4746
: link.view === "vdo" || link.view === "vdm";
4847
}
4948

49+
@service site;
50+
51+
get isLastLink() {
52+
const visibleLinks = links.filter((item) =>
53+
this.site.mobileView
54+
? item.view === "vmo" || item.view === "vdm"
55+
: item.view === "vdo" || item.view === "vdm"
56+
);
57+
58+
return link === visibleLinks.at(-1);
59+
}
60+
5061
<template>
5162
<li
5263
class={{concatClass
5364
"custom-header-icon-link"
5465
className
5566
link.view
56-
isLastLink
67+
(if this.isLastLink "last-custom-icon")
5768
}}
5869
>
5970
<a

0 commit comments

Comments
 (0)