feat: hide nav item if no content in relevant collection
All checks were successful
Docker / build-and-push-image (push) Successful in 3m1s

This commit is contained in:
Troy 2025-08-22 22:57:31 +01:00
parent d1990ffb0e
commit e8160ad4fe
Signed by: troy
GPG key ID: DFC06C02ED3B4711
6 changed files with 29 additions and 21 deletions

View file

@ -2,9 +2,30 @@
import { SITE } from "@consts";
import Button from "@components/Button.astro";
import { Icon } from "astro-icon/components";
import { getCollection } from "astro:content";
const pathname = new URL(Astro.request.url).pathname;
const currentPath = pathname.replace(/\/$/, "");
const allPosts = (await getCollection("posts")).filter(
(post) => !post.data.draft,
);
const hasPosts = allPosts.length > 0;
const allProjects = (await getCollection("projects")).filter(
(post) => !post.data.draft,
);
const hasProjects = allProjects.length > 0;
const filteredNavLinks = SITE.NAVLINKS.filter((link) => {
if (link.href === "/posts") {
return hasPosts;
}
if (link.href === "/projects") {
return hasProjects;
}
return true;
});
---
<header class="mx-auto w-full">
@ -17,9 +38,10 @@ const currentPath = pathname.replace(/\/$/, "");
</a>
<ul class="flex w-fit flex-row items-center gap-4">
{
SITE.NAVLINKS.map((link) => {
filteredNavLinks.map((link) => {
let linkHref = link.href.replace(/\/$/, "");
const isActive = currentPath.startsWith(linkHref);
return (
<li class="text-tertiary hover:text-secondary focus:text-secondary font-medium transition-colors duration-300 focus:outline-hidden">
<a