PortGroupVisualizer.razor 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. @using RackPeek.Domain.Persistence
  2. @using RackPeek.Domain.Resources.Connections
  3. @using RackPeek.Domain.Resources.SubResources
  4. @inject IResourceCollection Repository
  5. @if (PortGroup is not null && !string.IsNullOrWhiteSpace(ResourceName))
  6. {
  7. <div class="flex flex-wrap">
  8. @for (var i = 0; i < PortGroup.Count; i++)
  9. {
  10. var index = i;
  11. var port = new PortReference
  12. {
  13. Resource = ResourceName,
  14. PortGroup = PortGroupIndex,
  15. PortIndex = index
  16. };
  17. var conn = GetConnection(port);
  18. var selected = SelectedPortIndex == index;
  19. <button type="button"
  20. data-testid="@($"{BaseTestId}-port-{index}")"
  21. title="@GetTooltip(conn, port)"
  22. class="group flex flex-col items-center w-6 leading-none"
  23. @onclick="() => SelectPort(index, port)">
  24. <div class="w-6 h-3 flex items-center justify-center
  25. shadow-inner
  26. border-t border-b border-r
  27. @(index == 0 ? "border-l" : "")
  28. @(selected
  29. ? "bg-emerald-500 border-emerald-400"
  30. : conn != null
  31. ? "bg-blue-600 border-blue-500"
  32. : "bg-zinc-800 border-zinc-700 group-hover:bg-zinc-700")">
  33. <div class="w-2 h-[1.5px]
  34. @(selected
  35. ? "bg-black"
  36. : conn != null
  37. ? "bg-blue-200"
  38. : "bg-zinc-600")">
  39. </div>
  40. </div>
  41. <div class="text-[8px] text-zinc-500 mt-[1px]">
  42. @(index + 1)
  43. </div>
  44. </button>
  45. }
  46. </div>
  47. }
  48. @code {
  49. [Parameter] public string ResourceName { get; set; } = "";
  50. [Parameter] public int PortGroupIndex { get; set; }
  51. [Parameter] public Port? PortGroup { get; set; }
  52. [Parameter] public string? TestIdPrefix { get; set; }
  53. [Parameter] public int? SelectedPortIndex { get; set; }
  54. [Parameter] public EventCallback<int?> SelectedPortIndexChanged { get; set; }
  55. [Parameter] public EventCallback<PortReference> OnPortClicked { get; set; }
  56. private List<Connection> _connections = new();
  57. private string BaseTestId =>
  58. string.IsNullOrWhiteSpace(TestIdPrefix)
  59. ? "port-visualizer"
  60. : TestIdPrefix;
  61. protected override async Task OnParametersSetAsync()
  62. {
  63. _connections = (await Repository.GetConnectionsAsync()).ToList();
  64. }
  65. async Task SelectPort(int index, PortReference port)
  66. {
  67. if (SelectedPortIndexChanged.HasDelegate)
  68. await SelectedPortIndexChanged.InvokeAsync(index);
  69. if (OnPortClicked.HasDelegate)
  70. await OnPortClicked.InvokeAsync(port);
  71. }
  72. Connection? GetConnection(PortReference port)
  73. {
  74. return _connections.FirstOrDefault(c =>
  75. (c.A.Resource == port.Resource &&
  76. c.A.PortGroup == port.PortGroup &&
  77. c.A.PortIndex == port.PortIndex)
  78. ||
  79. (c.B.Resource == port.Resource &&
  80. c.B.PortGroup == port.PortGroup &&
  81. c.B.PortIndex == port.PortIndex));
  82. }
  83. string GetTooltip(Connection? conn, PortReference port)
  84. {
  85. if (conn == null)
  86. return "Available";
  87. var other =
  88. conn.A.Resource == port.Resource &&
  89. conn.A.PortGroup == port.PortGroup &&
  90. conn.A.PortIndex == port.PortIndex
  91. ? conn.B
  92. : conn.A;
  93. return $"{other.Resource} (port {other.PortIndex + 1})";
  94. }
  95. }