From a883823051b668333908d42dcc3e927e5746f9c8 Mon Sep 17 00:00:00 2001 From: dingo Date: Wed, 8 Apr 2026 14:35:50 -0400 Subject: [PATCH] Fix host relation matching and display in related record panels, Resolve host fields through HostType/HostId, add numeric-aware ID matching for relationship links, and restore host names in related grids instead of raw host IDs. --- .../Shared/RelatedRecordsPanel.razor | 116 +++++++++++++----- 1 file changed, 84 insertions(+), 32 deletions(-) diff --git a/src/MaxInventory.Web/Components/Shared/RelatedRecordsPanel.razor b/src/MaxInventory.Web/Components/Shared/RelatedRecordsPanel.razor index 8f9464d..5e320ce 100644 --- a/src/MaxInventory.Web/Components/Shared/RelatedRecordsPanel.razor +++ b/src/MaxInventory.Web/Components/Shared/RelatedRecordsPanel.razor @@ -440,6 +440,11 @@ private string GetDisplayValue(IInventoryEntity entity, FieldDefinition field) { var rawValue = EntityValueResolver.GetValue(entity, field.PropertyName, field.JsonPropertyName); + if (string.Equals(field.PropertyName, "HostName", StringComparison.OrdinalIgnoreCase)) + { + return ResolveHostDisplay(entity, rawValue); + } + if (string.IsNullOrEmpty(rawValue)) return string.Empty; @@ -467,39 +472,8 @@ return GetDisplayValue(entity, field); } - var hostType = GetPropertyValue(entity, "HostType"); - var hostId = GetPropertyValue(entity, "HostId"); var hostName = EntityValueResolver.GetValue(entity, field.PropertyName, field.JsonPropertyName); - - if (string.IsNullOrWhiteSpace(hostType) || string.IsNullOrWhiteSpace(hostId)) - { - return hostName; - } - - var hostContentType = TryMapHostTypeToContentType(hostType); - if (hostContentType is null) - { - return string.IsNullOrWhiteSpace(hostName) - ? $"{hostType} #{hostId}" - : $"{hostType} #{hostId} ({hostName})"; - } - - if (AllData.TryGetValue(hostContentType.Value, out var relatedEntities)) - { - var hostEntity = relatedEntities.FirstOrDefault(item => - string.Equals(item.Id.ToString(), hostId, StringComparison.OrdinalIgnoreCase)); - - if (hostEntity is not null) - { - var hostDefinition = ContentTypeRegistry.GetDefinition(hostContentType.Value); - var displayName = GetEntityDisplayName(hostEntity, hostDefinition); - return $"{hostDefinition.DisplayName}: {displayName}"; - } - } - - return string.IsNullOrWhiteSpace(hostName) - ? $"{hostType} #{hostId}" - : $"{hostType} #{hostId} ({hostName})"; + return ResolveHostDisplay(entity, hostName); } private List SortRelatedItems(List items, ContentTypeDefinition definition) @@ -957,9 +931,87 @@ return HostTypesMatch(left, right); } + if (propertyName.EndsWith("Id", StringComparison.OrdinalIgnoreCase)) + { + return IdsMatch(left, right); + } + return string.Equals(left, right, StringComparison.OrdinalIgnoreCase); } + private string ResolveHostDisplay(IInventoryEntity entity, string? fallbackHostName) + { + var hostType = GetPropertyValue(entity, "HostType"); + var hostId = GetPropertyValue(entity, "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}" + : $"{hostType} #{hostId} ({fallbackHostName})"; + } + + if (AllData.TryGetValue(hostContentType.Value, out var relatedEntities)) + { + var hostEntity = relatedEntities.FirstOrDefault(item => IdsMatch(item.Id.ToString(), hostId)); + + if (hostEntity is not null) + { + var hostDefinition = ContentTypeRegistry.GetDefinition(hostContentType.Value); + var displayName = GetEntityDisplayName(hostEntity, hostDefinition); + return $"{hostDefinition.DisplayName}: {displayName}"; + } + } + + return string.IsNullOrWhiteSpace(fallbackHostName) + ? $"{hostType} #{hostId}" + : $"{hostType} #{hostId} ({fallbackHostName})"; + } + + 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; + } + private static bool HostTypesMatch(string? left, string? right) { var leftType = NormalizeHostType(left);