All files / src/views search-modal.ts

100% Statements 68/68
100% Branches 22/22
100% Functions 12/12
100% Lines 61/61

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                    35x     35x 35x 35x 5x 4x     35x 35x 35x 35x     35x       34x 34x 34x   34x   34x           34x   34x 34x   34x 5x 5x     34x       3x 3x 3x       34x   34x 2x   34x 31x         31x 31x 93x       93x 2x 2x 6x 2x 2x           42x 41x 41x             10x 9x 9x       10x 1x 1x   9x 9x 9x         8x 7x 7x 5x 5x   2x 3x     1x 1x        
import { App, Modal, Notice } from "obsidian";
import type LilbeePlugin from "../main";
import { SEARCH_CHUNK_TYPE, type DocumentResult, type SearchChunkType } from "../types";
import { renderDocumentResult } from "./results";
import { MESSAGES } from "../locales/en";
import { bindEscapeToClose, debounce, DEBOUNCE_MS } from "../utils";
 
export class SearchModal extends Modal {
    private plugin: LilbeePlugin;
    private debouncedSearch: () => void;
    private resultsContainer: HTMLElement | null = null;
 
    constructor(app: App, plugin: LilbeePlugin) {
        super(app);
        this.plugin = plugin;
        const debounced = debounce(() => {
            if (this.lastSearchQuery) {
                void this.runSearch(this.lastSearchQuery);
            }
        }, DEBOUNCE_MS);
        this.debouncedSearch = debounced.run;
        this.cancelDebouncedSearch = debounced.cancel;
        this.lastSearchQuery = "";
        bindEscapeToClose(this);
    }
 
    private lastSearchQuery = "";
    private cancelDebouncedSearch: () => void;
 
    onOpen(): void {
        const { contentEl } = this;
        contentEl.empty();
        contentEl.addClass("lilbee-modal");
 
        contentEl.createEl("h2", { text: MESSAGES.TITLE_SEARCH });
 
        const input = contentEl.createEl("input", {
            type: "text",
            cls: "lilbee-search-input",
            placeholder: MESSAGES.PLACEHOLDER_TYPE_SEARCH,
        });
 
        this.renderSearchModeToggle(contentEl);
 
        this.resultsContainer = contentEl.createDiv({ cls: "lilbee-modal-results" });
        this.renderEmptyState(MESSAGES.LABEL_ENTER_QUERY);
 
        input.addEventListener("input", () => {
            this.lastSearchQuery = input.value.trim();
            this.debouncedSearch();
        });
 
        window.setTimeout(() => input.focus(), 0);
    }
 
    onClose(): void {
        this.cancelDebouncedSearch();
        const { contentEl } = this;
        contentEl.empty();
    }
 
    private renderSearchModeToggle(container: HTMLElement): void {
        const wikiEnabled = this.plugin.settings.wikiEnabled;
        // With the toggle hidden, any narrowed scope is a scope the user cannot see or undo.
        if (!wikiEnabled && this.plugin.settings.searchChunkType !== SEARCH_CHUNK_TYPE.ALL) {
            this.plugin.settings.searchChunkType = SEARCH_CHUNK_TYPE.ALL;
        }
        if (!wikiEnabled) return;
        const modes: { value: SearchChunkType; label: string }[] = [
            { value: SEARCH_CHUNK_TYPE.ALL, label: MESSAGES.LABEL_SEARCH_ALL },
            { value: SEARCH_CHUNK_TYPE.WIKI, label: MESSAGES.LABEL_SEARCH_WIKI },
            { value: SEARCH_CHUNK_TYPE.RAW, label: MESSAGES.LABEL_SEARCH_RAW },
        ];
        const group = container.createDiv({ cls: "lilbee-search-mode" });
        for (const mode of modes) {
            const btn = group.createEl("button", {
                text: mode.label,
                cls: `lilbee-search-mode-btn${this.plugin.settings.searchChunkType === mode.value ? " active" : ""}`,
            });
            btn.addEventListener("click", () => {
                this.plugin.settings.searchChunkType = mode.value;
                void this.plugin.saveSettings();
                group.querySelectorAll(".lilbee-search-mode-btn").forEach((b) => b.removeClass("active"));
                btn.addClass("active");
                if (this.lastSearchQuery) this.debouncedSearch();
            });
        }
    }
 
    private renderEmptyState(message: string): void {
        if (!this.resultsContainer) return;
        this.resultsContainer.empty();
        this.resultsContainer.createEl("p", {
            text: message,
            cls: "lilbee-empty-state",
        });
    }
 
    private renderLoading(): void {
        if (!this.resultsContainer) return;
        this.resultsContainer.empty();
        this.resultsContainer.createDiv({ cls: "lilbee-loading" });
    }
 
    private async runSearch(query: string): Promise<void> {
        if (!query) {
            this.renderEmptyState(MESSAGES.LABEL_ENTER_QUERY);
            return;
        }
        this.renderLoading();
        try {
            const results: DocumentResult[] = await this.plugin.api.search(
                query,
                this.plugin.settings.topK,
                this.plugin.settings.searchChunkType,
            );
            if (!this.resultsContainer) return;
            this.resultsContainer.empty();
            if (results.length === 0) {
                this.renderEmptyState(MESSAGES.LABEL_NO_RESULTS);
                return;
            }
            for (const result of results) {
                renderDocumentResult(this.resultsContainer, result, this.app, this.plugin.api);
            }
        } catch {
            new Notice(MESSAGES.ERROR_COULD_NOT_CONNECT);
            this.renderEmptyState(MESSAGES.ERROR_SEARCH_CONNECT);
        }
    }
}