Files
AI-Studio/app/MindWork AI Studio/Dialogs/Settings/SettingsDialogProfiles.razor.cs
T

111 lines
4.5 KiB
C#
Raw Normal View History

2025-01-05 15:11:15 +01:00
using AIStudio.Settings;
using AIStudio.Tools.PluginSystem;
2025-01-05 15:11:15 +01:00
2025-05-25 13:27:22 +02:00
namespace AIStudio.Dialogs.Settings;
2025-01-05 15:11:15 +01:00
2025-05-25 13:27:22 +02:00
public partial class SettingsDialogProfiles : SettingsDialogBase
2025-01-05 15:11:15 +01:00
{
private async Task AddProfile()
{
var dialogParameters = new DialogParameters<ProfileDialog>
{
{ x => x.IsEditing, false },
};
await this.AddProfile(dialogParameters, isImport: false);
}
private async Task ImportProfile()
{
var import = await ConfigurationSnippetImportDialog.ShowAsync(this.DialogService, PluginConfigurationObjectType.PROFILE, T("Import Profile"));
if (import?.ConfigurationObject is not Profile profile)
return;
await this.AddProfile(CreateDialogParameters(profile, isEditing: false), isImport: true);
}
private async Task AddProfile(DialogParameters<ProfileDialog> dialogParameters, bool isImport)
{
2025-04-27 09:06:05 +02:00
var dialogReference = await this.DialogService.ShowAsync<ProfileDialog>(T("Add Profile"), dialogParameters, DialogOptions.FULLSCREEN);
2025-01-05 15:11:15 +01:00
var dialogResult = await dialogReference.Result;
if (dialogResult is null || dialogResult.Canceled)
return;
//
// An organization can lock the import while the user is still filling in the form, e.g.,
// with a new version of its configuration plugin. Hence, the permission is checked once
// more before the imported profile is stored:
//
if (isImport && !this.SettingsManager.ConfigurationData.App.CanImportConfigurationSnippet(PluginConfigurationObjectType.PROFILE))
return;
2025-01-05 15:11:15 +01:00
var addedProfile = (Profile)dialogResult.Data!;
addedProfile = addedProfile with { Num = this.SettingsManager.ConfigurationData.NextProfileNum++ };
2025-01-05 15:11:15 +01:00
this.SettingsManager.ConfigurationData.Profiles.Add(addedProfile);
2025-01-05 15:11:15 +01:00
await this.SettingsManager.StoreSettings();
await this.MessageBus.SendMessage<bool>(this, Event.CONFIGURATION_CHANGED);
}
2025-01-05 15:11:15 +01:00
private async Task EditProfile(Profile profile)
{
var dialogReference = await this.DialogService.ShowAsync<ProfileDialog>(T("Edit Profile"), CreateDialogParameters(profile, isEditing: true), DialogOptions.FULLSCREEN);
2025-01-05 15:11:15 +01:00
var dialogResult = await dialogReference.Result;
if (dialogResult is null || dialogResult.Canceled)
return;
2025-01-05 15:11:15 +01:00
var editedProfile = (Profile)dialogResult.Data!;
this.SettingsManager.ConfigurationData.Profiles[this.SettingsManager.ConfigurationData.Profiles.IndexOf(profile)] = editedProfile;
2025-01-05 15:11:15 +01:00
await this.SettingsManager.StoreSettings();
await this.MessageBus.SendMessage<bool>(this, Event.CONFIGURATION_CHANGED);
}
private async Task ViewProfile(Profile profile)
{
await this.DialogService.ShowAsync<ProfileDialog>(T("View Profile"), CreateDialogParameters(profile, isEditing: true, isReadOnly: true), DialogOptions.FULLSCREEN);
}
private static DialogParameters<ProfileDialog> CreateDialogParameters(Profile profile, bool isEditing, bool isReadOnly = false) => new()
{
{ x => x.DataNum, profile.Num },
{ x => x.DataId, profile.Id },
{ x => x.DataName, profile.Name },
{ x => x.DataNeedToKnow, profile.NeedToKnow },
{ x => x.DataActions, profile.Actions },
{ x => x.IsEditing, isEditing },
{ x => x.IsReadOnly, isReadOnly },
};
private async Task ExportProfile(Profile profile)
{
if (!this.SettingsManager.ConfigurationData.App.ShowAdminSettings)
return;
if (profile == Profile.NO_PROFILE || profile.IsEnterpriseConfiguration)
return;
var luaCode = profile.ExportAsConfigurationSection();
if (!string.IsNullOrWhiteSpace(luaCode))
2026-08-02 21:41:12 +02:00
await this.RustService.CopyText2Clipboard(luaCode);
}
2025-01-05 15:11:15 +01:00
private async Task DeleteProfile(Profile profile)
{
var dialogParameters = new DialogParameters<ConfirmDialog>
2025-01-05 15:11:15 +01:00
{
{ x => x.Message, string.Format(T("Are you sure you want to delete the profile '{0}'?"), profile.Name) },
2025-01-05 15:11:15 +01:00
};
2025-04-27 09:06:05 +02:00
var dialogReference = await this.DialogService.ShowAsync<ConfirmDialog>(T("Delete Profile"), dialogParameters, DialogOptions.FULLSCREEN);
2025-01-05 15:11:15 +01:00
var dialogResult = await dialogReference.Result;
if (dialogResult is null || dialogResult.Canceled)
return;
2025-01-05 15:11:15 +01:00
this.SettingsManager.ConfigurationData.Profiles.Remove(profile);
await this.SettingsManager.StoreSettings();
2025-01-05 15:11:15 +01:00
await this.MessageBus.SendMessage<bool>(this, Event.CONFIGURATION_CHANGED);
}
}