From 1fb8ddc0a3482390f6b33886c311641d300ce806 Mon Sep 17 00:00:00 2001 From: GauthierWebDev Date: Mon, 21 Apr 2025 15:18:42 +0200 Subject: [PATCH] refactor: Improve sorting logic for navigation links --- app/components/PrevNextLinks.tsx | 19 +++++++++++ app/layouts/DocsLayout.tsx | 16 +++++---- app/partials/Navigation.tsx | 57 ++++++++++++++++++-------------- 3 files changed, 61 insertions(+), 31 deletions(-) diff --git a/app/components/PrevNextLinks.tsx b/app/components/PrevNextLinks.tsx index cda97ea..f166b5a 100644 --- a/app/components/PrevNextLinks.tsx +++ b/app/components/PrevNextLinks.tsx @@ -68,6 +68,25 @@ export function PrevNextLinks() { const pageContext = usePageContext(); const allLinks = navigation + .sort((a, b) => { + // positions order (for sorting): + // 1. start + // 2. auto | undefined + // 3. end + + if (a.position === "start" && b.position !== "start") return -1; + if (a.position !== "start" && b.position === "start") return 1; + + if (a.position === "end" && b.position !== "end") return 1; + if (a.position !== "end" && b.position === "end") return -1; + + if (a.position === "auto" && b.position !== "auto") return -1; + if (a.position !== "auto" && b.position === "auto") return 1; + + if (a.position === undefined && b.position !== undefined) return -1; + if (a.position !== undefined && b.position === undefined) return 1; + return 0; + }) .flatMap((section) => section.links) .flatMap((link) => { return link.subitems ? [link, ...link.subitems] : link; diff --git a/app/layouts/DocsLayout.tsx b/app/layouts/DocsLayout.tsx index 0f2f261..30ca027 100644 --- a/app/layouts/DocsLayout.tsx +++ b/app/layouts/DocsLayout.tsx @@ -44,21 +44,23 @@ type DocsHeaderProps = { }; export function DocsHeader(props: DocsHeaderProps) { - const { urlPathname } = usePageContext(); + const pageContext = usePageContext(); - const section = navigation.find((section) => - section.links.find((link) => link.href === urlPathname), - ); + const getSection = () => { + return navigation.find((section) => + section.links.find((link) => link.href === pageContext.urlPathname), + ); + }; - if (!props.title && !section) { + if (!props.title && !getSection()) { return null; } return (
- {section && ( + {getSection() && (

- {section.title} + {getSection()?.title}

)} {props.title && ( diff --git a/app/partials/Navigation.tsx b/app/partials/Navigation.tsx index 95365f5..0232d1a 100644 --- a/app/partials/Navigation.tsx +++ b/app/partials/Navigation.tsx @@ -1,3 +1,5 @@ +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"; @@ -14,22 +16,32 @@ type NavigationItemProps = { function NavigationItem(props: NavigationItemProps) { const pageContext = usePageContext(); - const [isOpened, setIsOpened] = createSignal( - props.section.links.some( - (link) => - link.href === pageContext.urlPathname || - link.subitems?.some( - (subitem) => subitem.href === pageContext.urlPathname, - ), - ), - ); + 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()); + + 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 ( <>