added display value of dropdowns to the state

This commit is contained in:
krut_ni 2026-04-14 10:56:56 +02:00
parent d494fe4bc7
commit ff32f8f184
No known key found for this signature in database
GPG Key ID: A5C0151B4DDB172C
2 changed files with 47 additions and 1 deletions

View File

@ -121,6 +121,26 @@ internal sealed class AssistantDropdown : StatefulAssistantComponentBase
#endregion
internal string ResolveDisplayText(string value)
{
if (string.IsNullOrWhiteSpace(value))
return this.Default.Display;
var item = this.GetRenderedItems().FirstOrDefault(item => string.Equals(item.Value, value, StringComparison.Ordinal));
return item?.Display ?? value;
}
private List<AssistantDropdownItem> GetRenderedItems()
{
if (string.IsNullOrWhiteSpace(this.Default.Value))
return this.Items;
if (this.Items.Any(item => string.Equals(item.Value, this.Default.Value, StringComparison.Ordinal)))
return this.Items;
return [this.Default, .. this.Items];
}
public IEnumerable<object> GetParsedDropdownValues()
{
foreach (var item in this.Items)

View File

@ -156,12 +156,17 @@ public sealed class AssistantState
{
if (component is INamedAssistantComponent named)
{
target[named.Name] = new LuaTable
var componentEntry = new LuaTable
{
["Type"] = Enum.GetName(component.Type) ?? string.Empty,
["Value"] = component is IStatefulAssistantComponent ? this.ReadValueForLua(named.Name) : LuaValue.Nil,
["Props"] = this.CreatePropsTable(component),
};
if (component is AssistantDropdown dropdown)
this.AddDropdownDisplay(componentEntry, dropdown, named.Name);
target[named.Name] = componentEntry;
}
if (component.Children.Count > 0)
@ -218,6 +223,27 @@ public sealed class AssistantState
return table;
}
private void AddDropdownDisplay(LuaTable componentEntry, AssistantDropdown dropdown, string name)
{
if (dropdown.IsMultiselect)
{
if (!this.MultiSelect.TryGetValue(name, out var selectedValues))
return;
componentEntry["Display"] = AssistantLuaConversion.CreateLuaArray(
selectedValues
.OrderBy(static value => value, StringComparer.Ordinal)
.Select(dropdown.ResolveDisplayText));
return;
}
if (!this.SingleSelect.TryGetValue(name, out var selectedValue))
return;
componentEntry["Display"] = dropdown.ResolveDisplayText(selectedValue);
}
private static HashSet<string> ReadStringValues(LuaTable values)
{
var parsedValues = new HashSet<string>(StringComparer.Ordinal);