SwitchCardComponent.razor 11 KB

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