diff --git a/src/interface/obsidian/src/main.ts b/src/interface/obsidian/src/main.ts index f8cf621f..23082895 100644 --- a/src/interface/obsidian/src/main.ts +++ b/src/interface/obsidian/src/main.ts @@ -1,4 +1,4 @@ -import { Plugin } from 'obsidian'; +import { Notice, Plugin } from 'obsidian'; import { KhojSetting, KhojSettingTab, DEFAULT_SETTINGS } from 'src/settings' import { KhojModal } from 'src/modal' import { configureKhojBackend } from './utils'; @@ -14,18 +14,22 @@ export default class Khoj extends Plugin { this.addCommand({ id: 'search', name: 'Search', - callback: () => { - new KhojModal(this.app, this.settings).open(); + checkCallback: (checking) => { + if (!checking && this.settings.connectedToBackend) + new KhojModal(this.app, this.settings).open(); + return this.settings.connectedToBackend; } }); // Create an icon in the left ribbon. this.addRibbonIcon('search', 'Khoj', (_: MouseEvent) => { // Called when the user clicks the icon. - new KhojModal(this.app, this.settings).open(); + this.settings.connectedToBackend + ? new KhojModal(this.app, this.settings).open() + : new Notice(`❗️Ensure Khoj backend is running and Khoj URL is pointing to it in the plugin settings`); }); - // Add a settings tab so the user can configure various aspects of the plugin + // Add a settings tab so the user can configure khoj this.addSettingTab(new KhojSettingTab(this.app, this)); } @@ -41,7 +45,7 @@ export default class Khoj extends Plugin { } async saveSettings() { - await this.saveData(this.settings) - .then(() => configureKhojBackend(this.settings)); + await configureKhojBackend(this.settings) + .then(() => this.saveData(this.settings)); } } diff --git a/src/interface/obsidian/src/settings.ts b/src/interface/obsidian/src/settings.ts index 941c8b4d..e719ed8b 100644 --- a/src/interface/obsidian/src/settings.ts +++ b/src/interface/obsidian/src/settings.ts @@ -6,12 +6,14 @@ export interface KhojSetting { resultsCount: number; khojUrl: string; obsidianVaultPath: string; + connectedToBackend: boolean; } export const DEFAULT_SETTINGS: KhojSetting = { resultsCount: 6, khojUrl: 'http://localhost:8000', - obsidianVaultPath: getVaultAbsolutePath() + obsidianVaultPath: getVaultAbsolutePath(), + connectedToBackend: false, } export class KhojSettingTab extends PluginSettingTab { @@ -25,6 +27,13 @@ export class KhojSettingTab extends PluginSettingTab { display(): void { const { containerEl } = this; containerEl.empty(); + + // Add notice if unable to connect to khoj backend + if (!this.plugin.settings.connectedToBackend) { + containerEl.createEl('small', { text: '❗Ensure Khoj backend is running and Khoj URL is correctly set below' }); + } + + // Add khoj settings configurable from the plugin settings tab new Setting(containerEl) .setName('Vault Paths') .setDesc('The Obsidian Vault to search with Khoj') diff --git a/src/interface/obsidian/src/utils.ts b/src/interface/obsidian/src/utils.ts index 02ea6349..0cc67faa 100644 --- a/src/interface/obsidian/src/utils.ts +++ b/src/interface/obsidian/src/utils.ts @@ -16,13 +16,15 @@ export async function configureKhojBackend(setting: KhojSetting) { // Check if khoj backend is configured, show error if backend is not running let khoj_already_configured = await request(khojConfigUrl) .then(response => { + setting.connectedToBackend = true; return response !== "null" }) .catch(error => { - new Notice(`❗️Ensure Khoj backend is running and Khoj URL is pointing to it in the Khoj plugin settings.\n\n${error}`); + setting.connectedToBackend = false; + new Notice(`❗️Ensure Khoj backend is running and Khoj URL is pointing to it in the plugin settings.\n\n${error}`); }) // Short-circuit configuring khoj if unable to connect to khoj backend - if (khoj_already_configured === null) return; + if (!setting.connectedToBackend) return; // Get current config if khoj backend configured, else get default config from khoj backend await request(khoj_already_configured ? khojConfigUrl : `${khojConfigUrl}/default`)