AI-Studio/app/MindWork AI Studio/Components/ConfigurationMultiSelect.razor.cs

54 lines
1.7 KiB
C#
Raw Normal View History

using AIStudio.Settings;
using Microsoft.AspNetCore.Components;
namespace AIStudio.Components;
/// <summary>
/// Configuration component for selecting many values from a list.
/// </summary>
2025-04-27 07:06:05 +00:00
/// <typeparam name="TData">The type of the value to select.</typeparam>
public partial class ConfigurationMultiSelect<TData> : ConfigurationBase
{
/// <summary>
/// The data to select from.
/// </summary>
[Parameter]
2025-04-27 07:06:05 +00:00
public IEnumerable<ConfigurationSelectData<TData>> Data { get; set; } = [];
/// <summary>
/// The selected values.
/// </summary>
[Parameter]
2025-04-27 07:06:05 +00:00
public Func<HashSet<TData>> SelectedValues { get; set; } = () => [];
/// <summary>
/// An action that is called when the selection changes.
/// </summary>
[Parameter]
2025-04-27 07:06:05 +00:00
public Action<HashSet<TData>> SelectionUpdate { get; set; } = _ => { };
2025-04-27 07:06:05 +00:00
private async Task OptionChanged(IEnumerable<TData?>? updatedValues)
{
if(updatedValues is null)
this.SelectionUpdate([]);
else
this.SelectionUpdate(updatedValues.Where(n => n is not null).ToHashSet()!);
await this.SettingsManager.StoreSettings();
await this.InformAboutChange();
}
private static string GetClass => $"{MARGIN_CLASS} rounded-lg";
2025-04-27 07:06:05 +00:00
private string GetMultiSelectionText(List<TData?>? selectedValues)
{
if(selectedValues is null || selectedValues.Count == 0)
2025-04-27 07:06:05 +00:00
return T("No preview features selected.");
if(selectedValues.Count == 1)
2025-04-27 07:06:05 +00:00
return T("You have selected 1 preview feature.");
2025-04-27 07:06:05 +00:00
return string.Format(T("You have selected {0} preview features."), selectedValues.Count);
}
}