mirror of
https://github.com/khoaliber/khoj.git
synced 2026-03-03 13:19:16 +00:00
Track connectedToBackend as a setting. Use it across obsidian plugin
- Display warning at top of khoj obsidian plugin settings - Make search command available only if connected to backend - Show warning notice on clicking khoj search ribbon button - Call saveData after configureKhojBackend to ensure connnectedToBackend setting saved after being (potentially) updated in configureKhojBackend function
This commit is contained in:
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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')
|
||||
|
||||
@@ -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`)
|
||||
|
||||
Reference in New Issue
Block a user