LabelPage.razor 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. @page "/labels/{LabelName}"
  2. @using RackPeek.Domain.Persistence
  3. @using RackPeek.Domain.Resources
  4. @using RackPeek.Domain.Resources.SystemResources
  5. @inject IResourceCollection ResourceRepository
  6. <PageTitle>Label: @LabelName</PageTitle>
  7. <div class="min-h-screen bg-zinc-950 text-zinc-200 font-mono p-6 space-y-6">
  8. <!-- Header -->
  9. <div class="space-y-2">
  10. <h1 class="text-lg text-zinc-100">
  11. Label: <span class="text-emerald-400">@LabelName</span>
  12. </h1>
  13. </div>
  14. <!-- Controls -->
  15. @if (_resources is not null && _resources.Count > 0)
  16. {
  17. <div class="flex flex-wrap gap-4 items-center text-xs">
  18. <!-- Filter -->
  19. <input placeholder="filter resources..."
  20. class="bg-zinc-900 border border-zinc-800 px-3 py-1 rounded focus:outline-none focus:border-emerald-600"
  21. @bind="_filter"/>
  22. <!-- Sort By -->
  23. <select class="bg-zinc-900 border border-zinc-800 px-2 py-1 rounded"
  24. @bind="_sortBy">
  25. <option value="Value">Value</option>
  26. <option value="Name">Name</option>
  27. <option value="Kind">Kind</option>
  28. </select>
  29. <!-- Direction -->
  30. <button class="border border-zinc-800 px-3 py-1 rounded hover:border-emerald-600"
  31. @onclick="ToggleSortDirection">
  32. @(_ascending ? "Asc ↑" : "Desc ↓")
  33. </button>
  34. </div>
  35. }
  36. <!-- Content -->
  37. @if (_resources is null)
  38. {
  39. <div class="text-zinc-500">loading resources…</div>
  40. }
  41. else if (_resources.Count == 0)
  42. {
  43. <div class="text-zinc-500">no resources found for this label</div>
  44. }
  45. else if (!GetProcessed().Any())
  46. {
  47. <div class="text-zinc-500">no results match your filter</div>
  48. }
  49. else
  50. {
  51. <div class="space-y-3">
  52. @foreach (var item in GetProcessed())
  53. {
  54. var resource = item.Resource;
  55. var value = item.Value;
  56. <div class="border border-zinc-800 rounded p-3 bg-zinc-900 hover:border-emerald-700 transition">
  57. <NavLink href="@GetResourceUrl(resource)"
  58. class="block hover:text-emerald-300">
  59. <div class="flex justify-between items-center">
  60. <div class="text-zinc-100">
  61. @resource.Name
  62. </div>
  63. <div class="text-xs text-zinc-500 uppercase tracking-wide">
  64. @resource.Kind
  65. </div>
  66. </div>
  67. <!-- Label value -->
  68. <div class="mt-2 text-xs text-emerald-400 flex items-center gap-2">
  69. <span>
  70. Value: @FormatValue(value)
  71. </span>
  72. <span class="text-zinc-500 uppercase text-[10px] tracking-wide">
  73. [@DetectType(value)]
  74. </span>
  75. </div>
  76. </NavLink>
  77. </div>
  78. }
  79. </div>
  80. }
  81. </div>
  82. @code {
  83. [Parameter] public string LabelName { get; set; } = string.Empty;
  84. private IReadOnlyList<(Resource Resource, string Value)>? _resources;
  85. private string _sortBy = "Value";
  86. private bool _ascending = true;
  87. private string _filter = string.Empty;
  88. protected override async Task OnParametersSetAsync()
  89. {
  90. var decoded = Uri.UnescapeDataString(LabelName);
  91. _resources = await ResourceRepository.GetByLabelAsync(decoded);
  92. }
  93. private IEnumerable<(Resource Resource, string Value)> GetProcessed()
  94. {
  95. if (_resources is null)
  96. return [];
  97. var query = _resources.AsEnumerable();
  98. // Filtering
  99. if (!string.IsNullOrWhiteSpace(_filter))
  100. {
  101. query = query.Where(r =>
  102. r.Resource.Name.Contains(_filter, StringComparison.OrdinalIgnoreCase) ||
  103. r.Value.Contains(_filter, StringComparison.OrdinalIgnoreCase));
  104. }
  105. // Sorting
  106. query = _sortBy switch
  107. {
  108. "Value" => query
  109. .OrderBy(r => GetSortKey(r.Value))
  110. .ThenBy(r => r.Resource.Name),
  111. "Kind" => query.OrderBy(r => r.Resource.Kind),
  112. _ => query.OrderBy(r => r.Resource.Name)
  113. };
  114. if (!_ascending)
  115. query = query.Reverse();
  116. return query.ToList();
  117. }
  118. private void ToggleSortDirection()
  119. {
  120. _ascending = !_ascending;
  121. }
  122. private static string DetectType(string value)
  123. {
  124. if (DateTime.TryParse(value, out _)) return "DATE";
  125. if (bool.TryParse(value, out _)) return "BOOL";
  126. if (decimal.TryParse(value, out _)) return "NUMBER";
  127. return "TEXT";
  128. }
  129. private static string FormatValue(string value)
  130. {
  131. if (DateTime.TryParse(value, out var dt))
  132. return dt.ToString("yyyy-MM-dd");
  133. return value;
  134. }
  135. private string GetResourceUrl(Resource resource)
  136. {
  137. if (resource.Kind == SystemResource.KindLabel)
  138. return $"resources/systems/{Uri.EscapeDataString(resource.Name)}";
  139. if (resource.Kind == Service.KindLabel)
  140. return $"resources/services/{Uri.EscapeDataString(resource.Name)}";
  141. return $"resources/hardware/{Uri.EscapeDataString(resource.Name)}";
  142. }
  143. private readonly record struct SortKey(int TypeRank, object Value, string Fallback)
  144. : IComparable<SortKey>
  145. {
  146. public int CompareTo(SortKey other)
  147. {
  148. var rank = TypeRank.CompareTo(other.TypeRank);
  149. if (rank != 0) return rank;
  150. var valueCompare = TypeRank switch
  151. {
  152. 0 => ((int)Value).CompareTo((int)other.Value),
  153. 1 => ((DateTime)Value).CompareTo((DateTime)other.Value),
  154. 2 => ((decimal)Value).CompareTo((decimal)other.Value),
  155. 3 => ((bool)Value).CompareTo((bool)other.Value),
  156. _ => string.Compare((string)Value, (string)other.Value, StringComparison.Ordinal)
  157. };
  158. if (valueCompare != 0) return valueCompare;
  159. // Deterministic tie-breaker
  160. return string.Compare(Fallback, other.Fallback, StringComparison.Ordinal);
  161. }
  162. }
  163. private static SortKey GetSortKey(string? raw)
  164. {
  165. var s = raw?.Trim() ?? string.Empty;
  166. if (string.IsNullOrEmpty(s))
  167. return new SortKey(0, 0, "");
  168. // Date
  169. if (DateTime.TryParse(s, out var dt))
  170. return new SortKey(1, dt, s);
  171. // Number
  172. if (decimal.TryParse(s, out var dec))
  173. return new SortKey(2, dec, s);
  174. // Bool
  175. if (bool.TryParse(s, out var b))
  176. return new SortKey(3, b, s);
  177. // Text (case-insensitive sort by normalizing)
  178. return new SortKey(4, s.ToLowerInvariant(), s);
  179. }
  180. }