All files / apps/developer-portal/app/composables useRailEntries.ts

100% Statements 27/27
100% Branches 18/18
100% Functions 10/10
100% Lines 23/23

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                1x     14x 14x 14x 14x   14x 14x 182x 56x   104x         15x   14x   22x                     804x 22x   14x 67x 335x 335x   335x                   804x   804x                               14x    
import type { NavigationMenuItem } from "@nuxt/ui";
import type { ComputedRef } from "vue";
import { appEntries, locate } from "~/utils/apps";
import { menuLabel } from "~/utils/menuLabel";
import type { MenuRow } from "~/types/MenuRow";
import type { NavLink } from "~/types/NavLink";
 
/** The rail's nav tree: each app, its entries with their badges, and the rows each `menu` route answers. */
export const useRailEntries = (
    figures: ComputedRef<Record<string, { badge?: { label: string; class: string } }>>,
) => {
    const { nav }: { nav: Record<string, NavLink | undefined> } = useAppConfig();
    const { t } = useI18n();
    const apps = appEntries(nav, t);
    const route = useRoute();
 
    const menus: Record<string, Ref<MenuRow[]>> = {};
    for (const [key, link] of Object.entries(nav)) {
        if (link?.menu) {
            menus[key] = useFetch<MenuRow[]>(link.menu, {
                key: `menu-${key}`,
                default: () => [],
            }).data;
        }
    }
 
    const here = computed(() => locate(apps, route.path));
 
    const holds = (row: MenuRow): boolean => row.to === route.fullPath || row.to === route.path;
 
    const toItem = (row: MenuRow): NavigationMenuItem => ({
        label: menuLabel(row, t),
        icon: row.icon,
        to: row.to,
        active: row.to === route.fullPath,
        badge:
            row.badge === undefined
                ? undefined
                : { label: row.badge, class: toneClass(coverageTone(row.coverage)) },
    });
 
    const menuSections = (rows: MenuRow[]): NavigationMenuItem[] =>
        (rows.find((row) => (row.children ?? []).some(holds))?.children ?? rows).map(toItem);
 
    const items = computed((): NavigationMenuItem[] =>
        apps.map((app) => {
            const [first] = app.entries;
            const single = app.entries.length === 1;
 
            return {
                label: app.label,
                icon: app.icon,
                to: first?.to,
                value: app.id,
                active: here.value?.app.id === app.id,
                defaultOpen: here.value?.app.id === app.id,
                children: single
                    ? undefined
                    : app.entries.map((entry) => {
                          const children = menuSections(menus[entry.key]?.value ?? []);
 
                          return {
                              label: entry.label,
                              icon: entry.icon,
                              to: entry.to,
                              value: entry.key,
                              active: here.value?.entry.key === entry.key,
                              defaultOpen:
                                  here.value?.entry.key === entry.key && route.path !== entry.to,
                              badge: figures.value[entry.key]?.badge,
                              children: children.length === 0 ? undefined : children,
                          };
                      }),
            };
        }),
    );
 
    return { apps, menus, items };
};