All files / apps/developer-portal/layers/projects/app/composables useProjectPage.ts

100% Statements 53/53
72% Branches 18/25
100% Functions 28/28
100% Lines 41/41

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          2x           2x 8x   8x 8x 8x                     8x 32x 8x     8x 8x 8x 29x     8x 8x 8x   16x     8x 8x 8x   8x 8x 9x     8x 7x   8x   7x       8x 1x 1x 1x     8x 14x       2x 1x     14x         14x             8x 70x               8x           7x                        
import { areaLabel } from "#layers/github/app/utils/issues.ts";
import type { PageTab } from "~/types/PageTab";
import type { ProjectTab } from "~/types/ProjectTab";
import type { ProjectTabRow } from "~/types/ProjectTabRow";
 
const BUILT_IN = ["readme", "coverage", "deps", "issues"];
 
/**
 * Everything a project page shows but its README, with the tabs other layers add through
 * `projectTabs`. Every read starts here and `ready` settles once the tabs' routes answer.
 */
export const useProjectPage = () => {
    const { t } = useI18n();
    const { projectTabs = {} }: { projectTabs?: Record<string, ProjectTab | undefined> } =
        useAppConfig();
    const registered = Object.entries(projectTabs).flatMap(([id, entry]) =>
        entry
            ? [
                  {
                      id,
                      ...entry,
                      label: t(`projectTabs.${id}.label`),
                      empty: t(`projectTabs.${id}.empty`),
                  },
              ]
            : [],
    );
    const all = [
        ...BUILT_IN.map((id) => ({ id, label: t(`project.tabs.${id}`) })),
        ...registered.map(({ id, label }) => ({ id, label })),
    ];
 
    const route = useRoute("projects-name");
    const name = computed(() => String(route.params.name));
    const tab = computed(() =>
        all.some((item) => item.id === route.query.tab) ? String(route.query.tab) : "readme",
    );
 
    const { shown: read } = useIssues();
    const extraReads = registered.map((entry) =>
        useFetch<Record<string, ProjectTabRow[]>>(entry.rows, {
            key: `project-tab-${entry.id}`,
            default: () => ({}),
        }),
    );
    const extraRows = extraReads.map((read) => read.data);
    const coverageSnapshot = coverageArtifact();
    const deps = depsArtifact();
 
    const projects = projectsArtifact()?.projects ?? [];
    const project = computed(() =>
        projects.find((candidate) => folderOf(candidate.root) === name.value),
    );
 
    const coverage = computed(() =>
        coverageSnapshot?.projects.find((item) => item.root === project.value?.root),
    );
    const outdated = computed(
        () =>
            deps?.outdated?.filter((item) => item.dependents.includes(project.value?.root ?? "")) ??
            [],
    );
 
    const extra = computed(() => {
        const index = registered.findIndex((entry) => entry.id === tab.value);
        const entry = registered[index];
        return entry ? { ...entry, items: extraRows[index]?.value[name.value] ?? [] } : null;
    });
 
    const area = computed(() => areaLabel(project.value?.root ?? `infrastructure/${name.value}`));
    const issues = computed(() => ({
        read: Boolean(read.value.fetchedAt),
        area: area.value,
        list: read.value.issues
            .filter((issue) => issue.labels.includes(area.value))
            .map((issue) => ({ ...issue, age: shortAge(issue.updatedAt, t) })),
    }));
 
    const counts = computed((): Partial<Record<string, number | string>> => ({
        coverage: coverage.value?.lines ? `${coverage.value.lines.pct}%` : undefined,
        deps: outdated.value.length,
        issues: issues.value.read ? issues.value.list.length : undefined,
        ...Object.fromEntries(
            registered.map((entry, index) => [
                entry.id,
                extraRows[index]?.value[name.value]?.length ?? 0,
            ]),
        ),
    }));
 
    const tabs = computed((): PageTab[] =>
        all.map((item) => ({
            label: item.label,
            to: item.id === "readme" ? route.path : `${route.path}?tab=${item.id}`,
            active: item.id === tab.value,
            count: counts.value[item.id],
        })),
    );
 
    return {
        name,
        tab,
        tabs,
        project,
        metric: computed(() =>
            metricsArtifact()?.projects.find((item) => item.name === project.value?.name),
        ),
        projects,
        coverage,
        coverageAge: relativeTime(coverageSnapshot?.generatedAt, t),
        outdated,
        depsAge: relativeTime(deps?.generatedAt, t),
        extra,
        issues,
        ready: Promise.all(extraReads),
    };
};