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
This commit is contained in:
AlexKucera committed 2025-10-24 13:00:02 +02:00
1 parent 92e109e2e8
commit 361f946d87
1 file changed
+31 -3
+31 -3
View File
@@ -33,9 +33,6 @@ export class FreewritingPromptsSettingTab extends PluginSettingTab {
containerEl.createEl('h2', { text: 'Freewriting Prompts' }); containerEl.createEl('h2', { text: 'Freewriting Prompts' });
// Load models asynchronously
await this.loadModels();
// MARK: - API Configuration // MARK: - API Configuration
containerEl.createEl('h3', { text: '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 // MARK: - Command Configuration
containerEl.createEl('h3', { text: '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<void> {
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 { private populateModelDropdown(dropdown: DropdownComponent): void {
if (this.availableModels.length === 0) { if (this.availableModels.length === 0) {
// No models available yet, disable dropdown // No models available yet, disable dropdown