| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409 |
- @using RackPeek.Domain.Persistence
- @using RackPeek.Domain.Resources
- @using RackPeek.Domain.Resources.Connections
- @using RackPeek.Domain.Resources.Servers
- @using RackPeek.Domain.Resources.SubResources
- @inject IResourceCollection Repository
- @inject IAddConnectionUseCase AddConnectionUseCase
- @if (IsOpen)
- {
- <div class="fixed inset-0 z-50 flex items-center justify-center">
- <div class="absolute inset-0 bg-black/70" @onclick="Cancel"></div>
- <div
- class="relative bg-zinc-900 border border-zinc-800 rounded w-full max-w-3xl p-4"
- data-testid="@($"{BaseTestId}-container")">
-
- <div class="flex justify-between mb-4">
- <div class="text-zinc-100 text-sm font-medium">
- Create Connection
- </div>
- <button class="text-zinc-400 hover:text-zinc-200"
- @onclick="Cancel">
- ✕
- </button>
- </div>
- <div class="grid grid-cols-2 gap-6 text-sm">
- <!-- SIDE A -->
- <div class="space-y-3">
- <div class="text-zinc-400">Side A</div>
- <select class="w-full bg-zinc-800 border border-zinc-700 rounded px-2 py-1 text-zinc-100"
- data-testid="@($"{BaseTestId}-resource-a")"
- @bind="_resourceAIndex">
- <option value="">Select resource</option>
- @for (var i = 0; i < HardwareWithPorts.Count; i++)
- {
- var hw = (Resource)HardwareWithPorts[i];
- <option value="@i">@hw.Name</option>
- }
- </select>
- @if (_resourceA?.Ports?.Any() == true)
- {
- <select class="w-full bg-zinc-800 border border-zinc-700 rounded px-2 py-1 text-zinc-100"
- data-testid="@($"{BaseTestId}-group-a")"
- @bind="_groupAIndex">
- <option value="">Select group</option>
- @for (var i = 0; i < _resourceA.Ports.Count; i++)
- {
- var g = _resourceA.Ports[i];
- <option value="@i">@g.Type — @g.Speed Gbps (@g.Count)</option>
- }
- </select>
- }
- @if (_groupA is not null)
- {
- <select class="w-full bg-zinc-800 border border-zinc-700 rounded px-2 py-1 text-zinc-100"
- data-testid="@($"{BaseTestId}-port-a")"
- @bind="_portAIndex">
- <option value="">Select port</option>
- @for (var i = 0; i < _groupA.Count; i++)
- {
- <option value="@i">Port @(i + 1)</option>
- }
- </select>
- <PortGroupVisualizer
- ResourceName="@_portA.Resource"
- PortGroupIndex="@_portA.PortGroup"
- PortGroup="@_groupA"
- @bind-SelectedPortIndex="_portAIndex"
- OnPortClicked="HandleLeftPortClicked"/>
- }
- </div>
- <!-- SIDE B -->
- <div class="space-y-3">
- <div class="text-zinc-400">Side B</div>
- <select class="w-full bg-zinc-800 border border-zinc-700 rounded px-2 py-1 text-zinc-100"
- data-testid="@($"{BaseTestId}-resource-b")"
- @bind="_resourceBIndex">
- <option value="">Select resource</option>
- @for (var i = 0; i < HardwareWithPorts.Count; i++)
- {
- var hw = (Resource)HardwareWithPorts[i];
- <option value="@i">@hw.Name</option>
- }
- </select>
- @if (_resourceB?.Ports?.Any() == true)
- {
- <select class="w-full bg-zinc-800 border border-zinc-700 rounded px-2 py-1 text-zinc-100"
- data-testid="@($"{BaseTestId}-group-b")"
- @bind="_groupBIndex">
- <option value="">Select group</option>
- @for (var i = 0; i < _resourceB.Ports.Count; i++)
- {
- var g = _resourceB.Ports[i];
- <option value="@i">@g.Type — @g.Speed Gbps (@g.Count)</option>
- }
- </select>
- }
- @if (_groupB is not null)
- {
- <select class="w-full bg-zinc-800 border border-zinc-700 rounded px-2 py-1 text-zinc-100"
- data-testid="@($"{BaseTestId}-port-b")"
- @bind="_portBIndex">
- <option value="">Select port</option>
- @for (var i = 0; i < _groupB.Count; i++)
- {
- <option value="@i">Port @(i + 1)</option>
- }
- </select>
- <PortGroupVisualizer
- ResourceName="@_portB.Resource"
- PortGroupIndex="@_portB.PortGroup"
- PortGroup="@_groupB"
- @bind-SelectedPortIndex="_portBIndex"
- OnPortClicked="HandleRightPortClicked"/>
- }
- </div>
- </div>
- <div class="flex justify-end gap-2 mt-6">
- <button class="px-3 py-1 border border-zinc-700 rounded text-zinc-300"
- data-testid="@($"{BaseTestId}-cancel")"
- @onclick="Cancel">
- Cancel
- </button>
- <button class="px-3 py-1 rounded bg-emerald-600 text-black"
- disabled="@(!CanSubmit)"
- data-testid="@($"{BaseTestId}-submit")"
- @onclick="HandleSubmit">
- Add Connection
- </button>
- </div>
- </div>
- </div>
- }
- @code {
- [Parameter] public bool IsOpen { get; set; }
- [Parameter] public EventCallback<bool> IsOpenChanged { get; set; }
- [Parameter] public string? TestIdPrefix { get; set; }
- private string BaseTestId =>
- string.IsNullOrWhiteSpace(TestIdPrefix)
- ? "connection-modal"
- : $"{TestIdPrefix}-connection-modal";
- [Parameter] public PortReference? SeedPort { get; set; }
- List<IPortResource> HardwareWithPorts = new();
- IPortResource? _resourceA;
- IPortResource? _resourceB;
- Port? _groupA;
- Port? _groupB;
- readonly PortReference _portA = new();
- readonly PortReference _portB = new();
- int? _resourceAIndexValue;
- int? _resourceBIndexValue;
- int? _groupAIndexValue;
- int? _groupBIndexValue;
- int? _portAIndex;
- int? _portBIndex;
- int? _resourceAIndex
- {
- get => _resourceAIndexValue;
- set
- {
- _resourceAIndexValue = value;
- if (value is null)
- {
- _resourceA = null;
- _groupA = null;
- _portAIndex = null;
- return;
- }
- _resourceA = HardwareWithPorts[value.Value];
- _portA.Resource = ((Resource)_resourceA).Name;
- _groupAIndex = null;
- _portAIndex = null;
- }
- }
- int? _resourceBIndex
- {
- get => _resourceBIndexValue;
- set
- {
- _resourceBIndexValue = value;
- if (value is null)
- {
- _resourceB = null;
- _groupB = null;
- _portBIndex = null;
- return;
- }
- _resourceB = HardwareWithPorts[value.Value];
- _portB.Resource = ((Resource)_resourceB).Name;
- _groupBIndex = null;
- _portBIndex = null;
- }
- }
- int? _groupAIndex
- {
- get => _groupAIndexValue;
- set
- {
- _groupAIndexValue = value;
- if (value is null || _resourceA == null)
- {
- _groupA = null;
- _portAIndex = null;
- return;
- }
- _groupA = _resourceA.Ports![value.Value];
- _portA.PortGroup = value.Value;
- _portAIndex = null;
- }
- }
- int? _groupBIndex
- {
- get => _groupBIndexValue;
- set
- {
- _groupBIndexValue = value;
- if (value is null || _resourceB == null)
- {
- _groupB = null;
- _portBIndex = null;
- return;
- }
- _groupB = _resourceB.Ports![value.Value];
- _portB.PortGroup = value.Value;
- _portBIndex = null;
- }
- }
- bool CanSubmit =>
- _groupA != null &&
- _groupB != null &&
- _portAIndex != null &&
- _portBIndex != null;
- protected override async Task OnParametersSetAsync()
- {
- if (!IsOpen) return;
- var all = await Repository.GetAllOfTypeAsync<IPortResource>();
- HardwareWithPorts = all
- .Where(h => h.Ports?.Any() == true)
- .ToList();
- if (SeedPort != null)
- SeedSinglePortA(SeedPort);
- }
- async Task HandleLeftPortClicked(PortReference port)
- {
- var existing = await Repository.GetConnectionForPortAsync(port);
- if (existing != null)
- SeedConnection(existing);
- else
- SeedSinglePortA(port);
- }
- async Task HandleRightPortClicked(PortReference port)
- {
- var existing = await Repository.GetConnectionForPortAsync(port);
- if (existing != null)
- SeedConnection(existing);
- else
- SeedSinglePortB(port);
- }
- void SeedSinglePortA(PortReference port)
- {
- _resourceAIndex = HardwareWithPorts.FindIndex(r => ((Resource)r).Name == port.Resource);
- _groupAIndex = port.PortGroup;
- _portAIndex = port.PortIndex;
- }
- void SeedSinglePortB(PortReference port)
- {
- _resourceBIndex = HardwareWithPorts.FindIndex(r => ((Resource)r).Name == port.Resource);
- _groupBIndex = port.PortGroup;
- _portBIndex = port.PortIndex;
- }
- void SeedConnection(Connection conn)
- {
- SeedSinglePortA(conn.A);
- SeedSinglePortB(conn.B);
- }
- async Task HandleSubmit()
- {
- if (!CanSubmit) return;
- var a = new PortReference
- {
- Resource = _portA.Resource,
- PortGroup = _portA.PortGroup,
- PortIndex = _portAIndex!.Value
- };
- var b = new PortReference
- {
- Resource = _portB.Resource,
- PortGroup = _portB.PortGroup,
- PortIndex = _portBIndex!.Value
- };
- await AddConnectionUseCase.ExecuteAsync(a, b);
- await Cancel();
- }
- async Task Cancel()
- {
- await IsOpenChanged.InvokeAsync(false);
- }
- }
|