From 361f946d879981b2724f4206b9286adfb06c8b51 Mon Sep 17 00:00:00 2001 From: Alexander Kucera Date: Fri, 24 Oct 2025 13:00:02 +0200 Subject: [PATCH] Improves async model loading for settings UI Refactors model loading to prevent UI blocking Adds fallback mechanism for model selection Ensures smooth dropdown population and model persistence Handles cases where previously selected model may no longer be available --- settings.ts | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/settings.ts b/settings.ts index b59908d..ccbb609 100644 --- a/settings.ts +++ b/settings.ts @@ -33,9 +33,6 @@ export class FreewritingPromptsSettingTab extends PluginSettingTab { containerEl.createEl('h2', { text: 'Freewriting Prompts' }); - // Load models asynchronously - await this.loadModels(); - // MARK: - API Configuration containerEl.createEl('h3', { text: 'API Configuration' }); @@ -84,6 +81,9 @@ export class FreewritingPromptsSettingTab extends PluginSettingTab { }); }); + // Load models asynchronously without blocking UI + this.loadModelsAsync(); + // MARK: - Command Configuration containerEl.createEl('h3', { text: 'Command Configuration' }); @@ -205,6 +205,34 @@ export class FreewritingPromptsSettingTab extends PluginSettingTab { } } + /** + * Load models asynchronously without blocking UI rendering + * Updates dropdown when models are fetched + */ + private async loadModelsAsync(): Promise { + await this.loadModels(); + + // Update dropdown after models are loaded + if (this.modelDropdown) { + this.populateModelDropdown(this.modelDropdown); + + // Restore selected model if it still exists + const currentModel = this.plugin.settings.model; + const modelExists = this.availableModels.some(m => m.id === currentModel); + if (modelExists) { + this.modelDropdown.setValue(currentModel); + } else { + // Fall back to first available model if current one is gone + const fallbackModel = this.availableModels[0]; + if (fallbackModel) { + this.modelDropdown.setValue(fallbackModel.id); + this.plugin.settings.model = fallbackModel.id; + await this.plugin.saveSettings(); + } + } + } + } + private populateModelDropdown(dropdown: DropdownComponent): void { if (this.availableModels.length === 0) { // No models available yet, disable dropdown