fix main pages host linkage

This commit is contained in:
dingo
2026-04-08 15:51:06 -04:00
parent a883823051
commit 617f93214e
3 changed files with 116 additions and 1 deletions
@@ -31,6 +31,8 @@ else
<MaxInventory.Web.Components.Shared.InventoryDataGrid Items="@_items"
Fields="@_definition.Fields"
ContentType="@_definition.ContentType"
AllData="@_dataCache"
SelectedItem="@_selectedItem"
OnRowSelect="SelectItem"
OnEdit="ShowEditForm"
@@ -112,6 +112,7 @@ public partial class ContentTypePage : ComponentBase
{
_items = await LoadEntitiesAsync(_definition);
_dataCache[_definition.ContentType] = _items.AsReadOnly();
await PreloadGridReferenceDataAsync(_definition.ContentType);
}
catch (Exception ex)
{
@@ -155,6 +156,25 @@ public partial class ContentTypePage : ComponentBase
}
}
private async Task PreloadGridReferenceDataAsync(ContentTypeName contentType)
{
switch (contentType)
{
case ContentTypeName.Systems:
await EnsureCachedAsync(ContentTypeName.Devices);
break;
case ContentTypeName.Vms:
await EnsureCachedAsync(ContentTypeName.Systems);
await EnsureCachedAsync(ContentTypeName.Devices);
break;
case ContentTypeName.Ips:
await EnsureCachedAsync(ContentTypeName.Systems);
await EnsureCachedAsync(ContentTypeName.Devices);
await EnsureCachedAsync(ContentTypeName.Vms);
break;
}
}
/// <summary>
/// Loads entities for a content type using reflection to call the generic API method.
/// </summary>
@@ -15,6 +15,8 @@ public partial class InventoryDataGrid : ComponentBase
{
[Parameter] public required IReadOnlyList<IInventoryEntity> Items { get; set; }
[Parameter] public required IReadOnlyList<FieldDefinition> Fields { get; set; }
[Parameter] public ContentTypeName ContentType { get; set; }
[Parameter] public Dictionary<ContentTypeName, IReadOnlyList<IInventoryEntity>> AllData { get; set; } = new();
[Parameter] public EventCallback<IInventoryEntity> OnEdit { get; set; }
[Parameter] public EventCallback<IInventoryEntity> OnDelete { get; set; }
[Parameter] public EventCallback OnAdd { get; set; }
@@ -25,6 +27,7 @@ public partial class InventoryDataGrid : ComponentBase
[Parameter] public bool CanEdit { get; set; } = false;
[Parameter] public bool CanDelete { get; set; } = false;
[Inject] public required IDropdownOptionRegistry DropdownRegistry { get; set; }
[Inject] public required IContentTypeRegistry ContentTypeRegistry { get; set; }
// ── Internal State ──────────────────────────────────────────────
private string _globalSearchText = "";
@@ -105,6 +108,11 @@ public partial class InventoryDataGrid : ComponentBase
{
var rawValue = GetPropertyValueString(item, field.PropertyName, field.JsonPropertyName);
if (string.Equals(field.PropertyName, "HostName", StringComparison.OrdinalIgnoreCase))
{
return ResolveHostDisplay(item, rawValue);
}
// For dropdown fields, show the display label if available
if (field.InputType == InputType.Dropdown &&
!string.IsNullOrEmpty(field.DropdownOptionSetName) &&
@@ -122,6 +130,91 @@ public partial class InventoryDataGrid : ComponentBase
return rawValue;
}
private string ResolveHostDisplay(IInventoryEntity item, string? fallbackHostName)
{
var hostType = GetPropertyValueString(item, "HostType");
var hostId = GetPropertyValueString(item, "HostId");
if (string.IsNullOrWhiteSpace(hostType) || string.IsNullOrWhiteSpace(hostId))
{
return fallbackHostName ?? string.Empty;
}
var hostContentType = TryMapHostTypeToContentType(hostType);
if (hostContentType is null)
{
return string.IsNullOrWhiteSpace(fallbackHostName)
? $"{hostType} #{hostId}"
: fallbackHostName;
}
if (AllData.TryGetValue(hostContentType.Value, out var relatedEntities))
{
var hostEntity = relatedEntities.FirstOrDefault(entity => IdsMatch(entity.Id.ToString(), hostId));
if (hostEntity is not null)
{
var definition = ContentTypeRegistry.GetDefinition(hostContentType.Value);
var displayField = definition.Fields.FirstOrDefault(f => f.PropertyName == definition.DisplayPropertyName);
var displayName = EntityValueResolver.GetValue(hostEntity, definition.DisplayPropertyName, displayField?.JsonPropertyName);
if (!string.IsNullOrWhiteSpace(displayName))
{
return displayName;
}
}
}
return string.IsNullOrWhiteSpace(fallbackHostName)
? $"{hostType} #{hostId}"
: fallbackHostName;
}
private static ContentTypeName? TryMapHostTypeToContentType(string hostType)
{
if (string.Equals(hostType, "System", StringComparison.OrdinalIgnoreCase))
return ContentTypeName.Systems;
if (string.Equals(hostType, "Device", StringComparison.OrdinalIgnoreCase))
return ContentTypeName.Devices;
if (string.Equals(hostType, "Vm", StringComparison.OrdinalIgnoreCase) ||
string.Equals(hostType, "VM", StringComparison.OrdinalIgnoreCase))
return ContentTypeName.Vms;
return null;
}
private static bool IdsMatch(string? left, string? right)
{
if (TryParseId(left, out var leftId) && TryParseId(right, out var rightId))
return leftId == rightId;
return string.Equals(left?.Trim(), right?.Trim(), StringComparison.OrdinalIgnoreCase);
}
private static bool TryParseId(string? input, out int value)
{
value = 0;
if (string.IsNullOrWhiteSpace(input))
return false;
input = input.Trim();
if (int.TryParse(input, out value))
return true;
if (decimal.TryParse(input, out var decimalValue) &&
decimalValue == decimal.Truncate(decimalValue) &&
decimalValue >= int.MinValue &&
decimalValue <= int.MaxValue)
{
value = decimal.ToInt32(decimalValue);
return true;
}
return false;
}
// ── Event Handlers ──────────────────────────────────────────────
private string GetColumnFilter(string propertyName)
@@ -160,4 +253,4 @@ public partial class InventoryDataGrid : ComponentBase
{
await OnRowSelect.InvokeAsync(item);
}
}
}