Finished provider settings

This commit is contained in:
tsommer committed 2024-04-20 17:07:12 +02:00
1 parent 7748c6611b
commit c3fd6b81a8
8 files changed
+293 -51

No files matched your search

@@ -4,7 +4,7 @@
<MudPaper Class="pa-3">
<MudText Typo="Typo.h4" Class="mb-3">Configured Providers</MudText>
<MudTable Items="@this.Providers">
<MudTable Items="@this.SettingsManager.ConfigurationData.Providers">
<ColGroup>
<col style="width: 3em;"/>
<col style="width: 6em;"/>
@@ -22,17 +22,17 @@
<MudTd>@context.UsedProvider</MudTd>
<MudTd>@context.InstanceName</MudTd>
<MudTd Style="text-align: left;">
<MudButton Variant="Variant.Filled" Color="Color.Info" StartIcon="@Icons.Material.Filled.Edit" Class="mr-2">
<MudButton Variant="Variant.Filled" Color="Color.Info" StartIcon="@Icons.Material.Filled.Edit" Class="mr-2" OnClick="() => this.EditProvider(context)">
Edit
</MudButton>
<MudButton Variant="Variant.Filled" Color="Color.Error" StartIcon="@Icons.Material.Filled.Delete" Class="mr-2">
<MudButton Variant="Variant.Filled" Color="Color.Error" StartIcon="@Icons.Material.Filled.Delete" Class="mr-2" OnClick="() => this.DeleteProvider(context)">
Delete
</MudButton>
</MudTd>
</RowTemplate>
</MudTable>
@if(this.Providers.Count == 0)
@if(this.SettingsManager.ConfigurationData.Providers.Count == 0)
{
<MudText Typo="Typo.h6" Class="mt-3">No providers configured yet.</MudText>
}
@@ -1,5 +1,8 @@
using AIStudio.Components.CommonDialogs;
using AIStudio.Provider;
using AIStudio.Settings;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using MudBlazor;
@@ -14,35 +17,83 @@ public partial class Settings : ComponentBase
[Inject]
public IDialogService DialogService { get; init; } = null!;
[Inject]
public IJSRuntime JsRuntime { get; init; } = null!;
private List<global::AIStudio.Settings.Provider> Providers { get; set; } = new();
private static readonly DialogOptions DIALOG_OPTIONS = new()
{
CloseOnEscapeKey = true,
FullWidth = true, MaxWidth = MaxWidth.Medium,
};
#region Overrides of ComponentBase
protected override async Task OnInitializedAsync()
{
var settings = await this.SettingsManager.LoadSettings();
this.Providers = settings.Providers;
await this.SettingsManager.LoadSettings();
await base.OnInitializedAsync();
}
#endregion
private async Task AddProvider()
{
var dialogParameters = new DialogParameters<ProviderDialog>
{
{ x => x.UsedInstanceNames, this.Providers.Select(x => x.InstanceName.ToLowerInvariant()).ToList() },
{ x => x.IsEditing, false },
};
var dialogOptions = new DialogOptions { CloseOnEscapeKey = true, FullWidth = true, MaxWidth = MaxWidth.Medium };
var dialogReference = await this.DialogService.ShowAsync<ProviderDialog>("Add Provider", dialogParameters, dialogOptions);
var dialogReference = await this.DialogService.ShowAsync<ProviderDialog>("Add Provider", dialogParameters, DIALOG_OPTIONS);
var dialogResult = await dialogReference.Result;
if (dialogResult.Canceled)
return;
var addedProvider = (AIStudio.Settings.Provider)dialogResult.Data;
this.Providers.Add(addedProvider);
this.SettingsManager.ConfigurationData.Providers.Add(addedProvider);
await this.SettingsManager.StoreSettings();
}
private async Task EditProvider(global::AIStudio.Settings.Provider provider)
{
var dialogParameters = new DialogParameters<ProviderDialog>
{
{ x => x.DataId, provider.Id },
{ x => x.DataInstanceName, provider.InstanceName },
{ x => x.DataProvider, provider.UsedProvider },
{ x => x.IsEditing, true },
};
var dialogReference = await this.DialogService.ShowAsync<ProviderDialog>("Edit Provider", dialogParameters, DIALOG_OPTIONS);
var dialogResult = await dialogReference.Result;
if (dialogResult.Canceled)
return;
var editedProvider = (AIStudio.Settings.Provider)dialogResult.Data;
this.SettingsManager.ConfigurationData.Providers[this.SettingsManager.ConfigurationData.Providers.IndexOf(provider)] = editedProvider;
await this.SettingsManager.StoreSettings();
}
private async Task DeleteProvider(global::AIStudio.Settings.Provider provider)
{
var dialogParameters = new DialogParameters
{
{ "Message", $"Are you sure you want to delete the provider '{provider.InstanceName}'?" },
};
var dialogReference = await this.DialogService.ShowAsync<ConfirmDialog>("Delete Provider", dialogParameters, DIALOG_OPTIONS);
var dialogResult = await dialogReference.Result;
if (dialogResult.Canceled)
return;
var providerInstance = provider.UsedProvider.CreateProvider();
providerInstance.InstanceName = provider.InstanceName;
var deleteSecretResponse = await this.SettingsManager.DeleteAPIKey(this.JsRuntime, providerInstance);
if(deleteSecretResponse.Success)
{
this.SettingsManager.ConfigurationData.Providers.Remove(provider);
await this.SettingsManager.StoreSettings();
}
}
}