2024-12-03 20:02:37 +00:00
|
|
|
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:03:42 +00:00
|
|
|
/// <typeparam name="TData">The type of the value to select.</typeparam>
|
|
|
|
public partial class ConfigurationMultiSelect<TData> : ConfigurationBase
|
2024-12-03 20:02:37 +00:00
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// The data to select from.
|
|
|
|
/// </summary>
|
|
|
|
[Parameter]
|
2025-04-27 07:03:42 +00:00
|
|
|
public IEnumerable<ConfigurationSelectData<TData>> Data { get; set; } = [];
|
2024-12-03 20:02:37 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The selected values.
|
|
|
|
/// </summary>
|
|
|
|
[Parameter]
|
2025-04-27 07:03:42 +00:00
|
|
|
public Func<HashSet<TData>> SelectedValues { get; set; } = () => [];
|
2024-12-03 20:02:37 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// An action that is called when the selection changes.
|
|
|
|
/// </summary>
|
|
|
|
[Parameter]
|
2025-04-27 07:03:42 +00:00
|
|
|
public Action<HashSet<TData>> SelectionUpdate { get; set; } = _ => { };
|
2024-12-03 20:02:37 +00:00
|
|
|
|
2025-04-27 07:03:42 +00:00
|
|
|
private async Task OptionChanged(IEnumerable<TData?>? updatedValues)
|
2024-12-03 20:02:37 +00:00
|
|
|
{
|
|
|
|
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:03:42 +00:00
|
|
|
private string GetMultiSelectionText(List<TData?>? selectedValues)
|
2024-12-03 20:02:37 +00:00
|
|
|
{
|
|
|
|
if(selectedValues is null || selectedValues.Count == 0)
|
2025-04-27 07:03:42 +00:00
|
|
|
return T("No preview features selected.");
|
2024-12-03 20:02:37 +00:00
|
|
|
|
|
|
|
if(selectedValues.Count == 1)
|
2025-04-27 07:03:42 +00:00
|
|
|
return T("You have selected 1 preview feature.");
|
2024-12-03 20:02:37 +00:00
|
|
|
|
2025-04-27 07:03:42 +00:00
|
|
|
return string.Format(T("You have selected {0} preview features."), selectedValues.Count);
|
2024-12-03 20:02:37 +00:00
|
|
|
}
|
|
|
|
}
|