@using RackPeek.Domain.Persistence
@using RackPeek.Domain.Resources.Connections
@using RackPeek.Domain.Resources.SubResources
@inject IResourceCollection Repository
@if (PortGroup is not null && !string.IsNullOrWhiteSpace(ResourceName))
{
@for (var i = 0; i < PortGroup.Count; i++)
{
var index = i;
var port = new PortReference
{
Resource = ResourceName,
PortGroup = PortGroupIndex,
PortIndex = index
};
var conn = GetConnection(port);
var selected = SelectedPortIndex == index;
SelectPort(index, port)">
@(index + 1)
}
}
@code {
[Parameter] public string ResourceName { get; set; } = "";
[Parameter] public int PortGroupIndex { get; set; }
[Parameter] public Port? PortGroup { get; set; }
[Parameter] public string? TestIdPrefix { get; set; }
[Parameter] public int? SelectedPortIndex { get; set; }
[Parameter] public EventCallback SelectedPortIndexChanged { get; set; }
[Parameter] public EventCallback OnPortClicked { get; set; }
private List _connections = new();
private string BaseTestId =>
string.IsNullOrWhiteSpace(TestIdPrefix)
? "port-visualizer"
: TestIdPrefix;
protected override async Task OnParametersSetAsync()
{
_connections = (await Repository.GetConnectionsAsync()).ToList();
}
async Task SelectPort(int index, PortReference port)
{
if (SelectedPortIndexChanged.HasDelegate)
await SelectedPortIndexChanged.InvokeAsync(index);
if (OnPortClicked.HasDelegate)
await OnPortClicked.InvokeAsync(port);
}
Connection? GetConnection(PortReference port)
{
return _connections.FirstOrDefault(c =>
(c.A.Resource == port.Resource &&
c.A.PortGroup == port.PortGroup &&
c.A.PortIndex == port.PortIndex)
||
(c.B.Resource == port.Resource &&
c.B.PortGroup == port.PortGroup &&
c.B.PortIndex == port.PortIndex));
}
string GetTooltip(Connection? conn, PortReference port)
{
if (conn == null)
return "Available";
var other =
conn.A.Resource == port.Resource &&
conn.A.PortGroup == port.PortGroup &&
conn.A.PortIndex == port.PortIndex
? conn.B
: conn.A;
return $"{other.Resource} (port {other.PortIndex + 1})";
}
}