4
0

PortConnectionModal.razor 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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="mt-6 space-y-1">
  116. <label class="text-zinc-400 text-xs uppercase tracking-wider"
  117. for="@($"{BaseTestId}-label-input")">
  118. Label
  119. </label>
  120. <input id="@($"{BaseTestId}-label-input")"
  121. class="w-full bg-zinc-800 border border-zinc-700 rounded px-2 py-1 text-zinc-100 text-sm"
  122. data-testid="@($"{BaseTestId}-label")"
  123. placeholder="optional — shown on the topology diagram"
  124. maxlength="80"
  125. @bind="_label"/>
  126. </div>
  127. <div class="flex justify-end gap-2 mt-6">
  128. <button class="px-3 py-1 border border-zinc-700 rounded text-zinc-300"
  129. data-testid="@($"{BaseTestId}-cancel")"
  130. @onclick="Cancel">
  131. Cancel
  132. </button>
  133. <button class="px-3 py-1 rounded bg-emerald-600 text-black"
  134. disabled="@(!CanSubmit)"
  135. data-testid="@($"{BaseTestId}-submit")"
  136. @onclick="HandleSubmit">
  137. Add Connection
  138. </button>
  139. </div>
  140. </div>
  141. </div>
  142. }
  143. @code {
  144. [Parameter] public bool IsOpen { get; set; }
  145. [Parameter] public EventCallback<bool> IsOpenChanged { get; set; }
  146. [Parameter] public string? TestIdPrefix { get; set; }
  147. private string BaseTestId =>
  148. string.IsNullOrWhiteSpace(TestIdPrefix)
  149. ? "connection-modal"
  150. : $"{TestIdPrefix}-connection-modal";
  151. [Parameter] public PortReference? SeedPort { get; set; }
  152. List<IPortResource> HardwareWithPorts = new();
  153. IPortResource? _resourceA;
  154. IPortResource? _resourceB;
  155. Port? _groupA;
  156. Port? _groupB;
  157. readonly PortReference _portA = new();
  158. readonly PortReference _portB = new();
  159. int? _resourceAIndexValue;
  160. int? _resourceBIndexValue;
  161. int? _groupAIndexValue;
  162. int? _groupBIndexValue;
  163. int? _portAIndex;
  164. int? _portBIndex;
  165. string _label = string.Empty;
  166. int? _resourceAIndex
  167. {
  168. get => _resourceAIndexValue;
  169. set
  170. {
  171. _resourceAIndexValue = value;
  172. if (value is null)
  173. {
  174. _resourceA = null;
  175. _groupA = null;
  176. _portAIndex = null;
  177. return;
  178. }
  179. _resourceA = HardwareWithPorts[value.Value];
  180. _portA.Resource = ((Resource)_resourceA).Name;
  181. _groupAIndex = null;
  182. _portAIndex = null;
  183. }
  184. }
  185. int? _resourceBIndex
  186. {
  187. get => _resourceBIndexValue;
  188. set
  189. {
  190. _resourceBIndexValue = value;
  191. if (value is null)
  192. {
  193. _resourceB = null;
  194. _groupB = null;
  195. _portBIndex = null;
  196. return;
  197. }
  198. _resourceB = HardwareWithPorts[value.Value];
  199. _portB.Resource = ((Resource)_resourceB).Name;
  200. _groupBIndex = null;
  201. _portBIndex = null;
  202. }
  203. }
  204. int? _groupAIndex
  205. {
  206. get => _groupAIndexValue;
  207. set
  208. {
  209. _groupAIndexValue = value;
  210. if (value is null || _resourceA == null)
  211. {
  212. _groupA = null;
  213. _portAIndex = null;
  214. return;
  215. }
  216. _groupA = _resourceA.Ports![value.Value];
  217. _portA.PortGroup = value.Value;
  218. _portAIndex = null;
  219. }
  220. }
  221. int? _groupBIndex
  222. {
  223. get => _groupBIndexValue;
  224. set
  225. {
  226. _groupBIndexValue = value;
  227. if (value is null || _resourceB == null)
  228. {
  229. _groupB = null;
  230. _portBIndex = null;
  231. return;
  232. }
  233. _groupB = _resourceB.Ports![value.Value];
  234. _portB.PortGroup = value.Value;
  235. _portBIndex = null;
  236. }
  237. }
  238. bool CanSubmit =>
  239. _groupA != null &&
  240. _groupB != null &&
  241. _portAIndex != null &&
  242. _portBIndex != null;
  243. protected override async Task OnParametersSetAsync()
  244. {
  245. if (!IsOpen) return;
  246. var all = await Repository.GetAllOfTypeAsync<IPortResource>();
  247. HardwareWithPorts = all
  248. .Where(h => h.Ports?.Any() == true)
  249. .ToList();
  250. if (SeedPort != null)
  251. {
  252. // If the seed port is already part of a connection, hydrate the
  253. // whole modal (side B + label) so the user can see and edit what
  254. // they already configured. Otherwise just preselect side A.
  255. var existing = await Repository.GetConnectionForPortAsync(SeedPort);
  256. if (existing != null)
  257. SeedConnection(existing);
  258. else
  259. SeedSinglePortA(SeedPort);
  260. }
  261. }
  262. async Task HandleLeftPortClicked(PortReference port)
  263. {
  264. var existing = await Repository.GetConnectionForPortAsync(port);
  265. if (existing != null)
  266. SeedConnection(existing);
  267. else
  268. SeedSinglePortA(port);
  269. }
  270. async Task HandleRightPortClicked(PortReference port)
  271. {
  272. var existing = await Repository.GetConnectionForPortAsync(port);
  273. if (existing != null)
  274. SeedConnection(existing);
  275. else
  276. SeedSinglePortB(port);
  277. }
  278. void SeedSinglePortA(PortReference port)
  279. {
  280. _resourceAIndex = HardwareWithPorts.FindIndex(r => ((Resource)r).Name == port.Resource);
  281. _groupAIndex = port.PortGroup;
  282. _portAIndex = port.PortIndex;
  283. }
  284. void SeedSinglePortB(PortReference port)
  285. {
  286. _resourceBIndex = HardwareWithPorts.FindIndex(r => ((Resource)r).Name == port.Resource);
  287. _groupBIndex = port.PortGroup;
  288. _portBIndex = port.PortIndex;
  289. }
  290. void SeedConnection(Connection conn)
  291. {
  292. SeedSinglePortA(conn.A);
  293. SeedSinglePortB(conn.B);
  294. _label = conn.Label ?? string.Empty;
  295. }
  296. async Task HandleSubmit()
  297. {
  298. if (!CanSubmit) return;
  299. var a = new PortReference
  300. {
  301. Resource = _portA.Resource,
  302. PortGroup = _portA.PortGroup,
  303. PortIndex = _portAIndex!.Value
  304. };
  305. var b = new PortReference
  306. {
  307. Resource = _portB.Resource,
  308. PortGroup = _portB.PortGroup,
  309. PortIndex = _portBIndex!.Value
  310. };
  311. var trimmedLabel = string.IsNullOrWhiteSpace(_label) ? null : _label.Trim();
  312. await AddConnectionUseCase.ExecuteAsync(a, b, trimmedLabel);
  313. await Cancel();
  314. }
  315. async Task Cancel()
  316. {
  317. _label = string.Empty;
  318. await IsOpenChanged.InvokeAsync(false);
  319. }
  320. }