All files / src/views lint-modal.ts

100% Statements 27/27
100% Branches 4/4
100% Functions 4/4
100% Lines 27/27

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          2x               2x                       11x 11x 11x       11x 11x 11x   11x     11x 11x 17x 17x 17x       11x         11x 1x     10x 14x         1x       14x 14x   14x 17x   17x         17x         17x              
import { App, Modal } from "obsidian";
import type { LintIssue } from "../types";
import { MESSAGES } from "../locales/en";
import { bindEscapeToClose } from "../utils";
 
const STATUS_CLASSES: Record<LintIssue["status"], string> = {
    valid: "lilbee-lint-valid",
    stale_hash: "lilbee-lint-stale",
    source_deleted: "lilbee-lint-deleted",
    excerpt_missing: "lilbee-lint-missing",
    model_changed: "lilbee-lint-model",
};
 
const STATUS_LABELS: Record<LintIssue["status"], string> = {
    valid: MESSAGES.LABEL_LINT_STATUS_VALID,
    stale_hash: MESSAGES.LABEL_LINT_STATUS_STALE,
    source_deleted: MESSAGES.LABEL_LINT_STATUS_DELETED,
    excerpt_missing: MESSAGES.LABEL_LINT_STATUS_MISSING,
    model_changed: MESSAGES.LABEL_LINT_STATUS_MODEL,
};
 
export class LintModal extends Modal {
    private issues: LintIssue[];
 
    constructor(app: App, issues: LintIssue[]) {
        super(app);
        this.issues = issues;
        bindEscapeToClose(this);
    }
 
    onOpen(): void {
        const { contentEl } = this;
        contentEl.empty();
        contentEl.addClass("lilbee-modal");
 
        contentEl.createEl("h2", { text: MESSAGES.TITLE_LINT_RESULTS });
 
        // Group by wiki page
        const grouped = new Map<string, LintIssue[]>();
        for (const issue of this.issues) {
            const list = grouped.get(issue.wiki_page) ?? [];
            list.push(issue);
            grouped.set(issue.wiki_page, list);
        }
 
        // Summary line
        contentEl.createEl("p", {
            text: MESSAGES.LABEL_LINT_ISSUES(this.issues.length, grouped.size),
            cls: "lilbee-lint-summary",
        });
 
        if (this.issues.length === 0) {
            return;
        }
 
        for (const [page, issues] of grouped) {
            this.renderGroup(contentEl, page, issues);
        }
    }
 
    onClose(): void {
        this.contentEl.empty();
    }
 
    private renderGroup(container: HTMLElement, page: string, issues: LintIssue[]): void {
        const group = container.createDiv({ cls: "lilbee-lint-group" });
        group.createEl("h3", { text: page });
 
        for (const issue of issues) {
            const row = group.createDiv({ cls: "lilbee-lint-issue" });
 
            row.createEl("span", {
                text: issue.citation_key,
                cls: "lilbee-citation-key",
            });
 
            row.createEl("span", {
                text: STATUS_LABELS[issue.status],
                cls: `lilbee-lint-status ${STATUS_CLASSES[issue.status]}`,
            });
 
            row.createEl("span", {
                text: issue.detail,
                cls: "lilbee-lint-detail",
            });
        }
    }
}