Update plugin notifications for errors and success

- Only show notification on plugin load and failure.
- In settings page, set current backend status at top of pane instead
  of showing notification
  Notices bubbles cluttered the UI while typing updates to settings
- Show notification once index updated via settings pane button click
  There was no notification on index updated, which usually takes time
  on the backend
This commit is contained in:
Debanjum Singh Solanky
2023-01-11 02:45:50 -03:00
parent 853192932a
commit 5af2b68e2b
3 changed files with 22 additions and 15 deletions

View File

@@ -45,7 +45,7 @@ export default class Khoj extends Plugin {
} }
async saveSettings() { async saveSettings() {
await configureKhojBackend(this.settings) await configureKhojBackend(this.settings, false)
.then(() => this.saveData(this.settings)); .then(() => this.saveData(this.settings));
} }
} }

View File

@@ -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 Khoj from 'src/main';
import { getVaultAbsolutePath } from 'src/utils'; import { getVaultAbsolutePath } from 'src/utils';
@@ -28,10 +28,8 @@ export class KhojSettingTab extends PluginSettingTab {
const { containerEl } = this; const { containerEl } = this;
containerEl.empty(); containerEl.empty();
// Add notice if unable to connect to khoj backend // Add notice whether able to connect to khoj backend or not
if (!this.plugin.settings.connectedToBackend) { containerEl.createEl('small', { text: this.getBackendStatusMessage() });
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 // Add khoj settings configurable from the plugin settings tab
new Setting(containerEl) new Setting(containerEl)
@@ -49,8 +47,9 @@ export class KhojSettingTab extends PluginSettingTab {
.addText(text => text .addText(text => text
.setValue(`${this.plugin.settings.khojUrl}`) .setValue(`${this.plugin.settings.khojUrl}`)
.onChange(async (value) => { .onChange(async (value) => {
this.plugin.settings.khojUrl = value; this.plugin.settings.khojUrl = value.trim();
await this.plugin.saveSettings(); await this.plugin.saveSettings()
.finally(() => containerEl.firstElementChild?.setText(this.getBackendStatusMessage()));
})); }));
new Setting(containerEl) new Setting(containerEl)
.setName('Results Count') .setName('Results Count')
@@ -69,8 +68,15 @@ export class KhojSettingTab extends PluginSettingTab {
.setButtonText('Update') .setButtonText('Update')
.setCta() .setCta()
.onClick(async () => { .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.';
} }
} }

View File

@@ -9,7 +9,7 @@ export function getVaultAbsolutePath(): string {
return ''; return '';
} }
export async function configureKhojBackend(setting: KhojSetting) { export async function configureKhojBackend(setting: KhojSetting, notify: boolean = true) {
let mdInVault = `${setting.obsidianVaultPath}/**/*.md`; let mdInVault = `${setting.obsidianVaultPath}/**/*.md`;
let khojConfigUrl = `${setting.khojUrl}/api/config/data`; let khojConfigUrl = `${setting.khojUrl}/api/config/data`;
@@ -21,7 +21,8 @@ export async function configureKhojBackend(setting: KhojSetting) {
}) })
.catch(error => { .catch(error => {
setting.connectedToBackend = false; 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 // Short-circuit configuring khoj if unable to connect to khoj backend
if (!setting.connectedToBackend) return; if (!setting.connectedToBackend) return;
@@ -79,10 +80,10 @@ export async function configureKhojBackend(setting: KhojSetting) {
updateKhojBackend(setting.khojUrl, data); updateKhojBackend(setting.khojUrl, data);
console.log(`Khoj: Updated markdown config in khoj backend config:\n${JSON.stringify(data["content-type"]["markdown"])}`) console.log(`Khoj: Updated markdown config in khoj backend config:\n${JSON.stringify(data["content-type"]["markdown"])}`)
} }
new Notice(`✅ Successfully Setup Khoj`);
}) })
.catch(error => { .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}`);
}) })
} }