All files / src/views memories-view.ts

100% Statements 89/89
100% Branches 22/22
100% Functions 18/18
100% Lines 82/82

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                  8x       6x 6x 4x 4x   2x 2x 1x 1x           20x 20x 20x     20x 20x       1x       1x       1x       16x 16x 16x   16x 16x 16x 16x 16x 16x   16x 16x 16x 16x 3x 3x     16x   16x       2x       21x 21x 20x 3x 3x 3x   17x 16x   2x 2x                 5x 4x 4x       19x 17x   17x 17x 5x 5x     12x 14x         14x 14x   14x               14x   14x       14x 14x 14x   14x       14x 14x       3x 3x 3x 2x 3x   1x                 3x 3x 3x 3x 2x 2x 1x 1x   1x                
import { App, ItemView, Notice, WorkspaceLeaf } from "obsidian";
import type LilbeePlugin from "../main";
import { MEMORY_CONFIG_KEY, MEMORY_KIND, type MemoryItem } from "../types";
import { addCloseButton } from "../components/close-button";
import { MESSAGES } from "../locales/en";
import { errorMessage } from "../utils";
import { ConfirmModal } from "./confirm-modal";
import { RememberModal } from "./remember-modal";
 
export const VIEW_TYPE_MEMORIES = "lilbee-memories";
 
/** Reveal the memories view, opening it in the right sidebar when it is not open. */
export async function openMemoriesView(app: App): Promise<void> {
    const existing = app.workspace.getLeavesOfType(VIEW_TYPE_MEMORIES);
    if (existing.length > 0) {
        void app.workspace.revealLeaf(existing[0]);
        return;
    }
    const leaf = app.workspace.getRightLeaf(false);
    if (leaf) {
        await leaf.setViewState({ type: VIEW_TYPE_MEMORIES, active: true });
        void app.workspace.revealLeaf(leaf);
    }
}
 
export class MemoriesView extends ItemView {
    private plugin: LilbeePlugin;
    private memories: MemoryItem[] | null = null;
    private filter = "";
    private listEl: HTMLElement | null = null;
 
    constructor(leaf: WorkspaceLeaf, plugin: LilbeePlugin) {
        super(leaf);
        this.plugin = plugin;
    }
 
    getViewType(): string {
        return VIEW_TYPE_MEMORIES;
    }
 
    getDisplayText(): string {
        return MESSAGES.LABEL_MEMORIES_VIEW;
    }
 
    getIcon(): string {
        return "brain";
    }
 
    async onOpen(): Promise<void> {
        const { contentEl } = this;
        contentEl.empty();
        contentEl.addClass("lilbee-memories-container");
 
        const header = contentEl.createDiv({ cls: "lilbee-memories-header" });
        header.createEl("h2", { text: MESSAGES.LABEL_MEMORIES_TITLE });
        const actions = header.createDiv({ cls: "lilbee-toolbar-group" });
        const addBtn = actions.createEl("button", { text: MESSAGES.BUTTON_ADD_MEMORY, cls: "lilbee-memories-add" });
        addBtn.addEventListener("click", () => new RememberModal(this.app, this.plugin).open());
        addCloseButton(actions, this.leaf, MESSAGES.LABEL_CLOSE_VIEW(MESSAGES.LABEL_MEMORIES_VIEW));
 
        const searchEl = contentEl.createEl("input", { cls: "lilbee-memories-search" });
        searchEl.type = "text";
        searchEl.placeholder = MESSAGES.MEMORIES_SEARCH_PLACEHOLDER;
        searchEl.addEventListener("input", () => {
            this.filter = searchEl.value.toLowerCase();
            this.renderList();
        });
 
        this.listEl = contentEl.createDiv({ cls: "lilbee-memories-list" });
 
        await this.loadMemories();
    }
 
    async reload(): Promise<void> {
        await this.loadMemories();
    }
 
    private async loadMemories(): Promise<void> {
        try {
            const cfg = await this.plugin.api.config();
            if (cfg[MEMORY_CONFIG_KEY.ENABLED] !== true) {
                this.memories = null;
                this.renderMessage(MESSAGES.MEMORIES_DISABLED);
                return;
            }
            this.memories = await this.plugin.api.listMemories();
            this.renderList();
        } catch (err) {
            this.memories = null;
            this.renderMessage(
                MESSAGES.MEMORIES_LOAD_FAILED(
                    errorMessage(err, MESSAGES.ERROR_UNKNOWN, this.plugin.settings.serverMode),
                ),
            );
        }
    }
 
    private renderMessage(message: string): void {
        if (!this.listEl) return;
        this.listEl.empty();
        this.listEl.createDiv({ cls: "lilbee-memories-empty", text: message });
    }
 
    private renderList(): void {
        if (!this.listEl || this.memories === null) return;
        this.listEl.empty();
 
        const visible = this.memories.filter((m) => m.text.toLowerCase().includes(this.filter));
        if (visible.length === 0) {
            this.listEl.createDiv({ cls: "lilbee-memories-empty", text: MESSAGES.MEMORIES_EMPTY });
            return;
        }
 
        for (const memory of visible) {
            this.renderRow(this.listEl, memory);
        }
    }
 
    private renderRow(container: HTMLElement, memory: MemoryItem): void {
        const row = container.createDiv({ cls: "lilbee-memory-row" });
        row.dataset.kind = memory.kind;
 
        row.createSpan({
            cls: `lilbee-memory-kind lilbee-memory-kind-${memory.kind}`,
            text:
                memory.kind === MEMORY_KIND.PREFERENCE
                    ? MESSAGES.LABEL_REMEMBER_KIND_PREFERENCE
                    : MESSAGES.LABEL_REMEMBER_KIND_FACT,
        });
 
        row.createSpan({ cls: "lilbee-memory-text", text: memory.text });
 
        const shareBtn = row.createEl("button", {
            cls: "lilbee-memory-share",
            text: memory.shared ? MESSAGES.MEMORIES_FLAG_YES : MESSAGES.MEMORIES_FLAG_NO,
        });
        shareBtn.title = MESSAGES.MEMORIES_SHARE_TOOLTIP;
        shareBtn.toggleClass("is-shared", memory.shared);
        shareBtn.addEventListener("click", () => void this.toggleShared(memory));
 
        const deleteBtn = row.createEl("button", {
            cls: "lilbee-memory-delete",
            text: MESSAGES.LABEL_CLOSE_GLYPH,
        });
        deleteBtn.title = MESSAGES.MEMORIES_DELETE_TOOLTIP;
        deleteBtn.addEventListener("click", () => void this.confirmDelete(memory));
    }
 
    private async toggleShared(memory: MemoryItem): Promise<void> {
        const next = !memory.shared;
        try {
            await this.plugin.api.setMemoryShared(memory.id, next);
            new Notice(next ? MESSAGES.MEMORIES_SHARED_ON : MESSAGES.MEMORIES_SHARED_OFF);
            await this.loadMemories();
        } catch (err) {
            new Notice(
                MESSAGES.MEMORIES_FLAG_FAILED(
                    errorMessage(err, MESSAGES.ERROR_UNKNOWN, this.plugin.settings.serverMode),
                ),
            );
        }
    }
 
    private async confirmDelete(memory: MemoryItem): Promise<void> {
        const modal = new ConfirmModal(this.app, MESSAGES.MEMORIES_DELETE_CONFIRM);
        modal.open();
        const confirmed = await modal.result;
        if (!confirmed) return;
        try {
            await this.plugin.api.forgetMemory(memory.id);
            new Notice(MESSAGES.MEMORIES_DELETED);
            await this.loadMemories();
        } catch (err) {
            new Notice(
                MESSAGES.MEMORIES_DELETE_FAILED(
                    errorMessage(err, MESSAGES.ERROR_UNKNOWN, this.plugin.settings.serverMode),
                ),
            );
        }
    }
}