| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- @page "/labels/{LabelName}"
- @using RackPeek.Domain.Persistence
- @using RackPeek.Domain.Resources
- @using RackPeek.Domain.Resources.SystemResources
- @inject IResourceCollection ResourceRepository
- <PageTitle>Label: @LabelName</PageTitle>
- <div class="min-h-screen bg-zinc-950 text-zinc-200 font-mono p-6 space-y-6">
- <!-- Header -->
- <div class="space-y-2">
- <h1 class="text-lg text-zinc-100">
- Label: <span class="text-emerald-400">@LabelName</span>
- </h1>
- </div>
- <!-- Controls -->
- @if (_resources is not null && _resources.Count > 0)
- {
- <div class="flex flex-wrap gap-4 items-center text-xs">
- <!-- Filter -->
- <input placeholder="filter resources..."
- class="bg-zinc-900 border border-zinc-800 px-3 py-1 rounded focus:outline-none focus:border-emerald-600"
- @bind="_filter"/>
- <!-- Sort By -->
- <select class="bg-zinc-900 border border-zinc-800 px-2 py-1 rounded"
- @bind="_sortBy">
- <option value="Value">Value</option>
- <option value="Name">Name</option>
- <option value="Kind">Kind</option>
- </select>
- <!-- Direction -->
- <button class="border border-zinc-800 px-3 py-1 rounded hover:border-emerald-600"
- @onclick="ToggleSortDirection">
- @(_ascending ? "Asc ↑" : "Desc ↓")
- </button>
- </div>
- }
- <!-- Content -->
- @if (_resources is null)
- {
- <div class="text-zinc-500">loading resources…</div>
- }
- else if (_resources.Count == 0)
- {
- <div class="text-zinc-500">no resources found for this label</div>
- }
- else if (!GetProcessed().Any())
- {
- <div class="text-zinc-500">no results match your filter</div>
- }
- else
- {
- <div class="space-y-3">
- @foreach (var item in GetProcessed())
- {
- var resource = item.Resource;
- var value = item.Value;
- <div class="border border-zinc-800 rounded p-3 bg-zinc-900 hover:border-emerald-700 transition">
- <NavLink href="@GetResourceUrl(resource)"
- class="block hover:text-emerald-300">
- <div class="flex justify-between items-center">
- <div class="text-zinc-100">
- @resource.Name
- </div>
- <div class="text-xs text-zinc-500 uppercase tracking-wide">
- @resource.Kind
- </div>
- </div>
- <!-- Label value -->
- <div class="mt-2 text-xs text-emerald-400 flex items-center gap-2">
- <span>
- Value: @FormatValue(value)
- </span>
- <span class="text-zinc-500 uppercase text-[10px] tracking-wide">
- [@DetectType(value)]
- </span>
- </div>
- </NavLink>
- </div>
- }
- </div>
- }
- </div>
- @code {
- [Parameter] public string LabelName { get; set; } = string.Empty;
- private IReadOnlyList<(Resource Resource, string Value)>? _resources;
- private string _sortBy = "Value";
- private bool _ascending = true;
- private string _filter = string.Empty;
- protected override async Task OnParametersSetAsync()
- {
- var decoded = Uri.UnescapeDataString(LabelName);
- _resources = await ResourceRepository.GetByLabelAsync(decoded);
- }
- private IEnumerable<(Resource Resource, string Value)> GetProcessed()
- {
- if (_resources is null)
- return [];
- var query = _resources.AsEnumerable();
- // Filtering
- if (!string.IsNullOrWhiteSpace(_filter))
- {
- query = query.Where(r =>
- r.Resource.Name.Contains(_filter, StringComparison.OrdinalIgnoreCase) ||
- r.Value.Contains(_filter, StringComparison.OrdinalIgnoreCase));
- }
- // Sorting
- query = _sortBy switch
- {
- "Value" => query
- .OrderBy(r => GetSortKey(r.Value))
- .ThenBy(r => r.Resource.Name),
- "Kind" => query.OrderBy(r => r.Resource.Kind),
- _ => query.OrderBy(r => r.Resource.Name)
- };
- if (!_ascending)
- query = query.Reverse();
- return query.ToList();
- }
- private void ToggleSortDirection()
- {
- _ascending = !_ascending;
- }
- private static string DetectType(string value)
- {
- if (DateTime.TryParse(value, out _)) return "DATE";
- if (bool.TryParse(value, out _)) return "BOOL";
- if (decimal.TryParse(value, out _)) return "NUMBER";
- return "TEXT";
- }
- private static string FormatValue(string value)
- {
- if (DateTime.TryParse(value, out var dt))
- return dt.ToString("yyyy-MM-dd");
- return value;
- }
- private string GetResourceUrl(Resource resource)
- {
- if (resource.Kind == SystemResource.KindLabel)
- return $"resources/systems/{Uri.EscapeDataString(resource.Name)}";
- if (resource.Kind == Service.KindLabel)
- return $"resources/services/{Uri.EscapeDataString(resource.Name)}";
- return $"resources/hardware/{Uri.EscapeDataString(resource.Name)}";
- }
- private readonly record struct SortKey(int TypeRank, object Value, string Fallback)
- : IComparable<SortKey>
- {
- public int CompareTo(SortKey other)
- {
- var rank = TypeRank.CompareTo(other.TypeRank);
- if (rank != 0) return rank;
- var valueCompare = TypeRank switch
- {
- 0 => ((int)Value).CompareTo((int)other.Value),
- 1 => ((DateTime)Value).CompareTo((DateTime)other.Value),
- 2 => ((decimal)Value).CompareTo((decimal)other.Value),
- 3 => ((bool)Value).CompareTo((bool)other.Value),
- _ => string.Compare((string)Value, (string)other.Value, StringComparison.Ordinal)
- };
- if (valueCompare != 0) return valueCompare;
- // Deterministic tie-breaker
- return string.Compare(Fallback, other.Fallback, StringComparison.Ordinal);
- }
- }
- private static SortKey GetSortKey(string? raw)
- {
- var s = raw?.Trim() ?? string.Empty;
- if (string.IsNullOrEmpty(s))
- return new SortKey(0, 0, "");
- // Date
- if (DateTime.TryParse(s, out var dt))
- return new SortKey(1, dt, s);
- // Number
- if (decimal.TryParse(s, out var dec))
- return new SortKey(2, dec, s);
- // Bool
- if (bool.TryParse(s, out var b))
- return new SortKey(3, b, s);
- // Text (case-insensitive sort by normalizing)
- return new SortKey(4, s.ToLowerInvariant(), s);
- }
- }
|