domain/loaders/reference.loader.ts

domain/loaders/reference.loader.ts is a file in Bane's Lab Site. 67 lines of code and 15 definitions.

import { ANATOMY_FACE, CHAPTER_FACE } from "#configuration/constants/vocabulary.constants";
import type { ReferenceContent, ReferenceRecord, ReferenceRelation, ReferenceTarget } from "#types/reference.types";
import { TOOLTIP_RELATIONS, chunkKeyOf } from "#configuration/constants/graph.constants";
import { chapterOf, payloadOfHref } from "#domain/converters/reference.converter";
import { ANCHOR_PREFIX } from "#configuration/constants/document.constants";
import { FACE_SEPARATOR } from "@govlab/constants";
import { loadPayload } from "#core/loaders/payload.loader";
import { loadReferences } from "#core/generated/reference.generated";

const fragmentOf = function fragmentOf(href: string): string {
    return href.slice(href.indexOf(ANCHOR_PREFIX) + ANCHOR_PREFIX.length);
};

const anatomyRecord = async function anatomyRecord(fragment: string): Promise<ReferenceRecord | null> {
    const [{ ANATOMY_INDEX }, { loadRecords }, { targetOf }] = await Promise.all([
        import("#core/generated/anatomy.index.generated"),
        import("#core/loaders/walk.loader"),
        import("#core/converters/folder.converter"),
    ]);
    const target = targetOf(`#${fragment}`);
    const records = await loadRecords(target === null ? undefined : ANATOMY_INDEX.nodes[target.id]);
    if (records === null || target === null) {
        return null;
    }
    return target.line === null ? records.node : (records.definitions[String(target.line)] ?? records.node);
};

const loadContent = async function loadContent(
    target: ReferenceTarget,
    href: string,
): Promise<ReferenceContent | null> {
    if (target.face === CHAPTER_FACE) {
        const chapter = chapterOf(await loadPayload(payloadOfHref(href)), fragmentOf(href));
        return chapter === null ? null : { chapter, kind: "chapter" };
    }
    if (target.face === ANATOMY_FACE) {
        const record = await anatomyRecord(fragmentOf(href));
        return record === null ? null : { kind: "record", record };
    }
    const index = await loadReferences(target.face);
    const record = index?.[target.ref];
    return record === undefined ? null : { kind: "record", record };
};

const graphRelations = async function graphRelations(ref: string): Promise<ReferenceRelation[]> {
    const { loadGraph } = await import("#core/generated/graph.generated");
    const node = (await loadGraph(chunkKeyOf(ref)))?.[ref];
    return (node?.relations ?? [])
        .filter((held) => TOOLTIP_RELATIONS.has(held.relation))
        .map((held) => ({
            edges: held.targets.map((target) => ({
                label: target.title,
                ref: target.href === null ? null : CHAPTER_FACE + FACE_SEPARATOR + target.href,
            })),
            relation: held.relation,
        }));
};

export const loadReference = async function loadReference(
    target: ReferenceTarget,
    href: string,
): Promise<ReferenceContent | null> {
    const content = await loadContent(target, href);
    if (content === null) {
        return null;
    }
    const relations = await graphRelations(target.ref);
    if (content.kind === "chapter") {
        return { chapter: { ...content.chapter, relations }, kind: "chapter" };
    }
    return { kind: "record", record: { ...content.record, relations: [...content.record.relations, ...relations] } };
};