diff --git a/src/MaxInventory.Web/Components/Pages/ContentTypePage.razor b/src/MaxInventory.Web/Components/Pages/ContentTypePage.razor index 72d4285..e38ae1c 100644 --- a/src/MaxInventory.Web/Components/Pages/ContentTypePage.razor +++ b/src/MaxInventory.Web/Components/Pages/ContentTypePage.razor @@ -31,6 +31,8 @@ else /// Loads entities for a content type using reflection to call the generic API method. /// diff --git a/src/MaxInventory.Web/Components/Shared/InventoryDataGrid.razor.cs b/src/MaxInventory.Web/Components/Shared/InventoryDataGrid.razor.cs index c14b229..f02c4c3 100644 --- a/src/MaxInventory.Web/Components/Shared/InventoryDataGrid.razor.cs +++ b/src/MaxInventory.Web/Components/Shared/InventoryDataGrid.razor.cs @@ -15,6 +15,8 @@ public partial class InventoryDataGrid : ComponentBase { [Parameter] public required IReadOnlyList Items { get; set; } [Parameter] public required IReadOnlyList Fields { get; set; } + [Parameter] public ContentTypeName ContentType { get; set; } + [Parameter] public Dictionary> AllData { get; set; } = new(); [Parameter] public EventCallback OnEdit { get; set; } [Parameter] public EventCallback 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); } -} \ No newline at end of file +}