All files / src/views sessions-modal.ts

100% Statements 136/136
100% Branches 32/32
100% Functions 23/23
100% Lines 123/123

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                                        24x 24x 24x 24x   24x 24x 24x     24x 24x 24x 24x       23x 23x 23x   23x 23x 23x   23x 23x 23x 23x 1x 1x     23x         23x 2x 2x   23x   23x 23x       24x 24x 20x 20x   4x   4x 3x   1x 1x     24x         2x 2x   1x 1x 1x   1x       3x 3x 3x       3x         38x 38x 3x       39x 38x 38x 38x   39x 7x 3x 3x   4x 3x 7x 7x   33x       33x 33x       33x 6x 6x     27x 27x 27x 27x       27x 27x       27x   27x 27x 27x 27x 27x 6x 6x     27x 27x 27x 27x       6x         6x   6x 6x 6x   6x 6x 2x 1x 1x 1x             4x 4x 4x 2x 2x   2x 2x 1x   1x 1x   2x       3x 3x 3x 2x 2x 1x 1x 1x   1x 1x         1x 1x      
import { App, Modal, Notice, setIcon } from "obsidian";
import type LilbeePlugin from "../main";
import { isHttpStatus } from "../api";
import type { SessionMeta } from "../types";
import { ConfirmModal } from "./confirm-modal";
import { MESSAGES } from "../locales/en";
import { bindEscapeToClose, errorMessage, relativeTimeFromIso } from "../utils";
import { displayLabelForRef } from "../utils/model-ref";
 
/** Hooks the chat view supplies so the modal can drive it without importing it. */
export interface SessionsModalHooks {
    /** Id of the conversation the chat view currently has open, if any. */
    activeId: string | null;
    resume: (id: string) => void;
    startNew: () => void;
}
 
export class SessionsModal extends Modal {
    private plugin: LilbeePlugin;
    private hooks: SessionsModalHooks;
    private sessions: SessionMeta[] = [];
    private filter = "";
    private renamingId: string | null = null;
    private loadFailed = false;
    /** True when the server said sessions are switched off; renders the turn-on offer. */
    private disabled = false;
    private listEl: HTMLElement | null = null;
    private countEl: HTMLElement | null = null;
 
    constructor(app: App, plugin: LilbeePlugin, hooks: SessionsModalHooks) {
        super(app);
        this.plugin = plugin;
        this.hooks = hooks;
        bindEscapeToClose(this);
    }
 
    onOpen(): void {
        const { contentEl } = this;
        contentEl.empty();
        contentEl.addClass("lilbee-sessions-modal");
 
        const header = contentEl.createDiv({ cls: "lilbee-sessions-header" });
        header.createEl("h2", { text: MESSAGES.TITLE_SESSIONS });
        this.countEl = header.createSpan({ cls: "lilbee-sessions-count" });
 
        const newBtn = header.createEl("button", { cls: "lilbee-sessions-new" });
        setIcon(newBtn, "plus");
        newBtn.setAttribute("aria-label", MESSAGES.LABEL_NEW_SESSION);
        newBtn.addEventListener("click", () => {
            this.hooks.startNew();
            this.close();
        });
 
        const filterEl = contentEl.createEl("input", {
            cls: "lilbee-sessions-filter",
            placeholder: MESSAGES.PLACEHOLDER_FILTER_SESSIONS,
            attr: { type: "text" },
        });
        filterEl.addEventListener("input", () => {
            this.filter = filterEl.value;
            this.renderList();
        });
        window.setTimeout(() => filterEl.focus(), 0);
 
        this.listEl = contentEl.createDiv({ cls: "lilbee-sessions-list" });
        void this.load();
    }
 
    private async load(): Promise<void> {
        try {
            this.sessions = await this.plugin.api.listSessions();
            this.loadFailed = false;
            this.disabled = false;
        } catch (err) {
            this.loadFailed = true;
            // The server 404s every session route when sessions_enabled is off.
            if (err instanceof Error && isHttpStatus(err, 404)) {
                this.disabled = true;
            } else {
                const reason = errorMessage(err, MESSAGES.ERROR_UNKNOWN, this.plugin.settings.serverMode);
                new Notice(MESSAGES.ERROR_SESSIONS_LOAD_FAILED(reason));
            }
        }
        this.renderList();
    }
 
    /** Flip the server's writable `sessions_enabled` flag, then reload the list. */
    private async enableSessions(): Promise<void> {
        try {
            await this.plugin.api.updateConfig({ sessions_enabled: true });
        } catch (err) {
            const reason = errorMessage(err, MESSAGES.ERROR_UNKNOWN, this.plugin.settings.serverMode);
            new Notice(MESSAGES.ERROR_SESSIONS_ENABLE_FAILED(reason));
            return;
        }
        await this.load();
    }
 
    private renderDisabledState(container: HTMLElement): void {
        const box = container.createDiv({ cls: "lilbee-sessions-disabled" });
        box.createDiv({ cls: "lilbee-sessions-disabled-text", text: MESSAGES.SESSIONS_DISABLED });
        const enableBtn = box.createEl("button", {
            cls: "lilbee-sessions-enable",
            text: MESSAGES.LABEL_ENABLE_SESSIONS,
        });
        enableBtn.addEventListener("click", () => void this.enableSessions());
    }
 
    /** Case-insensitive substring on the title, matching the TUI's filter. */
    private visible(): SessionMeta[] {
        const needle = this.filter.trim().toLowerCase();
        if (!needle) return this.sessions;
        return this.sessions.filter((s) => s.title.toLowerCase().includes(needle));
    }
 
    private renderList(): void {
        if (!this.listEl) return;
        this.listEl.empty();
        const rows = this.visible();
        this.countEl?.setText(MESSAGES.SESSIONS_COUNT(rows.length));
 
        if (rows.length === 0) {
            if (this.disabled) {
                this.renderDisabledState(this.listEl);
                return;
            }
            if (this.loadFailed) return;
            const empty = this.sessions.length === 0 ? MESSAGES.SESSIONS_EMPTY : MESSAGES.SESSIONS_NO_MATCH;
            this.listEl.createDiv({ cls: "lilbee-sessions-empty", text: empty });
            return;
        }
        for (const meta of rows) this.renderRow(this.listEl, meta);
    }
 
    private renderRow(container: HTMLElement, meta: SessionMeta): void {
        const isActive = meta.id === this.hooks.activeId;
        const row = container.createDiv({
            cls: `lilbee-session-row${isActive ? " is-active" : ""}`,
        });
 
        if (this.renamingId === meta.id) {
            this.renderRenameField(row, meta);
            return;
        }
 
        const main = row.createDiv({ cls: "lilbee-session-main" });
        const titleRow = main.createDiv({ cls: "lilbee-session-title-row" });
        titleRow.createSpan({ cls: "lilbee-session-title", text: meta.title });
        const dateEl = titleRow.createSpan({
            cls: "lilbee-session-date",
            text: relativeTimeFromIso(meta.updated_at),
        });
        if (meta.updated_at) dateEl.setAttribute("title", meta.updated_at);
        main.createDiv({
            cls: "lilbee-session-meta",
            text: MESSAGES.SESSIONS_ROW_META(meta.message_count, displayLabelForRef(meta.model_ref)),
        });
        main.addEventListener("click", () => this.resume(meta));
 
        const actions = row.createDiv({ cls: "lilbee-session-actions" });
        const renameBtn = actions.createEl("button", { cls: "lilbee-session-rename" });
        setIcon(renameBtn, "pencil");
        renameBtn.setAttribute("aria-label", MESSAGES.LABEL_RENAME_SESSION);
        renameBtn.addEventListener("click", () => {
            this.renamingId = meta.id;
            this.renderList();
        });
 
        const deleteBtn = actions.createEl("button", { cls: "lilbee-session-delete" });
        setIcon(deleteBtn, "trash-2");
        deleteBtn.setAttribute("aria-label", MESSAGES.LABEL_DELETE_SESSION);
        deleteBtn.addEventListener("click", () => void this.confirmDelete(meta));
    }
 
    private renderRenameField(row: HTMLElement, meta: SessionMeta): void {
        const input = row.createEl("input", {
            cls: "lilbee-session-rename-input",
            placeholder: MESSAGES.PLACEHOLDER_RENAME_SESSION,
            attr: { type: "text" },
        });
        input.value = meta.title;
        // Focus after the row re-renders; select the title so typing replaces it.
        window.setTimeout(() => {
            input.focus();
            input.select();
        }, 0);
        input.addEventListener("keydown", (evt: KeyboardEvent) => {
            if (evt.key === "Enter") void this.commitRename(meta, input.value);
            else if (evt.key === "Escape") {
                evt.stopPropagation();
                this.renamingId = null;
                this.renderList();
            }
        });
    }
 
    /** An empty title is discarded rather than written, matching the TUI. */
    private async commitRename(meta: SessionMeta, raw: string): Promise<void> {
        const title = raw.trim();
        this.renamingId = null;
        if (!title || title === meta.title) {
            this.renderList();
            return;
        }
        try {
            await this.plugin.api.renameSession(meta.id, title);
            meta.title = title;
        } catch (err) {
            const reason = errorMessage(err, MESSAGES.ERROR_UNKNOWN, this.plugin.settings.serverMode);
            new Notice(MESSAGES.ERROR_SESSION_RENAME_FAILED(reason));
        }
        this.renderList();
    }
 
    private async confirmDelete(meta: SessionMeta): Promise<void> {
        const modal = new ConfirmModal(this.app, MESSAGES.NOTICE_CONFIRM_DELETE_SESSION(meta.title));
        modal.open();
        if (!(await modal.result)) return;
        try {
            await this.plugin.api.deleteSession(meta.id);
            this.sessions = this.sessions.filter((s) => s.id !== meta.id);
            new Notice(MESSAGES.NOTICE_SESSION_DELETED(meta.title));
            this.renderList();
        } catch (err) {
            const reason = errorMessage(err, MESSAGES.ERROR_UNKNOWN, this.plugin.settings.serverMode);
            new Notice(MESSAGES.ERROR_SESSION_DELETE_FAILED(reason));
        }
    }
 
    private resume(meta: SessionMeta): void {
        this.hooks.resume(meta.id);
        this.close();
    }
}