All files / src/views task-center.ts

100% Statements 201/201
100% Branches 110/110
100% Functions 30/30
100% Lines 181/181

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 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332                                3x   3x   3x       65x 65x 65x 65x 65x 65x 65x 65x     65x 65x       1x       1x       1x       54x 54x 54x   54x 54x   54x 54x 54x   54x 54x 54x 1x 1x     54x   54x 54x   54x 54x   54x 54x     83x 54x   54x       55x 55x 54x 54x         181x   180x 180x 180x 180x 180x 180x       234x 234x 234x   122x 68x   122x 122x       181x 180x 180x 180x 180x               181x 180x 81x   180x 180x 3x           3x   177x 177x         181x 180x 180x 180x   180x   180x 102x 102x 102x     78x 81x         181x 180x 180x 180x   180x   180x 177x 177x 177x     3x 3x         181x 180x 180x 180x   180x   180x 133x 133x 133x     47x 47x         131x 131x 131x   131x   131x       131x 131x 131x   131x 40x     131x   131x   131x 131x 131x 131x 131x 131x   131x 131x 131x 131x 131x   131x 131x 131x 131x   131x 1x 1x 1x 1x 1x 1x       131x 4x 4x 4x 4x 1x 1x       131x 10x         4x 2x 2x     2x 2x 2x   2x 1x           131x 81x   50x 3x   47x 25x   22x 10x   12x 4x   8x       131x 50x 44x 34x   81x 81x 3x 78x 2x   81x 2x   81x 76x       47x 43x    
import { ItemView, WorkspaceLeaf } from "obsidian";
import type LilbeePlugin from "../main";
import {
    BACKGROUND_TASK_TYPES,
    INDETERMINATE_PROGRESS,
    TASK_QUEUE,
    TASK_STATUS,
    type TaskEntry,
    type TaskStatus,
} from "../types";
import { addCloseButton } from "../components/close-button";
import { MESSAGES } from "../locales/en";
import { ConfirmModal } from "./confirm-modal";
import { FLASH_WINDOW_MS } from "../task-queue";
import { formatBytes, formatElapsed, formatRate, relativeTime, TIME_REFRESH_INTERVAL_MS } from "../utils";
 
const ACTIVE_PCT_CAP = 99;
 
export const VIEW_TYPE_TASKS = "lilbee-tasks";
 
const ACTIVE_REFRESH_INTERVAL_MS = 1000;
 
export class TaskCenterView extends ItemView {
    private plugin: LilbeePlugin;
    private unsubscribe: (() => void) | null = null;
    private countersEl: HTMLElement | null = null;
    private capPill: HTMLElement | null = null;
    private activeSection: HTMLElement | null = null;
    private queuedSection: HTMLElement | null = null;
    private completedSection: HTMLElement | null = null;
    private refreshInterval: number | null = null;
    private refreshIntervalMs = TIME_REFRESH_INTERVAL_MS;
 
    constructor(leaf: WorkspaceLeaf, plugin: LilbeePlugin) {
        super(leaf);
        this.plugin = plugin;
    }
 
    getViewType(): string {
        return VIEW_TYPE_TASKS;
    }
 
    getDisplayText(): string {
        return MESSAGES.LABEL_TASKS_VIEW;
    }
 
    getIcon(): string {
        return "list-checks";
    }
 
    async onOpen(): Promise<void> {
        const { contentEl } = this;
        contentEl.empty();
        contentEl.addClass("lilbee-tasks-container");
 
        const header = contentEl.createDiv({ cls: "lilbee-tasks-header" });
        header.createEl("h2", { text: MESSAGES.LABEL_TASK_CENTER });
 
        this.countersEl = header.createSpan({ cls: "lilbee-tasks-counters" });
        this.capPill = header.createSpan({ cls: "lilbee-tasks-cap-pill" });
        this.capPill.hide();
 
        const clearBtn = header.createEl("button", { cls: "lilbee-tasks-clear" });
        clearBtn.setText(MESSAGES.BUTTON_CLEAR_TASKS);
        clearBtn.addEventListener("click", () => {
            this.plugin.taskQueue.clearHistory();
            this.render();
        });
 
        addCloseButton(header, this.leaf, MESSAGES.LABEL_CLOSE_VIEW(MESSAGES.LABEL_TASKS_VIEW));
 
        this.activeSection = contentEl.createDiv({ cls: "lilbee-tasks-section" });
        this.activeSection.createDiv({ cls: "lilbee-tasks-section-header" }).setText(MESSAGES.LABEL_ACTIVE_TASKS);
 
        this.queuedSection = contentEl.createDiv({ cls: "lilbee-tasks-section" });
        this.queuedSection.createDiv({ cls: "lilbee-tasks-section-header" }).setText(MESSAGES.LABEL_QUEUED_TASKS);
 
        this.completedSection = contentEl.createDiv({ cls: "lilbee-tasks-section" });
        this.completedSection.createDiv({ cls: "lilbee-tasks-section-header" }).textContent =
            MESSAGES.LABEL_COMPLETED_TASKS;
 
        this.unsubscribe = this.plugin.taskQueue.onChange(() => this.render());
        this.retuneRefreshInterval();
 
        this.render();
    }
 
    async onClose(): Promise<void> {
        this.unsubscribe?.();
        if (this.refreshInterval !== null) {
            window.clearInterval(this.refreshInterval);
            this.refreshInterval = null;
        }
    }
 
    private render(): void {
        if (!this.activeSection || !this.queuedSection || !this.completedSection) return;
 
        this.renderCounters();
        this.renderCapPill();
        this.renderActive();
        this.renderQueued();
        this.renderCompleted();
        this.retuneRefreshInterval();
    }
 
    private retuneRefreshInterval(): void {
        const hasActive = this.plugin.taskQueue.activeAll.length > 0;
        const targetMs = hasActive ? ACTIVE_REFRESH_INTERVAL_MS : TIME_REFRESH_INTERVAL_MS;
        if (targetMs === this.refreshIntervalMs && this.refreshInterval !== null) return;
 
        if (this.refreshInterval !== null) {
            window.clearInterval(this.refreshInterval);
        }
        this.refreshIntervalMs = targetMs;
        this.refreshInterval = window.setInterval(() => this.render(), targetMs);
    }
 
    private renderCounters(): void {
        if (!this.countersEl) return;
        const active = this.plugin.taskQueue.activeAll.length;
        const queued = this.plugin.taskQueue.queued.length;
        const done = this.plugin.taskQueue.completed.length;
        this.countersEl.setText(
            MESSAGES.LABEL_TASK_COUNTERS.replace("{active}", String(active))
                .replace("{queued}", String(queued))
                .replace("{done}", String(done)),
        );
    }
 
    private renderCapPill(): void {
        if (!this.capPill) return;
        const backgroundActive = this.plugin.taskQueue.activeAll.filter((t) =>
            BACKGROUND_TASK_TYPES.has(t.type),
        ).length;
        const cap = TASK_QUEUE.MAX_CONCURRENT_BACKGROUND;
        if (backgroundActive >= cap) {
            this.capPill.setText(
                MESSAGES.LABEL_TASK_CAP_PILL.replace("{active}", String(backgroundActive)).replace(
                    "{cap}",
                    String(cap),
                ),
            );
            this.capPill.show();
        } else {
            this.capPill.setText("");
            this.capPill.hide();
        }
    }
 
    private renderActive(): void {
        if (!this.activeSection) return;
        const container = this.activeSection;
        container.querySelectorAll(".lilbee-task-row").forEach((el) => el.remove());
        container.querySelectorAll(".lilbee-tasks-empty").forEach((el) => el.remove());
 
        const allActive = this.plugin.taskQueue.activeAll;
 
        if (allActive.length === 0) {
            const empty = container.createDiv({ cls: "lilbee-tasks-empty" });
            empty.setText(MESSAGES.LABEL_NO_ACTIVE_TASKS);
            return;
        }
 
        for (const task of allActive) {
            this.renderTaskRow(container, task, TASK_STATUS.ACTIVE);
        }
    }
 
    private renderQueued(): void {
        if (!this.queuedSection) return;
        const container = this.queuedSection;
        container.querySelectorAll(".lilbee-task-row").forEach((el) => el.remove());
        container.querySelectorAll(".lilbee-tasks-empty").forEach((el) => el.remove());
 
        const queued = this.plugin.taskQueue.queued;
 
        if (queued.length === 0) {
            const empty = container.createDiv({ cls: "lilbee-tasks-empty" });
            empty.setText(MESSAGES.LABEL_NO_QUEUED_TASKS);
            return;
        }
 
        for (const task of queued) {
            this.renderTaskRow(container, task, TASK_STATUS.QUEUED);
        }
    }
 
    private renderCompleted(): void {
        if (!this.completedSection) return;
        const container = this.completedSection;
        container.querySelectorAll(".lilbee-task-row").forEach((el) => el.remove());
        container.querySelectorAll(".lilbee-tasks-empty").forEach((el) => el.remove());
 
        const completed = this.plugin.taskQueue.completed;
 
        if (completed.length === 0) {
            const empty = container.createDiv({ cls: "lilbee-tasks-empty" });
            empty.setText(MESSAGES.LABEL_NO_COMPLETED_TASKS);
            return;
        }
 
        for (const task of completed) {
            this.renderTaskRow(container, task, task.status);
        }
    }
 
    private renderTaskRow(container: HTMLElement, task: TaskEntry, state: TaskStatus): void {
        const row = container.createDiv({ cls: "lilbee-task-row" });
        row.dataset.state = state;
        row.dataset.type = task.type;
 
        const isActive = state === TASK_STATUS.ACTIVE;
        const isTerminal =
            state === TASK_STATUS.DONE ||
            state === TASK_STATUS.FAILED ||
            state === TASK_STATUS.CANCELLED ||
            state === TASK_STATUS.WAITING;
        const isIndeterminate = isActive && task.progress <= INDETERMINATE_PROGRESS;
        const rawPct = Math.max(0, Math.min(100, task.progress));
        const pct = isIndeterminate ? 100 : isActive ? Math.min(rawPct, ACTIVE_PCT_CAP) : rawPct;
 
        if (isTerminal && isWithinFlashWindow(task)) {
            row.addClass("lilbee-task-flash");
        }
 
        row.createDiv({ cls: "lilbee-task-rail" });
 
        const body = row.createDiv({ cls: "lilbee-task-body" });
 
        const head = body.createDiv({ cls: "lilbee-task-head" });
        const typeBadge = head.createSpan({ cls: `lilbee-task-type-badge lilbee-task-badge-${task.type}` });
        typeBadge.setText(task.type.toUpperCase());
        head.createSpan({ cls: "lilbee-task-name", text: task.name });
        const meta = head.createSpan({ cls: "lilbee-task-meta" });
        meta.setText(metaForRow(task, state));
 
        const stats = body.createDiv({ cls: "lilbee-task-stats" });
        const statsLabel = stats.createSpan({ cls: "lilbee-task-stats-label" });
        statsLabel.setText(statsLine(task, state));
        const pctLabel = stats.createSpan({ cls: "lilbee-task-pct" });
        pctLabel.setText(isIndeterminate || !isActive ? "" : `${Math.round(pct)}%`);
 
        const barContainer = body.createDiv({ cls: "lilbee-task-progress-bar" });
        const barFill = barContainer.createDiv({ cls: "lilbee-task-progress-fill" });
        barFill.style.width = `${pct}%`;
        if (isIndeterminate) barFill.classList.add("lilbee-task-progress-indeterminate");
 
        if (isActive && task.canCancel) {
            const cancelBtn = row.createEl("button", { cls: "lilbee-task-cancel" });
            cancelBtn.setText(MESSAGES.LABEL_CLOSE_GLYPH);
            cancelBtn.title = MESSAGES.LABEL_CANCEL_TASK;
            cancelBtn.addEventListener("click", (e) => {
                e.stopPropagation();
                void this.handleCancel(task);
            });
        }
 
        if ((state === TASK_STATUS.FAILED || state === TASK_STATUS.CANCELLED) && task.retry) {
            const retryBtn = row.createEl("button", { cls: "lilbee-task-retry" });
            retryBtn.setText(MESSAGES.LABEL_RETRY_TASK);
            retryBtn.title = MESSAGES.LABEL_RETRY_TASK;
            retryBtn.addEventListener("click", (e) => {
                e.stopPropagation();
                void task.retry?.();
            });
        }
 
        if (task.error && state === TASK_STATUS.FAILED) {
            row.title = task.error;
        }
    }
 
    private async handleCancel(task: TaskEntry): Promise<void> {
        if (task.canCancel) {
            this.plugin.taskQueue.cancel(task.id);
            return;
        }
 
        const modal = new ConfirmModal(this.plugin.app, MESSAGES.NOTICE_CONFIRM_CANCEL);
        modal.open();
        const confirmed = await modal.result;
 
        if (confirmed) {
            this.plugin.taskQueue.cancel(task.id);
        }
    }
}
 
function metaForRow(task: TaskEntry, state: TaskStatus): string {
    if (state === TASK_STATUS.ACTIVE) {
        return formatElapsed(Date.now() - task.startedAt);
    }
    if (state === TASK_STATUS.QUEUED) {
        return MESSAGES.LABEL_TASK_STATE_QUEUED;
    }
    if (state === TASK_STATUS.DONE) {
        return task.completedAt !== null ? relativeTime(task.completedAt) : MESSAGES.LABEL_TASK_STATE_DONE;
    }
    if (state === TASK_STATUS.FAILED) {
        return task.completedAt !== null ? relativeTime(task.completedAt) : MESSAGES.LABEL_TASK_STATE_FAILED;
    }
    if (state === TASK_STATUS.WAITING) {
        return task.completedAt !== null ? relativeTime(task.completedAt) : MESSAGES.LABEL_TASK_STATE_WAITING;
    }
    return task.completedAt !== null ? relativeTime(task.completedAt) : MESSAGES.LABEL_TASK_STATE_CANCELLED;
}
 
function statsLine(task: TaskEntry, state: TaskStatus): string {
    if (state !== TASK_STATUS.ACTIVE) {
        if (task.detail) return task.detail;
        if (task.error) return task.error;
        return "";
    }
    const parts: string[] = [];
    if (task.bytesTotal && task.bytesCurrent !== undefined) {
        parts.push(`${formatBytes(task.bytesCurrent)} / ${formatBytes(task.bytesTotal)}`);
    } else if (task.bytesCurrent !== undefined) {
        parts.push(formatBytes(task.bytesCurrent));
    }
    if (task.rateBps && task.rateBps > 0) {
        parts.push(formatRate(task.rateBps));
    }
    if (parts.length === 0 && task.detail) return task.detail;
    return parts.join(" — ");
}
 
function isWithinFlashWindow(task: TaskEntry): boolean {
    if (task.completedAt === null) return false;
    return Date.now() - task.completedAt < FLASH_WINDOW_MS;
}