using AIStudio.Settings; using Microsoft.AspNetCore.Components; namespace AIStudio.Components; /// /// Configuration component for selecting many values from a list. /// /// The type of the value to select. public partial class ConfigurationMultiSelect : ConfigurationBaseCore { /// /// The data to select from. /// [Parameter] public IEnumerable> Data { get; set; } = []; /// /// The selected values. /// [Parameter] public Func> SelectedValues { get; set; } = () => []; /// /// An action that is called when the selection changes. /// [Parameter] public Action> SelectionUpdate { get; set; } = _ => { }; /// /// An asynchronous action that is called when the selection changes. /// [Parameter] public Func, Task> SelectionUpdateAsync { get; set; } = _ => Task.CompletedTask; /// /// Determines whether a specific item is locked by a configuration plugin. /// [Parameter] public Func IsItemLocked { get; set; } = _ => false; [Parameter] public string? EmptySelectionText { get; set; } [Parameter] public string? SingleSelectionText { get; set; } [Parameter] public string? MultipleSelectionText { get; set; } #region Overrides of ConfigurationBase /// protected override bool Stretch => true; /// protected override Variant Variant => Variant.Outlined; /// protected override string Label => this.OptionDescription; #endregion private async Task OptionChanged(IEnumerable? updatedValues) { // OfType drops the nulls and gives back the non-nullable element type in one step, which // Where cannot: it keeps the nullable type no matter what the predicate proves. var selection = updatedValues is null ? [] : updatedValues.OfType().ToHashSet(); this.SelectionUpdate(selection); await this.SelectionUpdateAsync(selection); await this.SettingsManager.StoreSettings(); await this.InformAboutChange(); } private string GetMultiSelectionText(List? selectedValues) { if(selectedValues is null || selectedValues.Count == 0) return this.EmptySelectionText ?? T("No items selected."); if(selectedValues.Count == 1) return this.SingleSelectionText ?? T("You have selected 1 item."); return string.Format(this.MultipleSelectionText ?? T("You have selected {0} items."), selectedValues.Count); } private bool IsLockedValue(TData value) => this.IsItemLocked(value); private string LockedTooltip() => this.T( "This feature is managed by your organization and has therefore been disabled.", typeof(ConfigurationBase).Namespace, nameof(ConfigurationBase)); }