AI-Studio/app/MindWork AI Studio/Components/DynamicAssistantDropdown.razor.cs
nilskruthoff f6a128f2e4
Some checks are pending
Build and Release / Determine run mode (push) Waiting to run
Build and Release / Read metadata (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-apple-darwin, osx-arm64, macos-latest, aarch64-apple-darwin, dmg,updater, dmg) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-pc-windows-msvc.exe, win-arm64, windows-latest, aarch64-pc-windows-msvc, nsis,updater, nsis) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-unknown-linux-gnu, linux-arm64, ubuntu-22.04-arm, aarch64-unknown-linux-gnu, appimage,deb,updater, appimage,deb) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-apple-darwin, osx-x64, macos-latest, x86_64-apple-darwin, dmg,updater, dmg) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-pc-windows-msvc.exe, win-x64, windows-latest, x86_64-pc-windows-msvc, nsis,updater, nsis) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-unknown-linux-gnu, linux-x64, ubuntu-22.04, x86_64-unknown-linux-gnu, appimage,deb,updater, appimage,deb) (push) Blocked by required conditions
Build and Release / Prepare & create release (push) Blocked by required conditions
Build and Release / Publish release (push) Blocked by required conditions
Added assistant plugins (#659)
2026-04-09 10:01:24 +02:00

131 lines
4.2 KiB
C#

using AIStudio.Tools.PluginSystem.Assistants.DataModel;
using Microsoft.AspNetCore.Components;
namespace AIStudio.Components
{
public partial class DynamicAssistantDropdown : ComponentBase
{
[Parameter]
public List<AssistantDropdownItem> Items { get; set; } = new();
[Parameter]
public AssistantDropdownItem Default { get; set; } = new();
[Parameter]
public string Value { get; set; } = string.Empty;
[Parameter]
public EventCallback<string> ValueChanged { get; set; }
[Parameter]
public HashSet<string> SelectedValues { get; set; } = [];
[Parameter]
public EventCallback<HashSet<string>> SelectedValuesChanged { get; set; }
[Parameter]
public string Label { get; set; } = string.Empty;
[Parameter]
public string HelperText { get; set; } = string.Empty;
[Parameter]
public Func<string, string?> ValidateSelection { get; set; } = _ => null;
[Parameter]
public string OpenIcon { get; set; } = Icons.Material.Filled.ArrowDropDown;
[Parameter]
public string CloseIcon { get; set; } = Icons.Material.Filled.ArrowDropUp;
[Parameter]
public Color IconColor { get; set; } = Color.Default;
[Parameter]
public Adornment IconPosition { get; set; } = Adornment.End;
[Parameter]
public Variant Variant { get; set; } = Variant.Outlined;
[Parameter]
public bool IsMultiselect { get; set; }
[Parameter]
public bool HasSelectAll { get; set; }
[Parameter]
public string SelectAllText { get; set; } = string.Empty;
[Parameter]
public string Class { get; set; } = string.Empty;
[Parameter]
public string Style { get; set; } = string.Empty;
private async Task OnValueChanged(string newValue)
{
if (this.Value != newValue)
{
this.Value = newValue;
await this.ValueChanged.InvokeAsync(newValue);
}
}
private async Task OnSelectedValuesChanged(IEnumerable<string?>? newValues)
{
var updatedValues = newValues?
.Where(value => !string.IsNullOrWhiteSpace(value))
.Select(value => value!)
.ToHashSet(StringComparer.Ordinal) ?? [];
if (this.SelectedValues.SetEquals(updatedValues))
return;
this.SelectedValues = updatedValues;
await this.SelectedValuesChanged.InvokeAsync(updatedValues);
}
private List<AssistantDropdownItem> GetRenderedItems()
{
var items = this.Items;
if (string.IsNullOrWhiteSpace(this.Default.Value))
return items;
if (items.Any(item => string.Equals(item.Value, this.Default.Value, StringComparison.Ordinal)))
return items;
return [this.Default, .. items];
}
private string GetMultiSelectionText(List<string?>? selectedValues)
{
if (selectedValues is null || selectedValues.Count == 0)
return this.Default.Display;
var labels = selectedValues
.Where(value => !string.IsNullOrWhiteSpace(value))
.Select(value => this.ResolveDisplayText(value!))
.Where(value => !string.IsNullOrWhiteSpace(value))
.ToList();
return labels.Count == 0 ? this.Default.Display : string.Join(", ", labels);
}
private string ResolveDisplayText(string value)
{
var item = this.GetRenderedItems().FirstOrDefault(item => string.Equals(item.Value, value, StringComparison.Ordinal));
return item?.Display ?? value;
}
private static string MergeClasses(string custom, string fallback)
{
var trimmedCustom = custom.Trim();
var trimmedFallback = fallback.Trim();
if (string.IsNullOrEmpty(trimmedCustom))
return trimmedFallback;
return string.IsNullOrEmpty(trimmedFallback) ? trimmedCustom : $"{trimmedCustom} {trimmedFallback}";
}
}
}