Added a provider selection component for the settings page to let the user, e.g., preselect a provider for a certain area

This commit is contained in:
Thorsten Sommer 2024-07-27 12:21:09 +02:00
parent 1d5e4542c8
commit f378ac6d83
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
2 changed files with 69 additions and 0 deletions

View File

@ -0,0 +1 @@
<ConfigurationSelect OptionDescription="Preselected provider" OptionHelp="Select a provider that is preselected." Data="@this.Data" SelectedValue="@this.SelectedValue" SelectionUpdate="@this.SelectionUpdate"/>

View File

@ -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<string> SelectedValue { get; set; } = () => string.Empty;
[Parameter]
public Action<string> SelectionUpdate { get; set; } = _ => { };
[Parameter]
public IEnumerable<ConfigurationSelectData<string>> Data { get; set; } = new List<ConfigurationSelectData<string>>();
[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<T>(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<TResult?> ProcessMessageWithResult<TPayload, TResult>(ComponentBase? sendingComponent, Event triggeredEvent, TPayload? data)
{
return Task.FromResult<TResult?>(default);
}
#endregion
}