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

100% Statements 18/18
70% Branches 7/10
100% Functions 9/9
100% Lines 14/14

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              1x       14x 14x             14x 26x         14x 2x     27x         8x 8x                 14x 29x 29x     18x    
import type { AppEntry } from "~/types/AppEntry";
import type { AppGroup } from "~/types/AppGroup";
import type { MenuRow } from "~/types/MenuRow";
import { menuLabel } from "~/utils/menuLabel";
import { matchPages, searchGroups } from "~/utils/search";
 
/** ⌘K search over the pages, the projects and the docs; the docs load on first open. */
export const useShellSearch = (
    apps: (AppGroup & { entries: AppEntry[] })[],
    projects: Ref<MenuRow[]> | undefined,
) => {
    const { t } = useI18n();
    const open = ref(false);
 
    // Deferred to the first open; built here it rides in every prerendered payload (../../README.md).
    const {
        data: sections,
        status,
        execute,
    } = useAsyncData("search-sections", () => queryCollectionSearchSections("docs"), {
        default: () => [],
        immediate: false,
        server: false,
    });
 
    watch(open, () => {
        Eif (open.value && status.value === "idle") void execute();
    });
 
    const projectGroup = computed(() => ({
        id: "projects",
        label: t("app.search.projects"),
        ignoreFilter: true,
        postFilter: matchPages,
        items: (projects?.value ?? []).flatMap((kind) =>
            (kind.children ?? []).map((row) => ({
                label: menuLabel(row, t),
                suffix: menuLabel(kind, t),
                to: row.to,
                icon: "i-lucide-box",
            })),
        ),
    }));
 
    const groups = computed(() => {
        const [pages, ...docs] = searchGroups(apps, sections.value, t);
        return [...(pages ? [pages] : []), projectGroup.value, ...docs];
    });
 
    return { open, groups, loading: computed(() => status.value === "pending") };
};