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

100% Statements 32/32
95.65% Branches 22/23
100% Functions 15/15
100% Lines 28/28

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        2x     2x 5x 5x 19x 5x       5x 5x         5x 5x   5x 5x   10x 10x 10x 10x 10x   10x                                             6x     5x 7x 16x           5x 28x 21x     27x   21x     5x    
import type { ProjectRow } from "#layers/projects/app/types/ProjectRow.ts";
 
type KindId = "all" | "apps" | "packages" | "infrastructure";
 
const KINDS = ["all", "apps", "packages", "infrastructure"] as const satisfies KindId[];
 
/** The catalog as table rows, narrowed by the `kind` query and a search; `ready` settles on load. */
export const useProjectList = () => {
    const route = useRoute();
    const { t } = useI18n();
    const filter = computed<KindId>(() => KINDS.find((kind) => kind === route.query.kind) ?? "all");
    const current = computed(() => ({
        id: filter.value,
        label: t(`projects.kinds.${filter.value}`),
    }));
    const crumbs = computed(() =>
        filter.value === "all"
            ? []
            : [{ label: t("projects.crumb"), to: "/projects" }, { label: filter.value }],
    );
 
    const search = ref("");
    const { projects, metricFor, readmeFor, ready } = useCatalog();
 
    const rows = computed(() =>
        projects
            .map((project): ProjectRow => {
                const metric = metricFor(project);
                const version = metric?.currentVersion ?? null;
                const unreleased = metric?.unreleasedCommits ?? 0;
                const lines = metric?.coverageLinesPct ?? null;
                const usedBy = project.dependedOnBy.length;
 
                return {
                    name: project.name,
                    root: project.root,
                    to: `/projects/${folderOf(project.root)}`,
                    kind: project.root.split("/")[0] ?? "",
                    description: readmeFor(project)?.description ?? "",
                    version,
                    unreleased,
                    recent: metric?.commitsLastTwoWeeks ?? null,
                    specs: metric?.specs ?? null,
                    lines,
                    linesLabel: dash(lines, "%"),
                    tone: toneClass(coverageTone(lines)),
                    usedBy,
                    compact: [
                        version,
                        unreleased ? t("projects.row.unreleased", { count: unreleased }) : null,
                        t("projects.row.usedBy", { count: usedBy }),
                    ]
                        .filter(Boolean)
                        .join(" ยท "),
                };
            })
            .toSorted((a, b) => a.root.localeCompare(b.root)),
    );
 
    const shown = computed(() =>
        rows.value.filter(
            (row) =>
                (filter.value === "all" || row.kind === filter.value) &&
                row.name.toLowerCase().includes(search.value.toLowerCase()),
        ),
    );
 
    const groups = computed(() =>
        KINDS.filter((kind) => kind !== "all")
            .map((kind) => ({
                id: kind,
                label: t(`projects.kinds.${kind}`),
                rows: shown.value.filter((row) => row.kind === kind),
            }))
            .filter((group) => group.rows.length > 0),
    );
 
    return { filter, current, crumbs, search, shown, groups, ready };
};