Skip to content

Commit 802044d

Browse files
committed
chore: simplify code
1 parent 3a92ac7 commit 802044d

File tree

1 file changed

+8
-17
lines changed

1 file changed

+8
-17
lines changed

src/pages/404.astro

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,14 @@ import Article from "../layouts/templates/Article.astro";
3535
<script>
3636
function register() {
3737
const localeElements = document.querySelectorAll("[data-translation]");
38-
39-
for (const element of localeElements) {
40-
if (!(element instanceof HTMLElement)) continue;
41-
const { translation } = element.dataset;
42-
if (!translation) continue;
43-
44-
if (window.location.pathname.startsWith(`/${translation}/`)) {
45-
element.classList.add("active");
46-
break;
47-
}
48-
}
49-
50-
if (!Array.from(localeElements).some((el) => el instanceof HTMLElement && el.classList.contains("active"))) {
51-
const defaultElement = document.querySelector(`[data-translation="de"]`);
52-
if (defaultElement instanceof HTMLElement) {
53-
defaultElement.classList.add("active");
54-
}
38+
const pathname = window.location.pathname;
39+
const activeElement =
40+
Array.from(localeElements).find(
41+
(el) => el instanceof HTMLElement && pathname.startsWith(`/${el.dataset.translation}/`),
42+
) || document.querySelector('[data-translation="de"]');
43+
44+
if (activeElement instanceof HTMLElement) {
45+
activeElement.classList.add("active");
5546
}
5647
}
5748

0 commit comments

Comments
 (0)