diff --git a/src/interface/obsidian/src/main.ts b/src/interface/obsidian/src/main.ts index 23082895..1725b5ae 100644 --- a/src/interface/obsidian/src/main.ts +++ b/src/interface/obsidian/src/main.ts @@ -45,7 +45,7 @@ export default class Khoj extends Plugin { } async saveSettings() { - await configureKhojBackend(this.settings) + await configureKhojBackend(this.settings, false) .then(() => this.saveData(this.settings)); } } diff --git a/src/interface/obsidian/src/settings.ts b/src/interface/obsidian/src/settings.ts index 74ae9c76..a551942a 100644 --- a/src/interface/obsidian/src/settings.ts +++ b/src/interface/obsidian/src/settings.ts @@ -1,4 +1,4 @@ -import { App, PluginSettingTab, request, Setting } from 'obsidian'; +import { App, Notice, PluginSettingTab, request, Setting } from 'obsidian'; import Khoj from 'src/main'; import { getVaultAbsolutePath } from 'src/utils'; @@ -28,10 +28,8 @@ export class KhojSettingTab extends PluginSettingTab { 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 notice whether able to connect to khoj backend or not + containerEl.createEl('small', { text: this.getBackendStatusMessage() }); // Add khoj settings configurable from the plugin settings tab new Setting(containerEl) @@ -49,8 +47,9 @@ export class KhojSettingTab extends PluginSettingTab { .addText(text => text .setValue(`${this.plugin.settings.khojUrl}`) .onChange(async (value) => { - this.plugin.settings.khojUrl = value; - await this.plugin.saveSettings(); + this.plugin.settings.khojUrl = value.trim(); + await this.plugin.saveSettings() + .finally(() => containerEl.firstElementChild?.setText(this.getBackendStatusMessage())); })); new Setting(containerEl) .setName('Results Count') @@ -69,8 +68,15 @@ export class KhojSettingTab extends PluginSettingTab { .setButtonText('Update') .setCta() .onClick(async () => { - await request(`${this.plugin.settings.khojUrl}/api/update?t=markdown&force=true`); - } - )); + await request(`${this.plugin.settings.khojUrl}/api/update?t=markdown&force=true`) + .then(() => new Notice('✅ Updated Khoj index.')) + }) + ); + } + + getBackendStatusMessage() { + return !this.plugin.settings.connectedToBackend + ? '❗Disconnected from Khoj backend. Ensure Khoj backend is running and Khoj URL is correctly set below.' + : '✅ Connected to Khoj backend.'; } } diff --git a/src/interface/obsidian/src/utils.ts b/src/interface/obsidian/src/utils.ts index 3d4220b2..10d08b3e 100644 --- a/src/interface/obsidian/src/utils.ts +++ b/src/interface/obsidian/src/utils.ts @@ -9,7 +9,7 @@ export function getVaultAbsolutePath(): string { return ''; } -export async function configureKhojBackend(setting: KhojSetting) { +export async function configureKhojBackend(setting: KhojSetting, notify: boolean = true) { let mdInVault = `${setting.obsidianVaultPath}/**/*.md`; let khojConfigUrl = `${setting.khojUrl}/api/config/data`; @@ -21,7 +21,8 @@ export async function configureKhojBackend(setting: KhojSetting) { }) .catch(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}`); + if (notify) + 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 (!setting.connectedToBackend) return; @@ -79,10 +80,10 @@ export async function configureKhojBackend(setting: KhojSetting) { updateKhojBackend(setting.khojUrl, data); console.log(`Khoj: Updated markdown config in khoj backend config:\n${JSON.stringify(data["content-type"]["markdown"])}`) } - new Notice(`✅ Successfully Setup Khoj`); }) .catch(error => { - new Notice(`❗️Failed to configure Khoj backend. Contact developer on Github.\n\nError: ${error}`); + if (notify) + new Notice(`❗️Failed to configure Khoj backend. Contact developer on Github.\n\nError: ${error}`); }) }