HardwareSelectionModal.razor 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. @using System.ComponentModel.DataAnnotations
  2. @using RackPeek.Domain.Resources.Hardware
  3. @using RackPeek.Domain.Resources.Hardware.Models
  4. @inject IHardwareRepository HardwareRepository
  5. @if (IsOpen)
  6. {
  7. <div class="fixed inset-0 z-50 flex items-center justify-center">
  8. <!-- Backdrop -->
  9. <div class="absolute inset-0 bg-black/70" @onclick="Cancel"></div>
  10. <!-- Modal -->
  11. <div class="relative bg-zinc-900 border border-zinc-800 rounded w-full max-w-lg p-4">
  12. <!-- Header -->
  13. <div class="flex justify-between items-center mb-4">
  14. <div class="text-zinc-100 text-sm font-medium">
  15. @Title
  16. </div>
  17. <button
  18. class="text-zinc-400 hover:text-zinc-200"
  19. @onclick="Cancel">
  20. </button>
  21. </div>
  22. <!-- Form -->
  23. <EditForm Model="_model" OnValidSubmit="HandleAccept">
  24. <DataAnnotationsValidator />
  25. <div class="space-y-3 text-sm">
  26. <!-- Search -->
  27. <div>
  28. <InputText
  29. class="w-full bg-zinc-800 border border-zinc-700 rounded px-2 py-1 text-zinc-100"
  30. placeholder="Search hardware…"
  31. Value="@_search"
  32. ValueChanged="OnSearchChanged"
  33. ValueExpression="() => _search"
  34. @oninput="OnSearchInput" />
  35. </div>
  36. <!-- Selection -->
  37. <div>
  38. <InputSelect
  39. class="w-full bg-zinc-800 border border-zinc-700 rounded px-2 py-1 text-zinc-100"
  40. @bind-Value="_model.Value">
  41. <option value="">Select hardware…</option>
  42. @foreach (var group in FilteredHardware)
  43. {
  44. <optgroup label="@group.Key">
  45. @foreach (var hw in group.Value)
  46. {
  47. <option value="@hw.Name">@hw.Name</option>
  48. }
  49. </optgroup>
  50. }
  51. </InputSelect>
  52. </div>
  53. </div>
  54. <!-- Actions -->
  55. <div class="flex justify-end items-center mt-5 gap-2">
  56. <button
  57. type="button"
  58. class="px-3 py-1 rounded border border-zinc-700 text-zinc-300 hover:bg-zinc-800"
  59. @onclick="Cancel">
  60. Cancel
  61. </button>
  62. <button
  63. type="submit"
  64. class="px-3 py-1 rounded bg-emerald-600 text-black hover:bg-emerald-500"
  65. disabled="@(!CanAccept)">
  66. Accept
  67. </button>
  68. </div>
  69. </EditForm>
  70. </div>
  71. </div>
  72. }
  73. @code {
  74. /* ---------- Parameters ---------- */
  75. [Parameter] public bool IsOpen { get; set; }
  76. [Parameter] public EventCallback<bool> IsOpenChanged { get; set; }
  77. [Parameter, EditorRequired]
  78. public string Title { get; set; } = "Select hardware";
  79. /// <summary>
  80. /// Default / pre-selected hardware name (may be null)
  81. /// </summary>
  82. [Parameter]
  83. public string? Value { get; set; }
  84. /// <summary>
  85. /// Called with the selected hardware name
  86. /// </summary>
  87. [Parameter]
  88. public EventCallback<string?> OnAccept { get; set; }
  89. /* ---------- State ---------- */
  90. private SelectionFormModel _model = new();
  91. private string _search = string.Empty;
  92. private List<Hardware> _hardware = new();
  93. private bool CanAccept => !string.IsNullOrWhiteSpace(_model.Value);
  94. /* ---------- Lifecycle ---------- */
  95. protected override async Task OnParametersSetAsync()
  96. {
  97. if (IsOpen)
  98. {
  99. _hardware = (await HardwareRepository.GetAllAsync())
  100. .OrderBy(h => h.Kind)
  101. .ThenBy(h => h.Name)
  102. .ToList();
  103. _model = new SelectionFormModel
  104. {
  105. Value = Value
  106. };
  107. _search = string.Empty;
  108. }
  109. }
  110. protected override void OnAfterRender(bool firstRender)
  111. {
  112. }
  113. /* ---------- Computed ---------- */
  114. private IReadOnlyDictionary<string, List<Hardware>> FilteredHardware =>
  115. _hardware
  116. .Where(h =>
  117. string.IsNullOrWhiteSpace(_search) ||
  118. h.Name.Contains(_search.Trim(), StringComparison.OrdinalIgnoreCase))
  119. .GroupBy(h => h.Kind)
  120. .ToDictionary(
  121. g => g.Key,
  122. g => g.OrderBy(h => h.Name).ToList());
  123. /* ---------- Actions ---------- */
  124. private async Task HandleAccept()
  125. {
  126. await OnAccept.InvokeAsync(_model.Value);
  127. await Close();
  128. }
  129. private async Task Cancel()
  130. {
  131. await Close();
  132. }
  133. private async Task Close()
  134. {
  135. _model = new();
  136. _search = string.Empty;
  137. await IsOpenChanged.InvokeAsync(false);
  138. }
  139. /* ---------- Model ---------- */
  140. private sealed class SelectionFormModel
  141. {
  142. [Required]
  143. public string? Value { get; set; }
  144. }
  145. private void OnSearchChanged(string? value)
  146. {
  147. _search = value ?? string.Empty;
  148. }
  149. private void OnSearchInput(ChangeEventArgs e)
  150. {
  151. _search = e.Value?.ToString() ?? string.Empty;
  152. if (FilteredHardware.SelectMany(g => g.Value).All(h => h.Name != _model.Value))
  153. {
  154. _model.Value = null;
  155. }
  156. }
  157. }