SwitchCardComponent.razor 11 KB

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