SwitchCardComponent.razor 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. @using RackPeek.Domain.Resources.SubResources
  2. @using RackPeek.Domain.Resources.Switches
  3. @using RackPeek.Domain.UseCases.Ports
  4. @inject UpdateSwitchUseCase UpdateUseCase
  5. @inject IGetResourceByNameUseCase<Switch> GetByNameUseCase
  6. @inject IAddPortUseCase<Switch> AddPortUseCase
  7. @inject IUpdatePortUseCase<Switch> UpdatePortUseCase
  8. @inject IRemovePortUseCase<Switch> RemovePortUseCase
  9. @inject IDeleteResourceUseCase<Switch> DeleteUseCase
  10. @inject ICloneResourceUseCase<Switch> CloneUseCase
  11. @inject IRenameResourceUseCase<Switch> RenameUseCase
  12. @inject NavigationManager Nav
  13. <div class="border border-zinc-800 rounded p-4 bg-zinc-900"
  14. data-testid=@($"switch-item-{Switch.Name.Replace(" ", "-")}")>
  15. <div class="flex justify-between items-center mb-3">
  16. <div class="text-zinc-100 hover:text-emerald-300">
  17. <NavLink href="@($"resources/hardware/{Uri.EscapeDataString(Switch.Name)}")"
  18. class="block"
  19. data-testid=@($"switch-item-{Switch.Name.Replace(" ", "-")}-link")>
  20. @Switch.Name
  21. </NavLink>
  22. </div>
  23. <div class="flex gap-3 text-xs">
  24. @if (!_isEditing)
  25. {
  26. <button data-testid="edit-switch-button"
  27. class="text-zinc-400 hover:text-zinc-200"
  28. @onclick="BeginEdit">
  29. Edit
  30. </button>
  31. <button data-testid="rename-switch-button"
  32. class="text-blue-400 hover:text-blue-300 transition"
  33. @onclick="OpenRename">
  34. Rename
  35. </button>
  36. <button data-testid="clone-switch-button"
  37. class="text-emerald-400 hover:text-emerald-300 transition"
  38. @onclick="OpenClone">
  39. Clone
  40. </button>
  41. <button data-testid="delete-switch-button"
  42. class="text-red-400 hover:text-red-300 transition"
  43. @onclick="ConfirmDelete">
  44. Delete
  45. </button>
  46. }
  47. else
  48. {
  49. <button data-testid="save-switch-button"
  50. class="text-emerald-400 hover:text-emerald-300"
  51. @onclick="Save">
  52. Save
  53. </button>
  54. <button data-testid="cancel-switch-button"
  55. class="text-zinc-500 hover:text-zinc-300"
  56. @onclick="Cancel">
  57. Cancel
  58. </button>
  59. }
  60. </div>
  61. </div>
  62. <div class="grid grid-cols-1 md:grid-cols-2 gap-3 text-sm">
  63. <!-- Model -->
  64. <div data-testid="switch-model-section">
  65. <div class="text-zinc-400 mb-1">Model</div>
  66. @if (_isEditing)
  67. {
  68. <input data-testid="switch-model-input"
  69. class="w-full px-3 py-2 rounded-md bg-zinc-800 text-zinc-100 border border-zinc-600"
  70. @bind="_edit.Model"/>
  71. }
  72. else if (!string.IsNullOrWhiteSpace(Switch.Model))
  73. {
  74. <div class="text-zinc-300"
  75. data-testid="switch-model-value">
  76. @Switch.Model
  77. </div>
  78. }
  79. </div>
  80. <!-- Features -->
  81. <div data-testid="switch-features-section">
  82. <div class="text-zinc-400 mb-1">Features</div>
  83. @if (_isEditing)
  84. {
  85. <div class="flex gap-4">
  86. <label class="flex items-center gap-2 text-zinc-300">
  87. <input type="checkbox"
  88. data-testid="switch-managed-checkbox"
  89. @bind="_edit.Managed"/>
  90. Managed
  91. </label>
  92. <label class="flex items-center gap-2 text-zinc-300">
  93. <input type="checkbox"
  94. data-testid="switch-poe-checkbox"
  95. @bind="_edit.Poe"/>
  96. PoE
  97. </label>
  98. </div>
  99. }
  100. else
  101. {
  102. <div class="flex gap-2 flex-wrap">
  103. @if (Switch.Managed == true)
  104. {
  105. <span class="text-xs px-2 py-0.5 rounded bg-zinc-800 text-zinc-300"
  106. data-testid="switch-managed-badge">
  107. Managed
  108. </span>
  109. }
  110. @if (Switch.Poe == true)
  111. {
  112. <span class="text-xs px-2 py-0.5 rounded bg-zinc-800 text-zinc-300"
  113. data-testid="switch-poe-badge">
  114. PoE
  115. </span>
  116. }
  117. </div>
  118. }
  119. </div>
  120. <!-- Ports -->
  121. <div class="md:col-span-2"
  122. data-testid="switch-ports-section">
  123. <div class="flex items-center justify-between mb-1 group">
  124. <div class="text-zinc-400">
  125. Ports
  126. <button data-testid="add-port-button"
  127. class="hover:text-emerald-400 ml-1"
  128. @onclick="OpenAddPort">
  129. +
  130. </button>
  131. </div>
  132. </div>
  133. @if (Switch.Ports?.Any() == true)
  134. {
  135. @foreach (var port in Switch.Ports)
  136. {
  137. <div
  138. class="flex items-center justify-between text-zinc-300 group hover:bg-zinc-800/40 rounded px-1 py-0.5">
  139. <button data-testid=@($"edit-port-{port.Type}-{port.Speed}")
  140. class="hover:text-emerald-400"
  141. @onclick="() => OpenEditPort(port)">
  142. @port.Count× @port.Type — @port.Speed Gbps
  143. </button>
  144. </div>
  145. }
  146. }
  147. </div>
  148. <ResourceTagEditor Resource="Switch"
  149. TestIdPrefix="switch"/>
  150. <ResourceLabelEditor Resource="Switch"
  151. TestIdPrefix="switch"/>
  152. <div class="md:col-span-2"
  153. data-testid="switch-notes-section">
  154. <div class="text-zinc-400 mb-1">Notes</div>
  155. @if (_isEditing)
  156. {
  157. <MarkdownEditor @bind-Value="_edit.Notes"
  158. ShowActionButtons="false"
  159. TestIdPrefix="switch-notes-editor"/>
  160. }
  161. else
  162. {
  163. <MarkdownViewer Value="@Switch.Notes"
  164. ShowEditButton="false"
  165. TestIdPrefix="switch-notes-viewer"/>
  166. }
  167. </div>
  168. </div>
  169. </div>
  170. <PortModal IsOpen="@_portModalOpen"
  171. IsOpenChanged="v => _portModalOpen = v"
  172. Value="@_editingPort"
  173. OnSubmit="HandlePortSubmit"
  174. OnDelete="HandlePortDelete"/>
  175. <ConfirmModal IsOpen="_confirmDeleteOpen"
  176. IsOpenChanged="v => _confirmDeleteOpen = v"
  177. Title="Delete switch"
  178. ConfirmText="Delete"
  179. ConfirmClass="bg-red-600 hover:bg-red-500"
  180. OnConfirm="DeleteServer"
  181. TestIdPrefix="Switch">
  182. Are you sure you want to delete <strong>@Switch.Name</strong>?
  183. </ConfirmModal>
  184. <StringValueModal IsOpen="_renameOpen"
  185. IsOpenChanged="v => _renameOpen = v"
  186. Title="Rename switch"
  187. Description="Enter a new name for this switch"
  188. Label="New switch name"
  189. Value="@Switch.Name"
  190. OnSubmit="HandleRenameSubmit"
  191. TestIdPrefix="switch-rename"/>
  192. <StringValueModal IsOpen="_cloneOpen"
  193. IsOpenChanged="v => _cloneOpen = v"
  194. Title="Clone resource"
  195. Description="Enter a name for the cloned resource"
  196. Label="New resource name"
  197. Value="@($"{Switch.Name}-copy")"
  198. OnSubmit="HandleCloneSubmit"
  199. TestIdPrefix="switch-clone"/>
  200. @code {
  201. [Parameter] [EditorRequired] public Switch Switch { get; set; } = default!;
  202. bool _isEditing;
  203. SwitchEditModel _edit = new();
  204. void BeginEdit()
  205. {
  206. _edit = SwitchEditModel.From(Switch);
  207. _isEditing = true;
  208. }
  209. async Task Save()
  210. {
  211. _isEditing = false;
  212. await UpdateUseCase.ExecuteAsync(
  213. Switch.Name,
  214. _edit.Model,
  215. _edit.Managed,
  216. _edit.Poe,
  217. _edit.Notes);
  218. Switch = await GetByNameUseCase.ExecuteAsync(Switch.Name);
  219. }
  220. void Cancel()
  221. {
  222. _isEditing = false;
  223. }
  224. #region Ports
  225. bool _portModalOpen;
  226. int _editingPortIndex;
  227. Port? _editingPort;
  228. void OpenAddPort()
  229. {
  230. _editingPortIndex = -1;
  231. _editingPort = null;
  232. _portModalOpen = true;
  233. }
  234. void OpenEditPort(Port port)
  235. {
  236. Switch.Ports ??= new List<Port>();
  237. _editingPortIndex = Switch.Ports.IndexOf(port);
  238. _editingPort = port;
  239. _portModalOpen = true;
  240. }
  241. async Task HandlePortSubmit(Port port)
  242. {
  243. if (_editingPortIndex < 0)
  244. {
  245. await AddPortUseCase.ExecuteAsync(
  246. Switch.Name,
  247. port.Type,
  248. port.Speed,
  249. port.Count);
  250. }
  251. else
  252. {
  253. await UpdatePortUseCase.ExecuteAsync(
  254. Switch.Name,
  255. _editingPortIndex,
  256. port.Type,
  257. port.Speed,
  258. port.Count);
  259. }
  260. Switch = await GetByNameUseCase.ExecuteAsync(Switch.Name);
  261. StateHasChanged();
  262. }
  263. async Task HandlePortDelete(Port _)
  264. {
  265. await RemovePortUseCase.ExecuteAsync(
  266. Switch.Name,
  267. _editingPortIndex);
  268. Switch = await GetByNameUseCase.ExecuteAsync(Switch.Name);
  269. StateHasChanged();
  270. }
  271. #endregion
  272. public class SwitchEditModel
  273. {
  274. public string? Model { get; set; }
  275. public bool? Managed { get; set; }
  276. public bool? Poe { get; set; }
  277. public string? Notes { get; set; }
  278. public static SwitchEditModel From(Switch Switch)
  279. {
  280. return new SwitchEditModel
  281. {
  282. Model = Switch.Model,
  283. Managed = Switch.Managed,
  284. Poe = Switch.Poe,
  285. Notes = Switch.Notes
  286. };
  287. }
  288. }
  289. }
  290. @code {
  291. private bool _confirmDeleteOpen;
  292. [Parameter] public EventCallback<string> OnDeleted { get; set; }
  293. void ConfirmDelete()
  294. {
  295. _confirmDeleteOpen = true;
  296. }
  297. async Task DeleteServer()
  298. {
  299. _confirmDeleteOpen = false;
  300. await DeleteUseCase.ExecuteAsync(Switch.Name);
  301. if (OnDeleted.HasDelegate)
  302. await OnDeleted.InvokeAsync(Switch.Name);
  303. }
  304. }
  305. @code
  306. {
  307. bool _renameOpen;
  308. void OpenRename()
  309. {
  310. _renameOpen = true;
  311. }
  312. async Task HandleRenameSubmit(string newName)
  313. {
  314. await RenameUseCase.ExecuteAsync(Switch.Name, newName);
  315. Nav.NavigateTo($"resources/hardware/{Uri.EscapeDataString(newName)}");
  316. }
  317. }
  318. @code
  319. {
  320. bool _cloneOpen;
  321. void OpenClone()
  322. {
  323. _cloneOpen = true;
  324. }
  325. async Task HandleCloneSubmit(string newName)
  326. {
  327. await CloneUseCase.ExecuteAsync(Switch.Name, newName);
  328. Nav.NavigateTo($"resources/hardware/{Uri.EscapeDataString(newName)}");
  329. }
  330. }