diff --git a/app/MindWork AI Studio/Components/Blocks/ConfigurationProviderSelection.razor b/app/MindWork AI Studio/Components/Blocks/ConfigurationProviderSelection.razor new file mode 100644 index 00000000..29dccb67 --- /dev/null +++ b/app/MindWork AI Studio/Components/Blocks/ConfigurationProviderSelection.razor @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/app/MindWork AI Studio/Components/Blocks/ConfigurationProviderSelection.razor.cs b/app/MindWork AI Studio/Components/Blocks/ConfigurationProviderSelection.razor.cs new file mode 100644 index 00000000..8898c8bb --- /dev/null +++ b/app/MindWork AI Studio/Components/Blocks/ConfigurationProviderSelection.razor.cs @@ -0,0 +1,68 @@ +using AIStudio.Settings; +using AIStudio.Tools; + +using Microsoft.AspNetCore.Components; + +namespace AIStudio.Components.Blocks; + +public partial class ConfigurationProviderSelection : ComponentBase, IMessageBusReceiver +{ + [Parameter] + public Func SelectedValue { get; set; } = () => string.Empty; + + [Parameter] + public Action SelectionUpdate { get; set; } = _ => { }; + + [Parameter] + public IEnumerable> Data { get; set; } = new List>(); + + [Inject] + private SettingsManager SettingsManager { get; init; } = null!; + + [Inject] + private MessageBus MessageBus { get; init; } = null!; + + #region Overrides of ComponentBase + + protected override async Task OnParametersSetAsync() + { + // Register this component with the message bus: + this.MessageBus.RegisterComponent(this); + this.MessageBus.ApplyFilters(this, [], [ Event.CONFIGURATION_CHANGED ]); + + await base.OnParametersSetAsync(); + } + + #endregion + + #region Implementation of IMessageBusReceiver + + public async Task ProcessMessage(ComponentBase? sendingComponent, Event triggeredEvent, T? data) + { + switch (triggeredEvent) + { + case Event.CONFIGURATION_CHANGED: + + if(string.IsNullOrWhiteSpace(this.SelectedValue())) + break; + + // Check if the selected value is still valid: + if (this.Data.All(x => x.Value != this.SelectedValue())) + { + this.SelectedValue = () => string.Empty; + this.SelectionUpdate(string.Empty); + await this.SettingsManager.StoreSettings(); + } + + this.StateHasChanged(); + break; + } + } + + public Task ProcessMessageWithResult(ComponentBase? sendingComponent, Event triggeredEvent, TPayload? data) + { + return Task.FromResult(default); + } + + #endregion +} \ No newline at end of file