PortConnectionModal.razor 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. @using RackPeek.Domain.Persistence
  2. @using RackPeek.Domain.Resources
  3. @using RackPeek.Domain.Resources.Connections
  4. @using RackPeek.Domain.Resources.Servers
  5. @using RackPeek.Domain.Resources.SubResources
  6. @inject IResourceCollection Repository
  7. @inject IAddConnectionUseCase AddConnectionUseCase
  8. @if (IsOpen)
  9. {
  10. <div class="fixed inset-0 z-50 flex items-center justify-center">
  11. <div class="absolute inset-0 bg-black/70" @onclick="Cancel"></div>
  12. <div
  13. class="relative bg-zinc-900 border border-zinc-800 rounded w-full max-w-3xl p-4"
  14. data-testid="@($"{BaseTestId}-container")">
  15. <div class="flex justify-between mb-4">
  16. <div class="text-zinc-100 text-sm font-medium">
  17. Create Connection
  18. </div>
  19. <button class="text-zinc-400 hover:text-zinc-200"
  20. @onclick="Cancel">
  21. </button>
  22. </div>
  23. <div class="grid grid-cols-2 gap-6 text-sm">
  24. <!-- SIDE A -->
  25. <div class="space-y-3">
  26. <div class="text-zinc-400">Side A</div>
  27. <select class="w-full bg-zinc-800 border border-zinc-700 rounded px-2 py-1 text-zinc-100"
  28. data-testid="@($"{BaseTestId}-resource-a")"
  29. @bind="_resourceAIndex">
  30. <option value="">Select resource</option>
  31. @for (var i = 0; i < HardwareWithPorts.Count; i++)
  32. {
  33. var hw = (Resource)HardwareWithPorts[i];
  34. <option value="@i">@hw.Name</option>
  35. }
  36. </select>
  37. @if (_resourceA?.Ports?.Any() == true)
  38. {
  39. <select class="w-full bg-zinc-800 border border-zinc-700 rounded px-2 py-1 text-zinc-100"
  40. data-testid="@($"{BaseTestId}-group-a")"
  41. @bind="_groupAIndex">
  42. <option value="">Select group</option>
  43. @for (var i = 0; i < _resourceA.Ports.Count; i++)
  44. {
  45. var g = _resourceA.Ports[i];
  46. <option value="@i">@g.Type — @g.Speed Gbps (@g.Count)</option>
  47. }
  48. </select>
  49. }
  50. @if (_groupA is not null)
  51. {
  52. <select class="w-full bg-zinc-800 border border-zinc-700 rounded px-2 py-1 text-zinc-100"
  53. data-testid="@($"{BaseTestId}-port-a")"
  54. @bind="_portAIndex">
  55. <option value="">Select port</option>
  56. @for (var i = 0; i < _groupA.Count; i++)
  57. {
  58. <option value="@i">Port @(i + 1)</option>
  59. }
  60. </select>
  61. <PortGroupVisualizer
  62. ResourceName="@_portA.Resource"
  63. PortGroupIndex="@_portA.PortGroup"
  64. PortGroup="@_groupA"
  65. @bind-SelectedPortIndex="_portAIndex"
  66. OnPortClicked="HandleLeftPortClicked"/>
  67. }
  68. </div>
  69. <!-- SIDE B -->
  70. <div class="space-y-3">
  71. <div class="text-zinc-400">Side B</div>
  72. <select class="w-full bg-zinc-800 border border-zinc-700 rounded px-2 py-1 text-zinc-100"
  73. data-testid="@($"{BaseTestId}-resource-b")"
  74. @bind="_resourceBIndex">
  75. <option value="">Select resource</option>
  76. @for (var i = 0; i < HardwareWithPorts.Count; i++)
  77. {
  78. var hw = (Resource)HardwareWithPorts[i];
  79. <option value="@i">@hw.Name</option>
  80. }
  81. </select>
  82. @if (_resourceB?.Ports?.Any() == true)
  83. {
  84. <select class="w-full bg-zinc-800 border border-zinc-700 rounded px-2 py-1 text-zinc-100"
  85. data-testid="@($"{BaseTestId}-group-b")"
  86. @bind="_groupBIndex">
  87. <option value="">Select group</option>
  88. @for (var i = 0; i < _resourceB.Ports.Count; i++)
  89. {
  90. var g = _resourceB.Ports[i];
  91. <option value="@i">@g.Type — @g.Speed Gbps (@g.Count)</option>
  92. }
  93. </select>
  94. }
  95. @if (_groupB is not null)
  96. {
  97. <select class="w-full bg-zinc-800 border border-zinc-700 rounded px-2 py-1 text-zinc-100"
  98. data-testid="@($"{BaseTestId}-port-b")"
  99. @bind="_portBIndex">
  100. <option value="">Select port</option>
  101. @for (var i = 0; i < _groupB.Count; i++)
  102. {
  103. <option value="@i">Port @(i + 1)</option>
  104. }
  105. </select>
  106. <PortGroupVisualizer
  107. ResourceName="@_portB.Resource"
  108. PortGroupIndex="@_portB.PortGroup"
  109. PortGroup="@_groupB"
  110. @bind-SelectedPortIndex="_portBIndex"
  111. OnPortClicked="HandleRightPortClicked"/>
  112. }
  113. </div>
  114. </div>
  115. <div class="flex justify-end gap-2 mt-6">
  116. <button class="px-3 py-1 border border-zinc-700 rounded text-zinc-300"
  117. data-testid="@($"{BaseTestId}-cancel")"
  118. @onclick="Cancel">
  119. Cancel
  120. </button>
  121. <button class="px-3 py-1 rounded bg-emerald-600 text-black"
  122. disabled="@(!CanSubmit)"
  123. data-testid="@($"{BaseTestId}-submit")"
  124. @onclick="HandleSubmit">
  125. Add Connection
  126. </button>
  127. </div>
  128. </div>
  129. </div>
  130. }
  131. @code {
  132. [Parameter] public bool IsOpen { get; set; }
  133. [Parameter] public EventCallback<bool> IsOpenChanged { get; set; }
  134. [Parameter] public string? TestIdPrefix { get; set; }
  135. private string BaseTestId =>
  136. string.IsNullOrWhiteSpace(TestIdPrefix)
  137. ? "connection-modal"
  138. : $"{TestIdPrefix}-connection-modal";
  139. [Parameter] public PortReference? SeedPort { get; set; }
  140. List<IPortResource> HardwareWithPorts = new();
  141. IPortResource? _resourceA;
  142. IPortResource? _resourceB;
  143. Port? _groupA;
  144. Port? _groupB;
  145. readonly PortReference _portA = new();
  146. readonly PortReference _portB = new();
  147. int? _resourceAIndexValue;
  148. int? _resourceBIndexValue;
  149. int? _groupAIndexValue;
  150. int? _groupBIndexValue;
  151. int? _portAIndex;
  152. int? _portBIndex;
  153. int? _resourceAIndex
  154. {
  155. get => _resourceAIndexValue;
  156. set
  157. {
  158. _resourceAIndexValue = value;
  159. if (value is null)
  160. {
  161. _resourceA = null;
  162. _groupA = null;
  163. _portAIndex = null;
  164. return;
  165. }
  166. _resourceA = HardwareWithPorts[value.Value];
  167. _portA.Resource = ((Resource)_resourceA).Name;
  168. _groupAIndex = null;
  169. _portAIndex = null;
  170. }
  171. }
  172. int? _resourceBIndex
  173. {
  174. get => _resourceBIndexValue;
  175. set
  176. {
  177. _resourceBIndexValue = value;
  178. if (value is null)
  179. {
  180. _resourceB = null;
  181. _groupB = null;
  182. _portBIndex = null;
  183. return;
  184. }
  185. _resourceB = HardwareWithPorts[value.Value];
  186. _portB.Resource = ((Resource)_resourceB).Name;
  187. _groupBIndex = null;
  188. _portBIndex = null;
  189. }
  190. }
  191. int? _groupAIndex
  192. {
  193. get => _groupAIndexValue;
  194. set
  195. {
  196. _groupAIndexValue = value;
  197. if (value is null || _resourceA == null)
  198. {
  199. _groupA = null;
  200. _portAIndex = null;
  201. return;
  202. }
  203. _groupA = _resourceA.Ports![value.Value];
  204. _portA.PortGroup = value.Value;
  205. _portAIndex = null;
  206. }
  207. }
  208. int? _groupBIndex
  209. {
  210. get => _groupBIndexValue;
  211. set
  212. {
  213. _groupBIndexValue = value;
  214. if (value is null || _resourceB == null)
  215. {
  216. _groupB = null;
  217. _portBIndex = null;
  218. return;
  219. }
  220. _groupB = _resourceB.Ports![value.Value];
  221. _portB.PortGroup = value.Value;
  222. _portBIndex = null;
  223. }
  224. }
  225. bool CanSubmit =>
  226. _groupA != null &&
  227. _groupB != null &&
  228. _portAIndex != null &&
  229. _portBIndex != null;
  230. protected override async Task OnParametersSetAsync()
  231. {
  232. if (!IsOpen) return;
  233. var all = await Repository.GetAllOfTypeAsync<IPortResource>();
  234. HardwareWithPorts = all
  235. .Where(h => h.Ports?.Any() == true)
  236. .ToList();
  237. if (SeedPort != null)
  238. SeedSinglePortA(SeedPort);
  239. }
  240. async Task HandleLeftPortClicked(PortReference port)
  241. {
  242. var existing = await Repository.GetConnectionForPortAsync(port);
  243. if (existing != null)
  244. SeedConnection(existing);
  245. else
  246. SeedSinglePortA(port);
  247. }
  248. async Task HandleRightPortClicked(PortReference port)
  249. {
  250. var existing = await Repository.GetConnectionForPortAsync(port);
  251. if (existing != null)
  252. SeedConnection(existing);
  253. else
  254. SeedSinglePortB(port);
  255. }
  256. void SeedSinglePortA(PortReference port)
  257. {
  258. _resourceAIndex = HardwareWithPorts.FindIndex(r => ((Resource)r).Name == port.Resource);
  259. _groupAIndex = port.PortGroup;
  260. _portAIndex = port.PortIndex;
  261. }
  262. void SeedSinglePortB(PortReference port)
  263. {
  264. _resourceBIndex = HardwareWithPorts.FindIndex(r => ((Resource)r).Name == port.Resource);
  265. _groupBIndex = port.PortGroup;
  266. _portBIndex = port.PortIndex;
  267. }
  268. void SeedConnection(Connection conn)
  269. {
  270. SeedSinglePortA(conn.A);
  271. SeedSinglePortB(conn.B);
  272. }
  273. async Task HandleSubmit()
  274. {
  275. if (!CanSubmit) return;
  276. var a = new PortReference
  277. {
  278. Resource = _portA.Resource,
  279. PortGroup = _portA.PortGroup,
  280. PortIndex = _portAIndex!.Value
  281. };
  282. var b = new PortReference
  283. {
  284. Resource = _portB.Resource,
  285. PortGroup = _portB.PortGroup,
  286. PortIndex = _portBIndex!.Value
  287. };
  288. await AddConnectionUseCase.ExecuteAsync(a, b);
  289. await Cancel();
  290. }
  291. async Task Cancel()
  292. {
  293. await IsOpenChanged.InvokeAsync(false);
  294. }
  295. }