4
0

HardwareSelectionModal.razor 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. @using System.ComponentModel.DataAnnotations
  2. @using RackPeek.Domain.Persistence
  3. @using RackPeek.Domain.Resources.Hardware
  4. @inject IResourceCollection Repo
  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. @* ReSharper disable once CSharpWarnings::CS8619 *@
  29. <InputText
  30. class="w-full bg-zinc-800 border border-zinc-700 rounded px-2 py-1 text-zinc-100"
  31. placeholder="Search hardware…"
  32. Value="@_search"
  33. ValueChanged="OnSearchChanged"
  34. ValueExpression="() => _search"
  35. @oninput="OnSearchInput"/>
  36. </div>
  37. <!-- Selection -->
  38. <div>
  39. <InputSelect
  40. class="w-full bg-zinc-800 border border-zinc-700 rounded px-2 py-1 text-zinc-100"
  41. @bind-Value="_model.Value">
  42. <option value="">Select hardware…</option>
  43. @foreach (var group in FilteredHardware)
  44. {
  45. <optgroup label="@group.Key">
  46. @foreach (var hw in group.Value)
  47. {
  48. <option value="@hw.Name">@hw.Name</option>
  49. }
  50. </optgroup>
  51. }
  52. </InputSelect>
  53. </div>
  54. </div>
  55. <!-- Actions -->
  56. <div class="flex justify-end items-center mt-5 gap-2">
  57. <button
  58. type="button"
  59. class="px-3 py-1 rounded border border-zinc-700 text-zinc-300 hover:bg-zinc-800"
  60. @onclick="Cancel">
  61. Cancel
  62. </button>
  63. <button
  64. type="submit"
  65. class="px-3 py-1 rounded bg-emerald-600 text-black hover:bg-emerald-500"
  66. disabled="@(!CanAccept)">
  67. Accept
  68. </button>
  69. </div>
  70. </EditForm>
  71. </div>
  72. </div>
  73. }
  74. @code {
  75. /* ---------- Parameters ---------- */
  76. [Parameter] public bool IsOpen { get; set; }
  77. [Parameter] public EventCallback<bool> IsOpenChanged { get; set; }
  78. [Parameter] [EditorRequired] 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 Repo.GetAllOfTypeAsync<Hardware>())
  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 SelectionFormModel();
  136. _search = string.Empty;
  137. await IsOpenChanged.InvokeAsync(false);
  138. }
  139. /* ---------- Model ---------- */
  140. private sealed class SelectionFormModel
  141. {
  142. [Required] public string? Value { get; set; }
  143. }
  144. private void OnSearchChanged(string? value)
  145. {
  146. _search = value ?? string.Empty;
  147. }
  148. private void OnSearchInput(ChangeEventArgs e)
  149. {
  150. _search = e.Value?.ToString() ?? string.Empty;
  151. if (FilteredHardware.SelectMany(g => g.Value).All(h => h.Name != _model.Value))
  152. {
  153. _model.Value = null;
  154. }
  155. }
  156. }