All files / src/views draft-modal.ts

100% Statements 235/235
100% Branches 88/88
100% Functions 16/16
100% Lines 235/235

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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 2641x             1x   1x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x   33x 33x 33x 33x 33x   33x 33x 33x 33x 33x 33x 33x 33x   33x 1x 1x   33x 33x 33x 33x 33x 33x 33x 33x   33x 33x 33x   33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x   33x 35x 34x 34x 34x 34x 34x 34x 34x 34x 35x 1x 1x 1x 1x 1x 1x 1x 1x 33x 33x 35x   33x 40x 40x 40x 9x 9x 9x 9x 9x 9x 40x 36x 36x 40x   33x 36x 36x   36x 36x 36x 34x 34x 36x 35x 35x 35x 35x 35x 36x 36x 1x 35x 36x 36x 36x 36x 36x   36x 36x   33x 21x 20x 21x 3x 3x 20x 20x 20x 20x 20x 20x 20x 21x 2x 1x 1x 1x 1x 18x 17x 17x 17x 21x   33x 17x 17x 1x 1x 1x 17x 59x 59x 17x   33x 9x 9x 7x 7x 7x 7x 7x 9x 1x 1x 1x 6x 5x 5x 9x 1x 1x 9x 7x 7x 9x   33x 5x 5x 4x 4x 4x 4x 4x 5x 1x 1x 1x 3x 2x 2x 5x 1x 1x 5x 4x 4x 5x   33x 7x 7x 7x 7x 7x 7x 7x 7x 1x 1x 7x 5x 5x 5x 7x   33x 66x 66x 66x 33x     1x 1x 1x 1x 1x 1x 1x   36x 36x 34x 34x 34x 36x 2x 2x   59x 59x 59x 59x 59x 17x 17x  
import { App, Modal, Notice } from "obsidian";
import type LilbeePlugin from "../main";
import { DRAFT_PENDING_KIND, type DraftInfoResponse, type DraftPendingKind } from "../types";
import { MESSAGES } from "../locales/en";
import { ConfirmModal } from "./confirm-modal";
import { bindEscapeToClose } from "../utils";
 
const NOTICE_DURATION_MS = 4000;
 
export class DraftModal extends Modal {
    private plugin: LilbeePlugin;
    private drafts: DraftInfoResponse[] = [];
    private selectedSlug: string | null = null;
    private selectedRow: HTMLElement | null = null;
    private titleEl!: HTMLElement;
    private listEl!: HTMLElement;
    private diffEl!: HTMLElement;
    private acceptBtn!: HTMLButtonElement;
    private rejectBtn!: HTMLButtonElement;
    private actionInFlight = false;
 
    constructor(app: App, plugin: LilbeePlugin) {
        super(app);
        this.plugin = plugin;
        bindEscapeToClose(this);
    }
 
    onOpen(): void {
        const { contentEl } = this;
        contentEl.empty();
        contentEl.addClass("lilbee-modal");
        contentEl.addClass("lilbee-drafts-modal");
        this.renderChrome(contentEl);
        void this.loadList();
    }
 
    onClose(): void {
        this.contentEl.empty();
    }
 
    private renderChrome(root: HTMLElement): void {
        const header = root.createDiv({ cls: "lilbee-drafts-header" });
        this.titleEl = header.createEl("h2", { text: MESSAGES.TITLE_DRAFTS(0) });
        const refreshBtn = header.createEl("button", {
            text: MESSAGES.LABEL_DRAFT_REFRESH,
            cls: "lilbee-drafts-refresh",
        });
        refreshBtn.addEventListener("click", () => void this.loadList());
 
        this.listEl = root.createDiv({ cls: "lilbee-drafts-list" });
        this.diffEl = root.createEl("pre", { cls: "lilbee-draft-diff" });
        this.diffEl.setText(MESSAGES.LABEL_DRAFT_NO_SELECTION);
 
        const actions = root.createDiv({ cls: "lilbee-drafts-actions" });
        this.acceptBtn = actions.createEl("button", {
            text: MESSAGES.LABEL_DRAFT_ACCEPT,
            cls: "mod-cta",
        }) as HTMLButtonElement;
        this.rejectBtn = actions.createEl("button", {
            text: MESSAGES.LABEL_DRAFT_REJECT,
        }) as HTMLButtonElement;
        this.acceptBtn.disabled = true;
        this.rejectBtn.disabled = true;
        this.acceptBtn.addEventListener("click", () => void this.accept());
        this.rejectBtn.addEventListener("click", () => void this.reject());
    }
 
    private async loadList(): Promise<void> {
        if (this.actionInFlight) return;
        this.selectedSlug = null;
        this.selectedRow = null;
        this.setActionsEnabled(false);
        this.diffEl.setText(MESSAGES.LABEL_DRAFT_NO_SELECTION);
        this.listEl.empty();
        const loading = this.listEl.createDiv({ cls: "lilbee-loading" });
        try {
            this.drafts = await this.plugin.api.wikiDrafts();
        } catch {
            loading.remove();
            this.titleEl.setText(MESSAGES.TITLE_DRAFTS(0));
            this.listEl.createEl("p", {
                text: MESSAGES.ERROR_LOAD_DRAFTS,
                cls: "lilbee-empty-state",
            });
            return;
        }
        loading.remove();
        this.renderRows();
    }
 
    private renderRows(): void {
        this.titleEl.setText(MESSAGES.TITLE_DRAFTS(this.drafts.length));
        this.listEl.empty();
        if (this.drafts.length === 0) {
            this.listEl.createEl("p", {
                text: MESSAGES.LABEL_NO_DRAFTS,
                cls: "lilbee-empty-state",
            });
            return;
        }
        for (const draft of this.drafts) {
            this.renderRow(draft);
        }
    }
 
    private renderRow(draft: DraftInfoResponse): void {
        const row = this.listEl.createDiv({ cls: "lilbee-draft-row" });
        row.createEl("strong", { text: draft.slug, cls: "lilbee-draft-slug" });
 
        const meta = row.createDiv({ cls: "lilbee-draft-meta" });
        const kindLabelText = kindChipLabel(draft);
        if (kindLabelText !== null) {
            meta.createEl("span", { text: kindLabelText, cls: "lilbee-draft-kind" });
        }
        if (draft.drift_ratio !== null) {
            meta.createEl("span", {
                text: MESSAGES.LABEL_DRAFT_DRIFT(Math.round(draft.drift_ratio * 100)),
                cls: "lilbee-draft-drift",
            });
        }
        const faithText =
            draft.faithfulness_score === null
                ? MESSAGES.LABEL_DRAFT_FAITH_NA
                : MESSAGES.LABEL_DRAFT_FAITH(draft.faithfulness_score);
        meta.createEl("span", { text: faithText, cls: "lilbee-draft-faith" });
        meta.createEl("span", {
            text: draft.published_exists ? MESSAGES.LABEL_DRAFT_PUBLISHED : MESSAGES.LABEL_DRAFT_NEW,
            cls: "lilbee-draft-pub",
        });
 
        row.addEventListener("click", () => void this.selectDraft(draft.slug, row));
    }
 
    private async selectDraft(slug: string, row: HTMLElement): Promise<void> {
        if (this.actionInFlight) return;
        this.selectedSlug = slug;
        if (this.selectedRow !== null && this.selectedRow !== row) {
            this.selectedRow.removeClass("is-selected");
        }
        this.selectedRow = row;
        row.addClass("is-selected");
        this.diffEl.empty();
        const loading = this.diffEl.createEl("span", { text: "…", cls: "lilbee-loading" });
        let diffText: string;
        try {
            diffText = await this.plugin.api.wikiDraftDiff(slug);
        } catch {
            if (this.selectedSlug !== slug) return;
            loading.remove();
            this.diffEl.setText(MESSAGES.ERROR_LOAD_DIFF);
            return;
        }
        if (this.selectedSlug !== slug) return;
        loading.remove();
        this.renderDiff(diffText);
        this.setActionsEnabled(true);
    }
 
    private renderDiff(text: string): void {
        this.diffEl.empty();
        if (text.trim() === "") {
            this.diffEl.setText(MESSAGES.LABEL_DRAFT_NO_DIFF);
            return;
        }
        for (const line of text.split("\n")) {
            this.diffEl.createEl("span", { text: `${line}\n`, cls: diffLineClass(line) });
        }
    }
 
    private async accept(): Promise<void> {
        const slug = this.selectedSlug;
        if (slug === null || this.actionInFlight) return;
        this.actionInFlight = true;
        this.setActionsEnabled(false);
        try {
            const confirm = new ConfirmModal(this.app, MESSAGES.NOTICE_CONFIRM_DRAFT_ACCEPT(slug));
            confirm.open();
            if (!(await confirm.result)) {
                this.setActionsEnabled(true);
                return;
            }
            const result = await this.plugin.api.wikiDraftAccept(slug);
            this.afterActionSuccess(slug, true);
            new Notice(MESSAGES.NOTICE_DRAFT_ACCEPTED(result.slug, result.reindexed_chunks), NOTICE_DURATION_MS);
        } catch {
            new Notice(MESSAGES.NOTICE_DRAFT_ACTION_FAILED, NOTICE_DURATION_MS);
            this.setActionsEnabled(true);
        } finally {
            this.actionInFlight = false;
        }
    }
 
    private async reject(): Promise<void> {
        const slug = this.selectedSlug;
        if (slug === null || this.actionInFlight) return;
        this.actionInFlight = true;
        this.setActionsEnabled(false);
        try {
            const confirm = new ConfirmModal(this.app, MESSAGES.NOTICE_CONFIRM_DRAFT_REJECT(slug));
            confirm.open();
            if (!(await confirm.result)) {
                this.setActionsEnabled(true);
                return;
            }
            await this.plugin.api.wikiDraftReject(slug);
            this.afterActionSuccess(slug, false);
            new Notice(MESSAGES.NOTICE_DRAFT_REJECTED(slug), NOTICE_DURATION_MS);
        } catch {
            new Notice(MESSAGES.NOTICE_DRAFT_ACTION_FAILED, NOTICE_DURATION_MS);
            this.setActionsEnabled(true);
        } finally {
            this.actionInFlight = false;
        }
    }
 
    private afterActionSuccess(slug: string, wasAccept: boolean): void {
        const removed = this.drafts.find((d) => d.slug === slug);
        this.drafts = this.drafts.filter((d) => d.slug !== slug);
        this.selectedSlug = null;
        this.selectedRow = null;
        this.diffEl.setText(MESSAGES.LABEL_DRAFT_NO_SELECTION);
        this.renderRows();
        if (this.plugin.wikiDraftCount > 0) this.plugin.wikiDraftCount -= 1;
        if (wasAccept && removed && !removed.published_exists) {
            this.plugin.wikiPageCount += 1;
        }
        if (wasAccept) {
            this.plugin.refreshOpenWikiViews();
            void this.plugin.reconcileWiki();
        }
    }
 
    private setActionsEnabled(enabled: boolean): void {
        this.acceptBtn.disabled = !enabled;
        this.rejectBtn.disabled = !enabled;
    }
}
 
// Exhaustive map: adding a new DraftPendingKind variant forces TS to demand a label here.
const KIND_LABELS: Record<DraftPendingKind, string> = {
    [DRAFT_PENDING_KIND.DRIFT]: MESSAGES.LABEL_DRAFT_KIND_DRIFT,
    [DRAFT_PENDING_KIND.PARSE]: MESSAGES.LABEL_DRAFT_KIND_PARSE,
    [DRAFT_PENDING_KIND.COLLISION]: MESSAGES.LABEL_DRAFT_KIND_COLLISION,
    [DRAFT_PENDING_KIND.LOW_FAITHFULNESS]: MESSAGES.LABEL_DRAFT_KIND_LOW_FAITH,
    [DRAFT_PENDING_KIND.BAD_TITLE]: MESSAGES.LABEL_DRAFT_KIND_BAD_TITLE,
};
 
function kindChipLabel(draft: DraftInfoResponse): string | null {
    if (draft.pending_kind !== null) {
        const label = KIND_LABELS[draft.pending_kind];
        if (label !== undefined) return label;
    }
    if (draft.bad_title) return MESSAGES.LABEL_DRAFT_KIND_BAD_TITLE;
    return null;
}
 
function diffLineClass(line: string): string {
    if (line.startsWith("+++") || line.startsWith("---")) return "lilbee-draft-diff-meta";
    if (line.startsWith("@@")) return "lilbee-draft-diff-hunk";
    if (line.startsWith("+")) return "lilbee-draft-diff-add";
    if (line.startsWith("-")) return "lilbee-draft-diff-del";
    return "lilbee-draft-diff-ctx";
}