mirror of
https://github.com/khoaliber/khoj.git
synced 2026-03-07 05:40:17 +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 { KhojSetting, KhojSettingTab, DEFAULT_SETTINGS } from 'src/settings'
|
||||||
import { KhojModal } from 'src/modal'
|
import { KhojModal } from 'src/modal'
|
||||||
import { configureKhojBackend } from './utils';
|
import { configureKhojBackend } from './utils';
|
||||||
@@ -14,18 +14,22 @@ export default class Khoj extends Plugin {
|
|||||||
this.addCommand({
|
this.addCommand({
|
||||||
id: 'search',
|
id: 'search',
|
||||||
name: 'Search',
|
name: 'Search',
|
||||||
callback: () => {
|
checkCallback: (checking) => {
|
||||||
|
if (!checking && this.settings.connectedToBackend)
|
||||||
new KhojModal(this.app, this.settings).open();
|
new KhojModal(this.app, this.settings).open();
|
||||||
|
return this.settings.connectedToBackend;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Create an icon in the left ribbon.
|
// Create an icon in the left ribbon.
|
||||||
this.addRibbonIcon('search', 'Khoj', (_: MouseEvent) => {
|
this.addRibbonIcon('search', 'Khoj', (_: MouseEvent) => {
|
||||||
// Called when the user clicks the icon.
|
// 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));
|
this.addSettingTab(new KhojSettingTab(this.app, this));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -41,7 +45,7 @@ export default class Khoj extends Plugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async saveSettings() {
|
async saveSettings() {
|
||||||
await this.saveData(this.settings)
|
await configureKhojBackend(this.settings)
|
||||||
.then(() => configureKhojBackend(this.settings));
|
.then(() => this.saveData(this.settings));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,12 +6,14 @@ export interface KhojSetting {
|
|||||||
resultsCount: number;
|
resultsCount: number;
|
||||||
khojUrl: string;
|
khojUrl: string;
|
||||||
obsidianVaultPath: string;
|
obsidianVaultPath: string;
|
||||||
|
connectedToBackend: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const DEFAULT_SETTINGS: KhojSetting = {
|
export const DEFAULT_SETTINGS: KhojSetting = {
|
||||||
resultsCount: 6,
|
resultsCount: 6,
|
||||||
khojUrl: 'http://localhost:8000',
|
khojUrl: 'http://localhost:8000',
|
||||||
obsidianVaultPath: getVaultAbsolutePath()
|
obsidianVaultPath: getVaultAbsolutePath(),
|
||||||
|
connectedToBackend: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
export class KhojSettingTab extends PluginSettingTab {
|
export class KhojSettingTab extends PluginSettingTab {
|
||||||
@@ -25,6 +27,13 @@ export class KhojSettingTab extends PluginSettingTab {
|
|||||||
display(): void {
|
display(): void {
|
||||||
const { containerEl } = this;
|
const { containerEl } = this;
|
||||||
containerEl.empty();
|
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)
|
new Setting(containerEl)
|
||||||
.setName('Vault Paths')
|
.setName('Vault Paths')
|
||||||
.setDesc('The Obsidian Vault to search with Khoj')
|
.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
|
// Check if khoj backend is configured, show error if backend is not running
|
||||||
let khoj_already_configured = await request(khojConfigUrl)
|
let khoj_already_configured = await request(khojConfigUrl)
|
||||||
.then(response => {
|
.then(response => {
|
||||||
|
setting.connectedToBackend = true;
|
||||||
return response !== "null"
|
return response !== "null"
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.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
|
// 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
|
// Get current config if khoj backend configured, else get default config from khoj backend
|
||||||
await request(khoj_already_configured ? khojConfigUrl : `${khojConfigUrl}/default`)
|
await request(khoj_already_configured ? khojConfigUrl : `${khojConfigUrl}/default`)
|
||||||
|
|||||||
Reference in New Issue
Block a user