All files / apps/huella-legal/app/composables useSiteNav.ts

97.29% Statements 36/37
95% Branches 19/20
100% Functions 13/13
100% Lines 30/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146                            2x                     2x                                     2x     2x   2x                                   7x   2x 14x 14x 14x 14x     14x   14x 7x   7x 49x   49x 49x   45x     7x 28x             28x 17x   28x                     14x 15x     70x                                   14x 6x               14x    
import type { FooterColumn, NavigationMenuItem } from "@nuxt/ui";
import type { AppConfig } from "nuxt/schema";
import type { RouteLocationRaw } from "vue-router";
 
type NavKey = keyof AppConfig["nav"];
type Current = "page" | "true" | undefined;
 
interface HeaderSection {
    key: string;
    to: NavKey;
    slot?: string;
    children?: readonly NavKey[];
}
 
const HEADER: readonly HeaderSection[] = [
    { key: "publications", to: "publications", slot: "publications" },
    { key: "subjects", to: "subjects", slot: "subjects" },
    {
        key: "journal",
        to: "about",
        children: ["about", "collaborators", "contact"],
    },
    { key: "publish", to: "publish" },
];
 
const COLUMNS = [
    {
        key: "journal",
        links: ["publications", "subjects", "collaborators", "about", "interestCalculator"],
    },
    {
        key: "participate",
        links: [
            "publish",
            "publishThesis",
            "publishGuide",
            "collaboratorPortal",
            "newsletter",
            "contact",
        ],
    },
    { key: "legal", links: ["legalNotice", "privacy", "cookies"] },
] as const satisfies readonly { key: string; links: readonly NavKey[] }[];
 
const UNPREFETCHED: ReadonlySet<NavKey> = new Set(["interestCalculator"]);
 
// The legacy layer forwards these to WordPress, so the client router must leave them to the server
const EXTERNAL: ReadonlySet<NavKey> = new Set(["collaboratorPortal"]);
 
const SOCIAL = [
    { key: "instagram", icon: "i-lucide-instagram" },
    { key: "linkedin", icon: "i-lucide-linkedin" },
    { key: "x", icon: "i-lucide-twitter" },
] as const;
 
interface CookiePreferences {
    enabled: boolean;
    open: () => unknown;
}
 
export interface SocialLink {
    key: string;
    icon: string;
    to: string;
    label: string;
}
 
const withTrailingSlash = (path: string): string => (path.endsWith("/") ? path : `${path}/`);
 
export const useSiteNav = (cookiePreferences: CookiePreferences) => {
    const { t } = useI18n();
    const route = useRoute();
    const router = useRouter();
    const { nav } = useAppConfig();
    const {
        public: { social: profiles },
    } = useRuntimeConfig();
 
    const sections = computed<NavigationMenuItem[]>(() => {
        const here = withTrailingSlash(route.path);
 
        const currentOf = (to: RouteLocationRaw): Current => {
            const { path, hash } = router.resolve(to);
 
            Iif (hash) return undefined;
            if (here === path) return "page";
 
            return here.startsWith(path) ? "true" : undefined;
        };
 
        return HEADER.map(({ key, to, slot, children }) => {
            const links = children?.map((child) => ({
                label: t(`app.header.nav.${child}`),
                description: t(`app.header.nav.descriptions.${child}`),
                to: nav[child],
                "aria-current": currentOf(nav[child]),
            }));
            const current: Current =
                currentOf(nav[to]) ??
                (links?.some((link) => link["aria-current"]) ? "true" : undefined);
 
            return {
                label: t(`app.header.nav.${key}`),
                to: nav[to],
                slot,
                children: links,
                active: !!current,
                "aria-current": current,
            };
        });
    });
 
    const columns = computed<FooterColumn[]>(() =>
        COLUMNS.map(({ key, links }) => ({
            label: t(`app.footer.columns.${key}`),
            children: [
                ...links.map((link) => ({
                    label: t(`app.footer.links.${link}`),
                    to: nav[link],
                    ...(UNPREFETCHED.has(link) && { prefetch: false }),
                    ...(EXTERNAL.has(link) && { external: true }),
                })),
                ...(key === "legal" && cookiePreferences.enabled
                    ? [
                          {
                              label: t("app.footer.links.cookieSettings"),
                              onClick: cookiePreferences.open,
                          },
                      ]
                    : []),
            ],
        })),
    );
 
    const social = computed<SocialLink[]>(() =>
        SOCIAL.filter(({ key }) => profiles[key]).map(({ key, icon }) => ({
            key,
            icon,
            to: profiles[key],
            label: t("app.footer.social.external", { network: t(`app.footer.social.${key}`) }),
        })),
    );
 
    return { sections, columns, social };
};