SwitchCardComponent.razor 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 class="flex items-center justify-between text-zinc-300 group hover:bg-zinc-800/40 rounded px-1 py-0.5">
  138. <button data-testid=@($"edit-port-{port.Type}-{port.Speed}")
  139. class="hover:text-emerald-400"
  140. @onclick="() => OpenEditPort(port)">
  141. @port.Count× @port.Type — @port.Speed Gbps
  142. </button>
  143. </div>
  144. }
  145. }
  146. </div>
  147. <ResourceTagEditor Resource="Switch"
  148. TestIdPrefix="switch" />
  149. <ResourceLabelEditor Resource="Switch"
  150. TestIdPrefix="switch" />
  151. <div class="md:col-span-2"
  152. data-testid="switch-notes-section">
  153. <div class="text-zinc-400 mb-1">Notes</div>
  154. @if (_isEditing)
  155. {
  156. <MarkdownEditor @bind-Value="_edit.Notes"
  157. ShowActionButtons="false"
  158. TestIdPrefix="switch-notes-editor" />
  159. }
  160. else
  161. {
  162. <MarkdownViewer Value="@Switch.Notes"
  163. ShowEditButton="false"
  164. TestIdPrefix="switch-notes-viewer" />
  165. }
  166. </div>
  167. </div>
  168. </div>
  169. <PortModal IsOpen="@_portModalOpen"
  170. IsOpenChanged="v => _portModalOpen = v"
  171. Value="@_editingPort"
  172. OnSubmit="HandlePortSubmit"
  173. OnDelete="HandlePortDelete" />
  174. <ConfirmModal IsOpen="_confirmDeleteOpen"
  175. IsOpenChanged="v => _confirmDeleteOpen = v"
  176. Title="Delete switch"
  177. ConfirmText="Delete"
  178. ConfirmClass="bg-red-600 hover:bg-red-500"
  179. OnConfirm="DeleteServer"
  180. TestIdPrefix="Switch">
  181. Are you sure you want to delete <strong>@Switch.Name</strong>?
  182. </ConfirmModal>
  183. <StringValueModal IsOpen="_renameOpen"
  184. IsOpenChanged="v => _renameOpen = v"
  185. Title="Rename switch"
  186. Description="Enter a new name for this switch"
  187. Label="New switch name"
  188. Value="@Switch.Name"
  189. OnSubmit="HandleRenameSubmit"
  190. TestIdPrefix="switch-rename" />
  191. <StringValueModal IsOpen="_cloneOpen"
  192. IsOpenChanged="v => _cloneOpen = v"
  193. Title="Clone resource"
  194. Description="Enter a name for the cloned resource"
  195. Label="New resource name"
  196. Value="@($"{Switch.Name}-copy")"
  197. OnSubmit="HandleCloneSubmit"
  198. TestIdPrefix="switch-clone" />
  199. @code {
  200. [Parameter] [EditorRequired] public Switch Switch { get; set; } = default!;
  201. bool _isEditing;
  202. SwitchEditModel _edit = new();
  203. void BeginEdit()
  204. {
  205. _edit = SwitchEditModel.From(Switch);
  206. _isEditing = true;
  207. }
  208. async Task Save()
  209. {
  210. _isEditing = false;
  211. await UpdateUseCase.ExecuteAsync(
  212. Switch.Name,
  213. _edit.Model,
  214. _edit.Managed,
  215. _edit.Poe,
  216. _edit.Notes);
  217. Switch = await GetByNameUseCase.ExecuteAsync(Switch.Name);
  218. }
  219. void Cancel()
  220. {
  221. _isEditing = false;
  222. }
  223. #region Ports
  224. bool _portModalOpen;
  225. int _editingPortIndex;
  226. Port? _editingPort;
  227. void OpenAddPort()
  228. {
  229. _editingPortIndex = -1;
  230. _editingPort = null;
  231. _portModalOpen = true;
  232. }
  233. void OpenEditPort(Port port)
  234. {
  235. Switch.Ports ??= new List<Port>();
  236. _editingPortIndex = Switch.Ports.IndexOf(port);
  237. _editingPort = port;
  238. _portModalOpen = true;
  239. }
  240. async Task HandlePortSubmit(Port port)
  241. {
  242. if (_editingPortIndex < 0)
  243. {
  244. await AddPortUseCase.ExecuteAsync(
  245. Switch.Name,
  246. port.Type,
  247. port.Speed,
  248. port.Count);
  249. }
  250. else
  251. {
  252. await UpdatePortUseCase.ExecuteAsync(
  253. Switch.Name,
  254. _editingPortIndex,
  255. port.Type,
  256. port.Speed,
  257. port.Count);
  258. }
  259. Switch = await GetByNameUseCase.ExecuteAsync(Switch.Name);
  260. StateHasChanged();
  261. }
  262. async Task HandlePortDelete(Port _)
  263. {
  264. await RemovePortUseCase.ExecuteAsync(
  265. Switch.Name,
  266. _editingPortIndex);
  267. Switch = await GetByNameUseCase.ExecuteAsync(Switch.Name);
  268. StateHasChanged();
  269. }
  270. #endregion
  271. public class SwitchEditModel
  272. {
  273. public string? Model { get; set; }
  274. public bool? Managed { get; set; }
  275. public bool? Poe { get; set; }
  276. public string? Notes { get; set; }
  277. public static SwitchEditModel From(Switch Switch)
  278. {
  279. return new SwitchEditModel
  280. {
  281. Model = Switch.Model,
  282. Managed = Switch.Managed,
  283. Poe = Switch.Poe,
  284. Notes = Switch.Notes
  285. };
  286. }
  287. }
  288. }
  289. @code {
  290. private bool _confirmDeleteOpen;
  291. [Parameter] public EventCallback<string> OnDeleted { get; set; }
  292. void ConfirmDelete()
  293. {
  294. _confirmDeleteOpen = true;
  295. }
  296. async Task DeleteServer()
  297. {
  298. _confirmDeleteOpen = false;
  299. await DeleteUseCase.ExecuteAsync(Switch.Name);
  300. if (OnDeleted.HasDelegate)
  301. await OnDeleted.InvokeAsync(Switch.Name);
  302. }
  303. }
  304. @code
  305. {
  306. bool _renameOpen;
  307. void OpenRename()
  308. {
  309. _renameOpen = true;
  310. }
  311. async Task HandleRenameSubmit(string newName)
  312. {
  313. await RenameUseCase.ExecuteAsync(Switch.Name, newName);
  314. Nav.NavigateTo($"resources/hardware/{Uri.EscapeDataString(newName)}");
  315. }
  316. }
  317. @code
  318. {
  319. bool _cloneOpen;
  320. void OpenClone()
  321. {
  322. _cloneOpen = true;
  323. }
  324. async Task HandleCloneSubmit(string newName)
  325. {
  326. await CloneUseCase.ExecuteAsync(Switch.Name, newName);
  327. Nav.NavigateTo($"resources/hardware/{Uri.EscapeDataString(newName)}");
  328. }
  329. }