All files / src/views confirm-pull-modal.ts

100% Statements 31/31
100% Branches 8/8
100% Functions 7/7
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                            12x 12x       12x 12x 12x 12x   12x       12x 12x 12x   12x   12x 12x 12x 12x   12x 12x 2x 2x         12x 12x       12x   12x 12x       6x       9x 4x             4x      
import { App, Modal } from "obsidian";
import { MESSAGES } from "../locales/en";
import { bindEscapeToClose } from "../utils";
 
export interface ConfirmPullInfo {
    displayName: string;
    sizeGb: number;
    minRamGb: number;
    /** Total system RAM in GB. When set and below ``minRamGb`` the modal warns the user. */
    systemMemGb?: number | null;
}
 
export class ConfirmPullModal extends Modal {
    private model: ConfirmPullInfo;
    private _resolve: ((value: boolean) => void) | null = null;
    private decided = false;
    readonly result: Promise<boolean>;
 
    constructor(app: App, model: ConfirmPullInfo) {
        super(app);
        this.model = model;
        this.result = new Promise<boolean>((resolve) => {
            this._resolve = resolve;
        });
        bindEscapeToClose(this);
    }
 
    onOpen(): void {
        const { contentEl } = this;
        contentEl.empty();
        contentEl.addClass("lilbee-confirm-pull");
 
        contentEl.createEl("h2", { text: MESSAGES.TITLE_DOWNLOAD_MODEL });
 
        const info = contentEl.createDiv({ cls: "lilbee-confirm-pull-info" });
        info.createEl("p", { text: `${MESSAGES.LABEL_MODEL}: ${this.model.displayName}` });
        info.createEl("p", { text: `${MESSAGES.LABEL_SIZE}: ${this.model.sizeGb} GB` });
        info.createEl("p", { text: `${MESSAGES.LABEL_MIN_RAM}: ${this.model.minRamGb} GB` });
 
        const tooLarge = typeof this.model.systemMemGb === "number" && this.model.systemMemGb < this.model.minRamGb;
        if (tooLarge) {
            const warn = contentEl.createDiv({ cls: "lilbee-confirm-pull-warning" });
            warn.createEl("p", {
                text: MESSAGES.WARNING_MODEL_EXCEEDS_RAM(this.model.minRamGb, this.model.systemMemGb!),
            });
        }
 
        const actions = contentEl.createDiv({ cls: "lilbee-confirm-pull-actions" });
        const pullBtn = actions.createEl("button", {
            text: tooLarge ? MESSAGES.BUTTON_PULL_ANYWAY : MESSAGES.BUTTON_PULL_MODEL,
            cls: "mod-cta",
        });
        pullBtn.addEventListener("click", () => this.decide(true));
 
        const cancelBtn = actions.createEl("button", { text: MESSAGES.BUTTON_CANCEL });
        cancelBtn.addEventListener("click", () => this.decide(false));
    }
 
    onClose(): void {
        this.decide(false);
    }
 
    private decide(confirmed: boolean): void {
        if (this.decided) return;
        this.decided = true;
        /* v8 ignore next -- `decided` guards re-entry, so `_resolve` is always set here */
        if (this._resolve) {
            const resolve = this._resolve;
            this._resolve = null;
            resolve(confirmed);
        }
        this.close();
    }
}