import type { NavigationLink } from "@/libs/navigation";
import { chevronDown, chevronUp } from "solid-heroicons/solid";
import { createEffect, createSignal, For } from "solid-js";
import { usePageContext } from "vike-solid/usePageContext";
import { navigation } from "@/libs/navigation";
import { Link } from "@/components/Link";
import { Icon } from "solid-heroicons";
import clsx from "clsx";
type NavigationItemProps = {
section: (typeof navigation)[number];
onLinkClick?: (event: MouseEvent) => void;
};
function NavigationItem(props: NavigationItemProps) {
const pageContext = usePageContext();
const checkIsLinkActive = (link: NavigationLink) => {
return (
link.href === pageContext.urlPathname ||
link.subitems?.some((subitem) => subitem.href === pageContext.urlPathname)
);
};
const checkIsActive = () => {
return props.section.links.some((link) => checkIsLinkActive(link));
};
const [isOpened, setIsOpened] = createSignal(
checkIsActive() || pageContext.urlPathname === "/",
);
createEffect(() => {
// If the current URL path is the same as the link's href, set isOpened to true
if (!isOpened() && checkIsActive()) {
setIsOpened(true);
}
}, [pageContext.urlPathname]);
return (
<>
{isOpened() && (
)}
>
);
}
type NavigationSubItemProps = {
link: NavigationLink;
onLinkClick?: (event: MouseEvent) => void;
isOpened?: boolean;
};
function NavigationSubItem(props: NavigationSubItemProps) {
const [isOpened, setIsOpened] = createSignal(props.isOpened);
const pageContext = usePageContext();
const checkIsLinkActive = (link: NavigationLink) => {
return (
link.href === pageContext.urlPathname ||
link.subitems?.some((subitem) => subitem.href === pageContext.urlPathname)
);
};
createEffect(() => {
setIsOpened(checkIsLinkActive(props.link) || props.isOpened);
}, [pageContext.urlPathname, props.link]);
return (
<>
{props.link.subitems.length > 0 && (
)}
{props.link.title}
{props.link.subitems.length > 0 && (
({props.link.subitems.length})
)}
{props.link.subitems.length > 0 && isOpened() && (
{(subitem) => (
-
{subitem.title}
)}
)}
>
);
}
export function Navigation(props: {
class?: string;
onLinkClick?: (event: MouseEvent) => void;
}) {
const firstSections = navigation.filter(
(section) => section.position === "start",
);
const lastSections = navigation.filter(
(section) => section.position === "end",
);
const filteredSections = navigation
.filter(
(section) =>
section.position === "auto" || section.position === undefined,
)
.reduce(
(acc, section) => {
if (!acc[section.type]) {
acc[section.type] = [];
}
acc[section.type].push(section);
return acc;
},
{} as Record,
);
return (
);
}